man lsat the UNIX prompt. Another way to use the man command is to use keywords; type either
man -k keywordor
apropos keywordat the UNIX prompt to find a list of commands that involve "keyword". One attractive feature of UNIX shells is tab completion; if you type only the first few letters of a command or file name, and then hit the tab key, the shell will complete the name you started to type provided that there is only one match. If there's more than one match, hitting tab twice will list all the names that match. A properly configured UNIX file system has permission controls to prevent unauthorized access to files, and to make sure that users do not accidently remove or modify key files. If you want to adjust the permissions on the files you own, take a look at the man page for the chmod command. I'm not going to make a big distinction between UNIX, Linux and the UNIX core of Mac OSX, so the commands we're going to look at here should work under any of these operating systems.
echo $PATHTo see the complete list of commands that are available on a given computer, you could look at all the files (commands) in all of the directories on your search path. (There are well over 2000 commands on most UNIX systems.)
Command | Description | Examples |
ls | Lists files in a given directory | ls /some/directory |
ls # with no args, lists current dir | ||
cd | Change Working Directory | cd /some/directory |
cd #with no args, cd to home dir | ||
pwd | Print Working Directory | pwd |
mkdir | Create New Directory | mkdir subdirectory |
less | Display file one screen at a time | less filename |
cp | Copy files | cp file1 newfile1 |
cp file1 file2 file3 somedirectory | ||
mv | Move or rename a file | mv oldfile newfile |
mv file1 file2 file3 somedirectory | ||
rm | Remove a file | rm file1 file2 |
rm -r dir #removes all directories and subdirectories | ||
rmdir | Remove a (empty) directory | rmdir mydir |
history | Display previously typed commands | history |
grep | Find strings in files | grep Error file.out |
head | Show the first few lines of a file | head myfile |
head -20 myfile | ||
tail | Show the last few lines of a file | tail myfile |
tail -20 myfile | ||
file | Identify the type of a file | file myfile |
man ls
Command | Meaning | Command | Meaning |
control-p | Previous line | control-n | Next line |
control-f | One character forward | control-b | One character backward |
control-a | Beginning of line | control-e | End of line |
control-d | Delete one character | control-k | Delete to end of line |
Wildcard | Meaning |
* | Zero or more of any character |
? | Any single character |
[...] | Any of the characters between the brackets |
[^...] | Any characters except those between the brackets |
[x-y] | Any character in the range x to y [0-9] [a-z] |
string-1,string-2,string-3 | Each of the strings in turn |
ls > myfilesSuch a command will overwrite the file myfiles if it already existed. To redirect output to the end of a file, leaving any content intact, use
ls >> myfilesTo have a command read its input from a file instead from standard input the less-than sign (<) can be used. Another useful form of redirection is known as a pipe. In this case, the standard output of one program is used as the standard input to another program. A very common use of pipes is to view the output of a command through a pager, like less. This allows you to view a screen at a time by pressing the space bar, to move up in the file by using control-u, and to move down using control-d. For example, suppose that you are listing the files in a directory that contains many, many files. If you simply type ls, the output will go streaming by too fast to read. But by typing
ls | lessthe output will be displayed one screen at a time, and you can navigate using the commands described above. As another example, suppose we want to find which files we've modified recently. The -lt option of the ls command will provide a long display of files in reverse chronological order; to display, say the five most recently modified files we could use
ls -lt | head -5
bgto put it in the background. Notice that jobs that are suspended with control-Z continue to use resources even if they are not running, so when you really want to stop a job you should use control-C or control-\. If you want to put a suspended job in the foreground, use the fg command. We've seen how to manage jobs when we still have access to the shell from which we started them, but sometimes you need to find out about jobs for which we don't have access to their originating shells. For example, you may remotely log in to a computer, start a job, log out and then want to find out if it's running, or to stop it. The ps command can be used to find out about all the programs that are running on a computer. Remember that for a networked system of computers, the ps command will only display information about commands that are running on the particular computer you're using, so if you put a job in the background and log off, it's a very good idea to remember the name of the computer you were using so that you can check on its progress later. To find all the commands you're running on a particular computer type:
ps -aux | grep usernamewhere username is the account name you logged in with. A single line of ps output looks like this:
spector 1325 10380 0 09:48 pts/10 00:00:00 /bin/bash /usr/local/linux/bin/RNotice that the name of the program that's running is shown at the end of the line. You can identify a particular program by looking for its name there, or by using a second grep command when invoking ps. The second column of the output contains the process id or PID of the process; this number uniquely identifies your job on a particular computer. If you want to terminate a job whose pid you know, type
kill pidThis is similar to control-C. To get the same affect as control-\, type
kill -9 pidwhere pid is the number from the ps output.