How to copy copy the largest file from folder to another using bash shell?

0

Within a directory with multiple subdirectories I can fetch all .py files, see only which ones have datetime, and show me only the largest of them. Using find, grep, ls and head, but when I try to copy the output of the head, in this case the largest file py. which has datetime, of error! I actually wanted to copy this file to grep it again to find all the classes and functions of that file. If anyone has any tips, thank you!

    
asked by anonymous 11.10.2018 / 15:16

1 answer

0

With the help of some commands, you can find the largest file like this:

find . -type f -printf "%s %p\n" | sort -n | tail -n1 | cut -f 2 -d' '

Explaining in parts:

  • find . -type f -printf "%s %p\n" finds all files that are of type file and prints the file size and path separated by a space
  • sort -n sorts output of find by size of files
  • tail -n1 takes the last line of sort which is the largest file
  • cut -f 2 -d' ' of last line, takes the second field using space as separator

Having the name of the file, just make the grep that is necessary, it is even possible to interpolate the previous commands with the grep and do everything on one line:

grep <padrão_procurado> "$(find . -type f -printf "%s %p\n" | sort -n | tail -n1 | cut -f 2 -d' ')"
    
10.12.2018 / 17:34