如何在bash中处理每一个文件?

kle*_*lew 6 bash scripting find

我有一个包含几十个文件的目录.我想对此目录中的每个第二个文件执行某些操作.到现在为止我只使用了find命令但是我处理了所有文件:

find ./dir/ -type f -exec cat {} \;
Run Code Online (Sandbox Code Playgroud)

plu*_*dra 15

for file in `find dir -type f | awk 'NR % 2 == 0'`; do
  echo $file
done
Run Code Online (Sandbox Code Playgroud)

NR是当前的行号.要获得奇数行,请使用... == 1.


ajr*_*eal 6

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
Run Code Online (Sandbox Code Playgroud)

要么

second_file=$(find -type f | head -2 | tail -1);
Run Code Online (Sandbox Code Playgroud)