How to find the largest file in a directory and its subdirectories?

前端 未结 15 1411
醉梦人生
醉梦人生 2020-11-28 18:29

We\'re just starting a UNIX class and are learning a variety of Bash commands. Our assignment involves performing various commands on a directory that has a number of folder

15条回答
  •  伪装坚强ぢ
    2020-11-28 18:55

    This lists files recursively if they're normal files, sorts by the 7th field (which is size in my find output; check yours), and shows just the first file.

    find . -type f -ls | sort +7 | head -1
    

    The first option to find is the start path for the recursive search. A -type of f searches for normal files. Note that if you try to parse this as a filename, you may fail if the filename contains spaces, newlines or other special characters. The options to sort also vary by operating system. I'm using FreeBSD.

    A "better" but more complex and heavier solution would be to have find traverse the directories, but perhaps use stat to get the details about the file, then perhaps use awk to find the largest size. Note that the output of stat also depends on your operating system.

提交回复
热议问题