How to process every second file in bash?

后端 未结 4 1258
忘掉有多难
忘掉有多难 2021-02-15 23:55

I have a directory with a few dozens of files. I would like to do something with every second file from this directory. By now I only used find command but with thi

4条回答
  •  生来不讨喜
    2021-02-16 00:04

    cnt=0; 
    for file in $(find ./dir -type f); <-- if not too many matches
    do 
      let cnt=cnt+1; 
      if [ $cnt -eq 2 ]; 
        then echo $file;               <-- do something
        cnt=0;                         <-- alternate file
      fi; 
    done
    

    or

    second_file=$(find -type f | head -2 | tail -1);
    

提交回复
热议问题