technonews technonews - 1 year ago 46
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
(files that having 100 lines or more) to folder
/home/john/folder b

Can someone help me please? Thank you very much.

Answer Source

Try this :

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

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.