How to process every second file in bash?

后端 未结 4 1275
忘掉有多难
忘掉有多难 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:13

    I had every file twice, and needed to delete every second file. Find just returned me random files, therefore I added a sort. It now looks like this:

    #!/bin/bash
    DIRNAME=""
    for file in `find $DIRNAME -type f | sort | awk 'NR % 2 == 0'`; do
      echo "going to modify" $file
      #  ls -laFh $file           # show file details
      #  rm $file                 # delete file
      #  mv $file     # move file to 
    done
    

    put this in a file called scriptName, run

    chmod +x scriptName
    

    and start it by calling

    ./scriptName
    

提交回复
热议问题