LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   A command (https://www.linuxquestions.org/questions/linux-general-1/a-command-314146/)

Gins 04-17-2005 09:42 AM

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

david_ross 04-17-2005 10:34 AM

I think you want to replace the last pipe with >

Gins 04-17-2005 11:22 AM

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

david_ross 04-17-2005 11:32 AM

It will need to be:
> tee /tmp/bigfiles.list

Gins 04-17-2005 02:09 PM

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

Ygrex 04-18-2005 03:25 AM

Do you understand what you write?
 
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).

Gins 04-18-2005 02:58 PM

It didn't work. Please read the following:

[ka@c83-250-94-177 ka]$ find . -type f -exec ls -s {} \; |sort -n -r| head -20| cat -n > tee
[ka@c83-250-94-177 ka]$


[ka@c83-250-94-177 ka]$ find -type f -exec ls -s {} \; |sort -n -r| head -20| cat -n > tee
[ka@c83-250-94-177 ka]$

Gins 04-18-2005 03:01 PM

Finally it worked. Thanks everbody.

[ka@c83-250-94-177 ka]$ find -type f -exec ls -s {} \; |sort -n -r| head -20| cat -n | tee
1 16500 ./glibc-2.3.3-12.8.100mdk.src.rpm
2 16176 ./jre-1_5_0_02-linux-i586.bin
3 9964 ./.mozilla/firefox/fe63pxfo.default/Cache/_CACHE_003_
4 9568 ./Documents/Firefox/firefox-installer/firefox-bin
5 9012 ./Bitwise/BitWise/BitWise
6 8456 ./Documents/Firefox/firefox-1.0.2.installer.tar.gz
7 6324 ./Documents/Firefox/firefox-installer/xpi/browser.xpi
8 6032 ./php/php-4.3.11/sapi/cgi/php
9 6028 ./php/php-4.3.11/sapi/cli/php
10 5792 ./skype/skype-1.0.0.20-mdk.i586.rpm
11 5792 ./skype-1.0.0.20-mdk.i586.rpm
12 4412 ./.mozilla/firefox/fe63pxfo.default/Cache/_CACHE_001_
13 3924 ./php/php-4.3.11.tar.bz2
14 3924 ./php-4.3.11.tar.bz2
15 3900 ./xscreensaver-4.14.tar.gz
16 3900 ./.mozilla/firefox/fe63pxfo.default/Cache/BAB03D27d01
17 3888 ./.mozilla/firefox/fe63pxfo.default/Cache/A6DCA76Ad01
18 3888 ./apache_1.3.33.tar.Z
19 3612 ./.mozilla/firefox/fe63pxfo.default/Cache/_CACHE_002_
20 3156 ./Bitwise/BitWiseBeta22.tar.gz
[ka@c83-250-94-177 ka]$

Gins 04-18-2005 03:18 PM

Could you parse the command?

What is 'tee' doing?

What is 'cat-n' doing?

What is 'head -20' doing? Of course 20 means number of files here.

Why do you have to write pipe? You find pipe in three places.

|sort -n -r|

| head -20|

| cat -n | I am used to writing the 'cat' command to read the context of a text file.


What is ' sort -n -r ' doing? Of course it is doing some sorting.

What is ' \; ' is doing here?

sirclif 04-18-2005 03:56 PM

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.

hope this makes sense and helps.

Gins 04-18-2005 04:12 PM

Sircliff

You are great Sir!

punt 04-18-2005 04:13 PM

interesting command. thanks for sharing and for the detailed explanation :)


All times are GMT -5. The time now is 02:20 AM.