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

Asked

Viewed 74 times

0

Inside a directory with several subdirectories I can search all the files . py, 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 head, in case the largest py. file that has datetime, error! Actually I wanted to copy this file to use grep on it again to find all the classes and functions of that file. If anyone has any tips, thank you!

  • based on stackoverflow responses in cp 'ls -S /path/to/Folder | head -1 ' /path/da/copy

1 answer

0

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

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

Halving:

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.