简体   繁体   中英

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!

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 -n -r | head -n 1 - sort in reverse order and get the first (ie biggest) one
  • awk '{print $2}' | xargs -I % sh -c 'ln -sf % /path/to/symlink' awk '{print $2}' | xargs -I % sh -c 'ln -sf % /path/to/symlink' - extract full file name and create or update a symlink (eg in you case long_file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM