LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 04-17-2005, 09:42 AM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
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
 
Old 04-17-2005, 10:34 AM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
I think you want to replace the last pipe with >
 
Old 04-17-2005, 11:22 AM   #3
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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
 
Old 04-17-2005, 11:32 AM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
It will need to be:
> tee /tmp/bigfiles.list
 
Old 04-17-2005, 02:09 PM   #5
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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
 
Old 04-18-2005, 03:25 AM   #6
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
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).
 
Old 04-18-2005, 02:58 PM   #7
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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]$
 
Old 04-18-2005, 03:01 PM   #8
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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]$
 
Old 04-18-2005, 03:18 PM   #9
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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?
 
Old 04-18-2005, 03:56 PM   #10
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
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.
 
Old 04-18-2005, 04:12 PM   #11
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Sircliff

You are great Sir!
 
Old 04-18-2005, 04:13 PM   #12
punt
Member
 
Registered: Jun 2001
Distribution: Fedora 22
Posts: 371

Rep: Reputation: 30
interesting command. thanks for sharing and for the detailed explanation
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 07:19 PM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM
Why after start of a command the invitation for input next command has not appeared. ukrainet Linux - General 3 06-24-2005 08:16 AM
Key stroke/command to shut down x and go into the command prompt screen? Fear58 Linux - General 1 07-14-2004 07:14 PM
Where is Command line utility for Cups and command tutorial mossy Linux - Software 8 01-16-2004 12:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration