technonews technonews - 4 months ago 8
Bash Question

How to copy files that having line number > 100 to other folder

I want to copy files that having 100 lines or more to another folder:

$ cd "/home/john/folder a"
$ wc -l *
10 file1.txt
50 file2.txt
100 file3.txt
150 file4.txt


I want to copy file
file3.txt
and
file4.txt
(files that having 100 lines or more) to folder
/home/john/folder b
.

Can someone help me please? Thank you very much.

Answer

Try this :

declare -i numfile
for f in *; do
  numfile=$([ -f "$f" ] && cat $f | wc -l )
  [ $numfile -ge 100 ] && cp $f otherdir
done

For each files in current directory, numfile is assigned the number of lines of the file.

If numfile is greater or equal to 100, file is copied to otherdir.