[command1]; [command2] executes both programs one after the other. Example: ls -a ; du --max-depth=1 Will first print a complete directory listing to the screen and then the sizes of the subdirectories. [command1] & [command2] executes command1 in the background and command2 in the foreground. Example: ls -a & du --max-depth=1 Works like above, but you will see an additional line: [2]+ Done ls --color=auto -F -a . The ls command has been executed as a 'job'. If you run this command on a really large directory, you will note that it is faster than the first one. ([command1] ; [command2]) > file.txt puts the output of command1 and command2 in the file 'file.txt'. The parentheses are necessary here to have both programs executed in the same 'sub-shell' (otherwise only the output of [command2] would be written to 'file.txt', whereas the output of [command1] would be put onto the terminal). So this means what? That parentheses create a sub-shell. This means more than just the output of both commands going to a file, it could go to a pipe, whatever... Example: (ls -a ; du --max-depth=1) > file.txt Will write two things to the file 'file.txt': a complete listing of the current directory and then the sizes of the subdirectories. ls -a ; du --max-depth=1 > file.txt However prints the directory listing on the screen and puts the sizes of the subdirectories into the file 'file.txt'. (ls -a ; du --max-depth=1) | grep srchcriteria This is a pipe example, you could do a grep this | grep that to double filter the content, based on the different type of data returned by the subshell. [command1] && [command2] runs 'command2' if and only if command1 HAS NOT returned an error. Example: ls -a bogusdir && du --max-depth=1 Will return ls: bogusdir: No such file or directory and du won't be executed at all (presuming you don't have a directory called 'bogusdir', that is ;-)). [command1] || [command2] runs 'command2' if and only if 'command1' DOES returned an error. Example: ls -a bogusdir || du --max-depth=1 Will return the ls again but this time du will be executed. If there were a directory called 'bogusdir', du won't. Useful Commands and Pipe Combinations
To unpack a tarred gzip: # gunzip -c file.tar.gz | tar -xvf - or # tar -xvfz file.tar.gz To tar a directory and gzip the tar: # tar -cvf - /path/to/dir | gzip -9c > file.tar.gz or # tar -cvfz file.tar.gz /etc Moving directories between filesystems. Quick way to move an entire tree of files as a sequential stream. Notice this is subshell to subshell. (cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xvfp -) Finding out which directories are the largest. du -S | sort -n How to find the biggest files on your hard-drive. ls -l | sort +4n Complete filesystem recursion: cd /; ls -lR | sort +4n How to find out what process is eating the most memory. ps -aux | sort +4n -OR- ps -aux | sort +5n The following command zeroes out a device. This can be important because if the filesystem on the device will be compressed later, all unused portions should be filled with zeroes to achieve maximum compression. dd if=/dev/zero of=DEVICE bs=1k count=3000 Use the cat command to put a header and trailer: ls -l | cat header - trailer | lpr The above command pipes the results of the ls directory listing to the cat command. Within the cat command, the STDIN is represented by the - . cat is simply concatenating the file 'header' then putting in STDIN, and then the file 'trailer'. After all this, it is piped to the lpr printing command. So, the command prints a directory listing that begins with a custom file called header and ends with trailer. Use the script command to save a copy of everything that appears on your screen into a file. Both the standard input that you type and the standard output and standard error streams that programs produce are captured by script into the file. Give the command: script filename Then everything that goes onto the screen (input or output) will also go into filename until you type a CTRL-D character or the command exit, which ends the script command and closes the file. Use this to get a record of a login session for debugging. cd fromdir; grep 'farrell' psout | (cd todir; cat > psfarrell)