Search code examples
linuxbashunixwc

select and copy files with N lines


I would like to select and copy from a folder only the files with a number N of lines.

How it is possible to do this in Bash?

Al.


Solution

  • You could do this using a loop in bash:

    for f in *; do
        [ -f "$f" ] && [ $(wc -l < "$f") = 8 ] && cp "$f" "$dest"
    done
    

    This will loop through all the files and folders in your directory. The first test checks the target is a file. The second checks that the number of lines is 8. If both are true, cp the file to "$dest".

    edit: If you wanted to include hidden files as well, you could change the loop to for f in .* *. Thanks @chepner for bringing this to my attention.