Home > Enterprise >  move all files in a directory that have less than X lines
move all files in a directory that have less than X lines

Time:10-28

I'm trying to move all files in a directory that have less than X lines (in my case 6)

I tried :

find /path/folder/ -type f -exec awk -v lines=6 'NR>lines{f=1; exit} END{exit !f}' {} \; -exec echo mv output_folder/ {} \;

But it's not working, any files is moved. Any tips?

CodePudding user response:

Since this is tagged as bash.

find /path/folder -type f -exec sh -c '
  for f; do
    total_count=$(wc -l < "$f")
    if [ "$total_count" -lt 6 ]; then
      echo mv -vn -- "$f" /destination
    fi
  done' _ {}  

In one line.

find /folder/path -type f -exec sh -c 'for f; do total_count=$(wc -l < "$f"); if [ "$total_count" -lt 6 ]; then echo mv -vn -- "$f" /destination; fi; done' _ {}  

  • Related