Some Linux shell cheats, tricks and tips
1 File Descriptors
File descriptors are simply positive integers that represent your open files.
In unix, everything is a file, and you may have many open files, including where
a process sends its output and error messages. These are called standard file
descriptors as discussed next.
2 Standard File Descriptors
There are two standard output file descriptors
- 1 is
stdout - 2 is
stderr
By default both are the terminal where the command was entered.
When running a script from a cron job, i.e NOT from the standard terminal
you can redirect &1 and &2 to some other location, typically a file.
batchjob.py > ~/batchoutput 2>&1
This means the standard output is the file ~/batchoutput which also is
automatically assigned the standard file descriptor 1. By adding the
2>&1 we are also telling bash to also redirect stderr to the same same place,
i.e. redirect 2 to what &1 is, which in this case is the file ~/batchoutput
2.1 Common errors
2>1 it would simply rediredt standard errors to a file called '1'
job &2>&1 it would run "job" in the background, & There does not have to be
a space between the first & and the 2, so that simply takes "2" which is
stderr and redirects it to, in this case &1, which is stdout.
3 Command line shortcuts
!!previous command, often used withsudo !!!$previous command's last argument, often used withls *.*~; rm !$M-.does the same as!$i.e. the previous command's last argument!-2two commands ago!-nnth commands ago!545the 545th command, often used afterhistory | grep somestring