This section describes some of the more important directory and file handling commands.
pwd displays the full absolute path to the your current location in the filesystem. So
$ pwd /usr/bin
implies that /usr/bin is the current working directory.
$ cd path
changes your current working directory to path (which can be an absolute or a relative path). One of the most common relative paths to use is '..' (i.e. the parent directory of the current directory).
Used without any target directory
$ cd
resets your current working directory to your home directory (useful if you get lost)
If you change into a directory and you subsequently want to return to your original directory, use
$ cd -
Exercises:
Try to move around using cd, and think where you are currently
located in the file system. Confirm it by pwd after each cd.
$ cd $ pwd $ cd .. $ pwd $ cd ~ $ cd /usr/ $ cd include/X11/ICE $ cd ../../.. $ cd ../tmp $ cd - $ cd - $ cd /root
ls lists the contents of a directory. If no target directory is given, then the contents of the current working directory are displayed. So, if the current working directory is /,
$ cd / $ ls bin dev home mnt share usr var boot etc lib proc sbin tmp vol
Actually, ls doesn't show you all the entries in a directory - files and directories that begin with a dot (.) are hidden (this includes the directories '.' and '..' which are always present). The reason for this is that files that begin with a . usually contain important configuration information and should not be changed under normal circumstances. If you want to see all files, ls supports the -a option:
$ ls -a
Even this listing is not that helpful - there are no hints to properties such as the size, type and ownership of files, just their names. To see more detailed information, use the -l option (long listing), which can be combined with the -a option as follows:
$ ls -a -l(or, equivalently,)
$ ls -al
Each line of the output looks like this:
type links size | | | drwxr-xr-x 2 cowboyNeal iab 4096 Jan 20 17:38 cowboyNeal --------- | | | | permission owner group date namewhere:
ls supports more options. To find out what they are, type:
$ man ls
man is the online UNIX user manual, and you can use it to get help with commands and find out about what options are supported. It has quite a terse style which is often not that helpful, so some users prefer to the use the (non-standard) info utility if it is installed:
$ info ls
Exercises:
$ cd $ ls $ ls -a $ ls -l $ ls /binDo you see ls and pwd listed?
$ ls -l /Does this tell you why you couldn't do cd /root?
$ cd /home $ lsWhat do you think these files are?
If you use Mac OS-X, try to explore the file system with cd, ls, and pwd. You can use Terminal.app (in /Applications/Utilities) or the terminal of X11.app. Do you notice similarity between Mac OS-X and linux file systems? Did you know that these files are usually hidden in the regular Finder?
$ mkdir directoryName
creates a subdirectory called directoryName in the current working directory. You can only create subdirectories in a directory if you have write permission on that directory.
$ rmdir directoryName
removes the subdirectory directoryName from the current working
directory. You can only remove subdirectories if they are completely
empty (i.e. of all entries besides the '.' and '..' directories).
Excercises
$ cd ~ $ mkdir docs $ mkdir bin $ ls $ mkdir docs/junk $ ls -al docs $ rmdir docs/junk
cp is used to make copies of files or entire directories. To copy files, use:
$ cp source-file(s) destination
where source-file(s) and destination specify the source and destination of the copy respectively. The behaviour of cp depends on whether the destinationis a file or a directory. If the destination is a file, only one source file is allowed and cp makes a new file called destination that has the same contents as the source file. If the destination is a directory, many source files can be specified, each of which will be copied into the destination directory. We will discuss efficient specification of source files using wildcard characters.
To copy entire directories (including their contents), use a recursive copy:
$ cp -r source-directories destination-directory
Excercises
$ cd $ cd docs $ mkdir practice1 $ ls $ cp /etc/passwd practice1 $ cd practice1 $ ls $ cp /etc/group . $ ls $ cp /etc/passwd passwd.dup $ ls $ cp -r ../practice1 ../practice2What did the last command do?
$ cd ~/docs $ ls $ cp -r practice1 practice2What did the last command do? Explore with ls and cd to figure out.
mv is used to rename files/directories and/or move them from one directory into another. Exactly one source and one destination must be specified:
$ mv source destination
If destination is an existing directory, the new name for source (whether it be a file or a directory) will be destination/source. If source and destination are both files, source is renamed destination. N.B.: if destination is an existing file it will be destroyed and overwritten by source (you can use the -i option if you would like to be asked for confirmation before a file is overwritten in this way).
$ rm target-file(s)
removes the specified files. Unlike other operating systems, it is almost impossible to recover a deleted file unless you have a backup (there is no recycle bin!) so use this command with care.
Example:
rm file1 file3
If you would like to be asked before files are deleted, use the -i option:
$ rm -i myfile rm: remove 'myfile'?
rm can also be used to delete directories (along with all of their contents, including any subdirectories they contain). To do this, use the -r option. To avoid rm from asking any questions or giving errors (e.g. if the file doesn't exist) you used the -f (force) option. Extreme care needs to be taken when using this option - consider what would happen if a system administrator was trying to delete user will's home directory and accidentally typed:
$ rm -rf / home/will
(instead of rm -rf /home/will).
$ cat target-file(s)
displays the contents of target-file(s) on the screen, one after the other. You can also use it to create files from keyboard input as follows (> is the output redirection operator):
$ cat > hello.txt hello world! [ctrl-d] $ ls hello.txt hello.txt $ cat hello.txt hello world! $ cat hello.txt hello.txt > hello2.txt $ cat hello2.txt $ cat /etc/passwd
$ more target-file(s)
displays the contents of target-file(s) on the screen, pausing at the end of each screenful and asking the user to press a key (useful for long files). It also incorporates a searching facility (press '/' and then type a phrase that you want to look for).
You can also use more to break up the output of commands that produce more than one screenful of output as follows (| is the pipe operator, which will be discussed in the next chapter):
$ ls | more
less is just like more, except that has a few extra features (such as allowing users to scroll backwards and forwards through the displayed file). However less may not be present on all UNIX systems.
Excersises: Let's practice navigating in less.
$ less /etc/services
>
(push Esc once, then hold down
shift and push ``.'') Go to the end of the
document. (only with less)
<
Go to the beginning of the document. p
has the same effect. (only with less)
echo "Howdy" date cal cal 2006 hostname uname -a whoami who finger last ps -aux | lessexplore the output by scrolling around, and press q to quit)
history top(press q to quit from top)
The output of some commands are obvious. For commands, which you can't guess their functions, try to figure out by man pages. I don't expect that you can understand everything (this is just exercises getting accustomed to command line interface), but at least give it a try. Feel free to ask me if you want to know about the commands. Don't be afraid to experiment with other commands and options described in man pages. In unix system, regular user can't do much harm.
cd pwd cd . pwd ls -al cd .. pwd ls -al cd /etc ls -al |less cd ../tmp pwd ls mkdir username (Use your username here, e.g. naoki) rmdir username (Use your username here, e.g. naoki)
In addition to sending me the email (see ``Practice of less''), execute the following command at the end, so I can check how well you have done the homework.
history > /tmp/`whoami`
Note that the quotes around whoami are back-ticks(`), not single quotes (').
Try
ls /etc/host*
Multiple filenames can be specified using special pattern-matching characters. The rules are:
Exercises:
$ cd $ mkdir fn ; cd fnNote: Two commands can be connected with ``;''.
$ touch hello[1-3] hello12 jello[1357]touch makes empty files
$ ls $ ls -l hell* $ ls -l *llo1 $ ls -l hello?Note that this expression doesn't include hello12
$ ls -l [hj]*3