echo $PATHSuppose you want to make sure that the first directory that the shell looks at is a directory called mycommands in your home directory. You could modify the PATH variable as follows:
export PATH=~/mycommands:$PATHFirst, note that the previous command path will still be searched after your local directory, because $PATH (the old value) is included as part of the new path. It is usually a very bad idea to overwrite the entire command path - usually one or two directories will be appended or prepended to the path as shown in the example above. The export command insures that other shells created by the current shell (for example, to run a program) will inherit the modified path; such a variable is sometimes known as an environmental variable. Without the export command, any shell variables that are set will only be modified within the current shell.
alias bye=logoutand you could then type bye in that shell to logout. As another example, suppose you find the -F option of ls (which displays / after directories, * after executable files and @ after links) to be very useful. The command
alias ls='ls -F'will insure that the -F option will be used whenever you use ls. If you need to use the unaliased version of something for which you've created an alias, precede the name with a backslash (\). For example, to use the normal version of ls after you've created the alias described above, just type
\lsat the bash prompt. Of course, the real utility of aliases is only achieved when they are automatically set up whenever you log in to the computer. To achieve that goal with aliases (or any other bash shell commands), simply insert the commands in the file .bashrc in your home directory.
? | a single character |
* | zero or more characters |
[c1c2...] | matches c1, c2, ... |
[^c1c2...] | matches anything but c1, c2, ... |
[c1-c2] | matches the range from c1 to c2 |
{string-1,string-2,¼} | uses each string in turn |
~ | your home directory |
~user | user's home directory |
control-A | beginning of line |
control-E | end of line |
control-N | next command |
control-P | previous command |
control-F | one character forward |
control-B | one character backward |
control-D | delete character |
control-K | delete to end of line |
control-W | delete to beginning of line |
!! | previous command |
!-n | nth previous command |
!$ | last token from previous line |
^old^new^ | previous command with old substituted with new |
!!-n:s/old/new/ | nth previous command with old substituted with new |
command >file | send command's standard output to file, |
overwriting its contents | |
command >>file | send command's standard output to file, |
appending to existing contents | |
command >file 2>errors | send command's standard output to file, |
and its standard error to errors | |
command &>file | send command's standard output |
and its standard error to the same location | |
command < file | execute command, reading standard input from file |
command < file > output | execute command, reading standard input from file |
and sending standard output to output | |
command << XXX | execute command using the following lines, until a line |
with only XXX (arbitrary) is found | |
command-1 | command-2 | use standard output from command-1 as |
standard input to command-2 |
head | shows first few lines of a file |
tail | shows last few lines of a file |
wc | counts lines, words and characters of a file |
grep | finds patterns in a file |
sort | sorts the lines of a file |
less | displays a file one screen at a time |
ls -t | head -10and we can search for the required pattern using grep. Putting these together with the backtick operator we can solve the problem using
grep graphics `ls -t | head -10`Note that piping the output of the ls command into grep would not achieve the desired goal, since grep reads its filenames from the command line, not standard input.
matlab < matlab.in &> matlab.outwhile the following command would send the output to matlab.out and the errors to matlab.err:
matlab < matlab.in > matlab.out 2> matlab.errIf you forget to put a job in the background when you first execute it, you can do it while it's running in the foreground in two steps. First, suspend the job using the control-Z signal. After receiving the signal, the program will stop executing, but will still have access to all files and other resources. Next, issue the bg command, which will put the stopped job in the background. Since only foreground jobs will accept signals through the keyboard, if you want to terminate a background job you must first determine the unique process id (PID) for the process you wish to terminate through the use of the ps command. (The method described here also works for foreground jobs, although directly signalling is ofter simpler.) For example, to see all the R jobs running on a particular computer, you could use a command like:
ps -aux | grep RAmong the output there might appear a line that looks like this:
spector 11998 0.4 10.6 141312 108956 pts/20 S+ 15:22 0:13 /usr/local/linux/R-2.4.0/bin/exec/RIn this example, the ps output tells us that this R job has a PID of 11998. You could then issue the command:
kill 11998or, if that doesn't work
kill -9 11998to terminate the job. Another useful command in this regard is killall, which accepts a program name instead of a process id, and will kill all instances of the named program. Of course, it will only kill the jobs that belong to you, so it will not affect the jobs of other users. The most important thing to keep in mind when managing your running jobs is that the ps and kill commands only apply to the particular computer on which they are executed, not to the entire computer network. Thus, if you start a job on one machine, you must log back into that same machine in order to manage your jobs.