Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Rep:
A command
The following command should list the 20 largest files on my system. I found the command in a Linux magazine. I just wrote it down on a piece of paper; I didn't buy the magazine.
# find . -type f -exec ls -s {} \; |sort -n -r| head -20| cat -n| tee /tmp |bigfiles.list
The following is the output. What is the problem?
[ka@c83-250-89-145 ka]$ find . -type f -exec ls -s {} \; |sort -n -r| head -20| cat -n| tee /tmp |bigfiles.list
bash: bigfiles.list: command not found
tee: /tmp: Is a directory
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
I did the way you suggested. It didn't work
[ka@c83-250-89-145 ka]$ find . -type f -exec ls -s {} \; |sort -n -r| head -20| cat -n > tee /tmp bigfiles.list
cat: /tmp: Is a directory
cat: bigfiles.list: No such file or directory
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
It didn't work. The following is the output:
[ka@c83-250-89-145 ka]$ find -type f -exec ls -s {} \; |sort -n -r| head -20| cat -n> tee /tmp/bigfiles.list
cat: /tmp/bigfiles.list: No such file or directory
What is a nonsense: "cat -n > tee /tmp/bigfiles.list" ?
Did you mean ''cat -n >/tmp/bigfiles.list && tee /tmp/bigfiles.list" ?
Or in the initial form: "cat -n| tee /tmp |bigfiles.list",
may be "cat -n| tee" without creating any file? But i do not remember the tee command so i do not sure in this tip. If tee does not get input from stdin it will not work (use the first paragraph).
find -type f -exec ls -s {} \; |sort -n -r| head -20| cat -n | tee
tee is going nothing, if a filename is given as an argument to tee it will dump the output into the file as well as to the standard output.
cat -n tells cat out number all the output lines
head -20 tell head to print out the first 20 lines of either stdin or a file, in this case stdin
sort -n -r tells sort to sort numerically and in reverse order, biggest to smallest
the pipes direct the standard output of one command to the other, so you get
find starts looking in the current directory for all files of type f (regular file), when it finds something it executes the command ls -s on it. the {} is replaced with the file that find finds. the ; tells find that there are no more arguments to the -exec option, so -exec gets three arguments: ls, -s, and {}. (the -s option to ls prints the file size before the file name) however, it is necessary to excape the ; because it has meaning in the shell.
so find has built a list of all files in the current directory (and it's subdirectories) and has printed thier size before their names. instead of printing it to the screen though, the output is piped to sort, where it is sorted numerically (since the size of each file is printed first, the output is sorted by file size) and in reverse order (bigger files first). that output is piped to head which takes it and only reprints the first 20 lines (the 20 largest files). those 20 lines are then piped to cat, which simply takes standard in and prints it to standard out, however the -n option is given so it prints the line number as well.
all of this is piped to tee, which takes standard input and prints it to both the standard output, and a file given as an argument. in this case, no file is given, however if you were to give the command.
find -type f -exec ls -s {} \; |sort -n -r| head -20| cat -n | tee tmp_file.txt
then a file called tmp_file.txt would be created and contain the same thing that was printed to the screen.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.