Create a symbolic link named long_file pointing to the largest file in the current directory

Viewed 24

I know how to create a symbolic link but I don't kow how to get largetst file in current directory. Please, help me!

1 Answers

You can do it with the following command:

find /path/to/dir-with-big-file/ -type f -printf "%s\t%p\n" | sort -n -r | head -n 1 | awk '{print $2}' | xargs -I % sh -c 'ln -sf % /path/to/symlink'

which breaks down as follows:

  • find /path/to/dir-with-big-file/ -type f -printf "%s\t%p\n" - find files and print %s files size and %p name.
  • sort -n -r | head -n 1 - sort in reverse order and get the first (i.e. biggest) one
  • awk '{print $2}' | xargs -I % sh -c 'ln -sf % /path/to/symlink' - extract full file name and create or update a symlink (e.g. in you case long_file)
Related