On 04-May-07 13:50:51, Barrys linux mail wrote:
Govind Chandra wrote:
find . -empty | xargs rmdir
Cheers
Govind Chandra
Hi really dumb question, still learning about commands for the terminal. please tell me what is the symbol after the word empty and before xargs, is that you hitting the enter key? Barry
No, it's the vertical bar (Shift + "" on most keyboards).
It's used in a command to denote a "pipe" -- the output of one command ("find" in this case) is piped into the input of another command (here "xargs").
The concept of a pipe goes back to the very origins of Unix (from which Linux is derived, of course), and is a key component in the originators' ideal that complex tasks should be broken down into simpler tasks, and each such task assigned to a small and simple program specially written to perform that specific kind of task efficiently and well.
Thus, for instance, go into a directory with a lot of files in it, and give the command below (you can enter the following lines separately as shown, if you want to, since the shell will wait until the command is syntactically complete):
ls -l | awk '{print $5 " " $9}' | sort -n | awk '{cum += $1};{print $2 "\t" $1 "\t" cum}' | less
This will print the name of each file, followed by its size in bytes, followed by the cumulative total of sizes so far, with the filenames being printed in the order of increasing file size. rationale:
ls -l |
lists the files in long format, with the filename in position 9 and the byte-size in position 5, and pipes this output intoL
awk '{print $5 " " $9}' |
which extracts the fields in the 5th and 9th positions and outputs them (in that order) separated by a space; and pipes them into
sort -n |
which sorts its input in numerical order (so that 256 precedes 1024, and not the other (alphabetical) way round, which is what 'sort' does by default); and pipes its output into
awk '{cum += $1};{print $2 "\t" $1 "\t" cum}' |
which calculates the cumulative total 'cum' by adding the number in the first position (byte-size) to 'cum'. and then outputting the results as follows:
first, the item in the second position of its intput (filename) next, the item in the first position (file byte-size) finally, the value of 'cum' all separated by TABs; and pipes its output to
less
so that you can page through it.
There's also a command 'tee' (for "T-junction") such that
<anything that produces output> | tee file1 file2 ... filek
copies the output into all of the files file1 ... filek and *also* produces output which is available for piping into any other command.
This you can set up not just a chain of piped commands as above, but can also pick up the results of earlier commends and process these at a later stage, and merge them in.
These sorts of command linkage are known in the trade as "Unix plumbing", and represent a technique for joining up simple commands (each of which "does one job and does it well" in the words of Kernighan and Ritchie) to carry out a complicated task.
It not only beats the clunky cumbersome performance of "this program can do everything" software, which is usually massively heavy and in any cases doesn't "do everything" but only what its writers thought it needed to do; may not do it very well (think Word); and doesn't offer you much scope to "roll your own".
Hoping this helps, Ted.
-------------------------------------------------------------------- E-Mail: (Ted Harding) ted.harding@nessie.mcc.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 04-May-07 Time: 15:28:18 ------------------------------ XFMail ------------------------------