Skip to content
Advertisement

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

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

Advertisement

Answer

You can do it with the following command:

find /path/to/dir-with-big-file/ -type f -printf "%st%pn" | 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 "%st%pn" – 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)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement