Common Commands for Files and Directories


This section discusses the basic UNIX file system commands. If you are working in a graphical environment, you will have to open a terminal window to enter these commands.

Listing the Contents of a Directory

Assume that you are in the directory called /home/raf from the example in Figure 3–1.

To see all the files in this directory, you enter the ls (list) command:

 $ ls Email notes.august Work

The ls command lists the contents of the current directory on your screen in multiple columns. (The precise behavior of the ls command varies in different releases of UNIX. For example, on most systems, it will list filenames in alphabetical order, but on some it will list all filenames that start with an uppercase character before listing filenames in lowercase. This is sometimes called ASCII order.) Notice that ls without arguments simply lists the contents by name. It does not tell you whether the names refer to files or directories.

If you want to view the contents of a subdirectory of your current directory, you can issue the ls command with an argument that is the name of the subdirectory For example,

 $ ls Email inbox   save   sent

If the object (file or directory) does not exist, ls gives you an error message, such as

 $ ls Emial Emial not found

You can see whether a file exists by supplying the pathname of the file, in relation to your current directory, as the argument to ls. If the file does exist, it will echo the name back to you.

 $ ls Email/save Email/save

Listing Directory Contents with Marks

When you use the ls command, you do not know whether a name refers to an ordinary file, a program that you can run, or a directory Running the ls command with the -F option produces a list in which the names are marked with symbols that indicate the kind of file that each name refers to.

Names of directories are listed with / (a slash) following their names. Executable files (those that can be run as programs) are listed with * (an asterisk) following their names. Symbolic links are listed with @ (an “at” sign) following their names. For instance, suppose that you run ls with the -F option to list the contents of a directory, producing the following result:

 $ ls -F Email/ notes Projects@

This example shows that the directory contains the ordinary file notes, the directory Email, and a symbolic link Projects. Another way to get information about file types and contents is with the file command, described later in this chapter.

Listing Files in the Current Directory Tree

You can add the -R (recursive) option to the ls command to list all the files in your current directory, along with all the files in each of its subdirectories, and so on. For example,

 $ ls -R Email     notes.august     Work ./Email inbox     save     sent ./Work final save

shows the contents of the current directory as well as the contents of its subdirectories Email and Work.

Viewing Files

The simplest and most basic way to view a file is with the cat command. cat (short for concatenate) takes any files you specify and displays them on the screen. For example, you could use cat to display on your screen the contents of the file review:

 $ cat review I recommend publication of this article. It provides a good overview of the topic and is appropriate for the lead article of this issue.

The cat command shows you everything in the file but nothing else: no header, title, file-name, size, or other information.

Viewing Files with Special Characters

The cat command recognizes eight-bit characters. In earlier versions of UNIX, it only recognized seven-bit characters. This enhancement permits cat to display characters from extended character sets, such as the kanji characters used to represent Japanese words.

If you try to display a binary file, such as an executable program, the output to your screen will usually be a mess. To better view files that contain nonprinting ASCII characters, you can use the -v option. For example, if the file output contains a line that includes the ASCII BEL character (CTRL-G), the command cat output will cause the computer to beep. Using the -v option, however, will replace the BEL character with the symbol ^G, as shown.

 $ cat -v output The ASCII control character ^G (007) will ring a bell ^G^G^G^G on the user's terminal. $

Directing the Output of cat

You can send the output of cat to a file as well as to the screen. For instance,

 $ cat physics > physics.backup

copies the contents of physics to physics.backup, instead of displaying the contents on the screen. The > provides a general way to redirect the output of a command to a file. This is explained in detail in the section “Standard Input and Output” of Chapter 4.

In the preceding example, if there is no file named physics.backup in the current directory, the system creates one. If a file with that name already exists, the output of cat overwrites it-its original contents are replaced. (Note that this can be prevented by using the noclobber features of some shells.) Sometimes this is what you want, but sometimes you want to add information from one file to the end of another. In order to add information to the end of a file, do the following:

 $ cat notes.august >> notes

The >> in the preceding example appends the contents of the file named notes.august to the end of the file named notes, without making any other changes to notes. It’s okay if notes does not exist; the system will create it if necessary The capability to append output to an existing file is another form of file redirection. Like simple redirection, it works with almost all commands, not just cat.

Combining Files and Using Wildcards

You can use cat to combine a number of files into one. For example, consider a directory that contains material being used in writing a chapter, as follows:

 $ ls Chapter1     macros      section2 chapter.1    names       section3 chapter.2    section1    sed_info

You can combine all of the sections into a chapter with cat:

 $ cat section1 section2 section3 > chapter.3

This copies each of the files, section1, section2, and section3, in order into the new file chapter.3. This can be described as concatenating the files into one, hence the name cat.

To make commands like this easier, the shell provides a wildcard symbol, * (asterisk), that allows you to specify a number of files with similar names. An asterisk by itself matches all filenames in the current directory When the * is part of a word, it stands for or matches any string of characters. For example, the pattern section* matches any file whose name begins with section. So the command

 $ cat section* > chapter.3

would have had the same effect as the command in the preceding example.

When you use the wildcard symbol * as part of a filename in a command, that pattern is replaced by the names of all files in the current directory that match the pattern, listed in alphabetical order. In the preceding example, section* matches section1, section2, and section3, and so would sect*. But se* would also match the file sed_info. A* (star, or asterisk) by itself matches all filenames in the current directory

When using wildcards, you can use ls to make sure that the wildcard pattern matches the files you want. For example,

 $ ls se* section1    section2    section3    sed_info

indicates that it would be a mistake to use se* unless you want to include sed_info.

You can also use * to simplify typing commands, even when you are not using it to match more than one file. The command

 $ cat *1 *2 > temp

is a lot easier to type than

 $ cat section1 section2 > temp

Chapter 4 describes the use of * and other wildcard characters in detail.

Note that there is an important difference between the UNIX System’s use of * and the similar use of it in Windows. In Windows, * does not match a . (dot), so section* would match section1 but not match section.txt. In Windows, you would have to use the pattern section*.* to match every file beginning with section.

Creating a File

So far, all the examples you have seen involved using cat to copy one or more normal files, either to another file or to your screen. But other possibilities exist. Just as your screen is the default output for cat and other commands, your keyboard is the default input. If you do not specify a file to use as input, cat will simply copy everything you type to its output. This provides a way to create simple files without using an editor. The command

 $ cat > names Nate   nate@engineer.com Rebecca   rlf@library.edu CTRL-D

sends everything you type to the file names. It sends the text one line at a time, after you hit ENTER. You can use BACKSPACE to correct your typing on the current line, but you cannot back up across lines. When you are finished typing, you must type CTRL-D (hold down the Ctrl key and press d) on a line by itself. This terminates cat and closes the file names. (CTRL-D is the end of file [EOF] mark in the UNIX System.)

Using cat in this way (cat > names) creates the file names if it does not already exist and overwrites (replaces) its contents if it does exist. You can use cat to add material to a file as well. For example,

 $ cat >> names Dan   dkraut@bio.ca.edu CTRL-D

will take everything you type at the keyboard and append it at the end of the file names. Again, you need to end by typing CTRL-D alone on a line.

Another command, touch, can also be used to create a file.

 $ touch notes

will create an empty file called notes, if that file does not already exist. Unlike cat, touch can also be used to change the creation time or last accessed time of an existing file. This is discussed in further detail in Chapter 19.

Moving Around in Directories

Since many UNIX commands operate on the current directory it is useful to know what your current directory is. The command pwd (present working directory) tells you which directory you are currently in. For example,

 $ pwd /home/raf

tells you that the current directory is /home/raf.

You can move between directories by using the cd (change directory) command. If you are in your home directory, /home/raf, and wish to change to the subdirectory Work, type

 $ cd Work

If you know where certain information is kept in a UNIX system, you can move directly there by specifying the full pathname of that directory:

 $ cd /home/raf/Email $ pwd /home/raf/Email $ ls inbox      save      sent

You can also change to a directory by using its relative pathname. Since .. (dot-dot) refers to the parent directory (the one above the current directory in the tree),

 $ cd . . $ pwd /home/raf

moves you to that directory To go a step further,

 $ cd ../.. $ pwd  /

changes directories to the parent of the parent of the current directory, or in our example, to the / (root) directory

Moving to Your Home Directory

If you issue cd by itself, you will be moved to your home directory, the directory in which you are placed when you log in. This is an especially effective use of shorthand if you are somewhere deep in the file system. For instance, you can use the following sequence of commands to list the contents of your home directory when you are in the directory /home/dkmut/iuork/cs106x/proj1/lib/Source:

 $ pwd /home/dkraut/work/cs106x/proj1/lib/Source $ cd $ pwd /home/raf $ ls Email     notes.august     Work

In the preceding example, the first pwd command shows that you are nested seven layers below the root directory The cd command moves you to your home directory as confirmed by the pwd command, which shows that the current working directory is /home/raf. The ls command shows the contents of that directory Since ~ is a shortcut that refers to your home directory,

 $ cd ~

does exactly the same thing.

Returning to Your Previous Directory

The command cd-will return you to your previous directory For example, after moving to your home directory as shown above, you can return to the directory Source with:

 $ cd - $ pwd /home/dkraut/work/cs106x/proj1/lib/Source

Like the ~ shortcut for your home directory, using cd-to return to the previous directory is a feature of the shell. Some shells also have the commands pushd and popd to allow you to save, and later return to, your current directory

Moving and Renaming Files and Directories

To keep your file system organized, you will need to move and rename files. You move a file from one directory to another with mv (from move). For example, the following moves the file names from the current directory to the directory /home/jmf/Info:

 $ mv names /home/jmf/Info

If you use ls to check, it confirms that a file with that name is now in Info:

 $ ls /home/jmf/Info/names /home/jmf/Info/names

You can move several files at once to the same destination directory by first naming all of the files to be moved, and giving the name of the destination last. For example, the following command moves three files to the subdirectory called TermPaper:

 $ mv section1 section2 section3 TermPaper

Of course you could make this easier by using the wildcard symbol, *. If the three files in the preceding example are the only files in the current directory with names beginning with sec, the following command has the same effect:

 $ mv sec* TermPaper

UNIX has no separate command for renaming a file. Renaming is just part of moving. To rename a file in the current directory, you use mv, but with the new filename as the destination. For example, the following renames overview to intro:

 $ mv overview intro

You can rename a file when you move it to a new directory by including the new filename as part of the destination. The following command puts notes in the directory Music and gives it the new name notes4:

 $ mv notes Music/notes4

Compare with this, which moves notes to Research but keeps the old name, notes:

 $ mv notes Music

To summarize, when you use mv, you first name the file or files to be moved, and then the destination. The destination can be a directory name, in which case the file is simply moved, or it can be a filename, in which case the file is renamed. The destination can be a full pathname, or a name relative to the current directory-for example, one of its subdirectories.

Moving files is very fast in UNIX. The actual contents of a file are not moved; you’re really only moving an entry in a table that tells the system what directory the data is in. So the size of the file being moved has no bearing on the time taken by the mv command.

Avoiding Mistakes with mv

When using mv, you should watch out for a few common mistakes. If you make a mistake in typing when you specify a destination directory, you may end up renaming the file in the current directory For example, suppose you meant to move a file to Info but made a mistake in typing.

 $ mv names Ifno

In this case, you end up with a new file named Ifno in the current directory A similar mistake can happen if you try to move a file to a directory that does not exist. Again, the file will be renamed instead.

When you move a file to a new directory, it is a good idea to check first to make sure the directory does not already contain a file with that name. If it does, mv will simply overwrite it with the new file. The same thing will happen if you try to rename a file using a filename that already exists.

Newer versions of UNIX provide an option to the mv command that helps prevent accidentally overwriting files. The -i (interactive) option causes mv to inform you when a move would overwrite an existing file. It displays the filename followed by a question mark. If you want to continue the move, type y. Any other entry (including n) stops that move. The following shows what happens if you try to use mv -i to rename the file totals to data when the data file already exists:

 $ mv -i totals data mv:   overwrite data?

In Chapter 4, you will see how to use an alias to change the mv command so that it always uses the -i option, if you choose. This can be very helpful for new users.

Moving Directories

Another feature not present in earlier versions of UNIX is the capability to use mv to move directories. You can use a single mv command to move a directory and all of its files and subdirectories just as you’d use it to move a single file. For example, if the directory Final contains all of your finished work on a document, you can move it to a directory in which you keep all of the versions of that document, Project, as shown here:

 $ ls Project Drafts $ mv Final Project $ ls Project Drafts Final

Copying Files

The cp command is similar to mv, except that it copies files rather than moving or renaming them. cp follows the same model as mv: you name the files to be copied first and then give the destination. The destination can be a directory, a pathname for a file, or a new file in the current directory. The following command makes a backup copy of seattle and names the copy seattle.bk:

 $ cp seattle seattle.bk

After you use this cp command, there are two separate copies of that file in the same directory The original is unchanged, and the contents of the copied file are identical to the original. The files are not linked in any way, so if you edit one of the files, the other will not change.

To create a copy of a file with the same name as the original but in a new directory, just use the directory name as the destination, as shown here:

 $ cp seattle Backups

Note that if the destination directory already contains a file named seattle, the copy will overwrite it.

If you invoke cp with the -i (interactive) option, it will warn you before overwriting an existing file. For example, if there is already a file named data.2 in the current directory, cp warns you that it will be overwritten and asks if you want to go ahead:

 $ cp -i data data.2 cp: overwrite data.2?

To go ahead and overwrite it, type y. Any other response, including n or ENTER, leaves the file as it was. Chapter 4 shows how to use an alias to replace cp with cp -i, if you choose.

Copying the Contents of a Directory

So far the discussion has assumed that you are copying an ordinary file to another file. If you try to copy a directory, you will get an error message. A feature of cp (found on most versions of UNIX) is the -r (recursive) option that lets you copy an entire directory structure. Suppose you have a directory called Project, and you wish to make a backup copy The following command creates a new directory, called Project.Backup, and copies all of the files and subdirectories in Project to the new directory:

 $ cp -r Project Project.Backup

Linking Files

When you copy a file, you create another file with the same contents as the original. Each copy takes up additional space in the file system, and each can be modified independently of the others.

As you recall, a link is a way to share a file with another user without actually copying it on the disk. An example where this might be useful is a list of names and contact information that two or more people use, and that any of the users can add to or edit. Each user needs access to a common version of the file in a convenient place in each user’s own directory system.

Hard Links

The ln command creates a link between files, which enables you to make a single file accessible at two or more locations in the directory system. The following links the file project.main in dkraut’s home directory with a new file of the same name in the current directory:

 $ ln /home/dkraut/project.main project.main

Using ln in this way creates a hard link to the file in /home/dkraut, but there is still only one file. Now if you add a new line of information to your linked copy of project.main, the line also appears in the file in dkraut’s directory, since this is really the same file.

Any changes to the contents of a linked file affect all the links. If you overwrite (or clobber) the information in your file, the information in dkraut’s copy is overwritten too. (For a description of a way to prevent clobbering of files like this, see the noclobber options to the C and Korn shells, which are described in Chapter 4.)

You can remove one of a set of hard-linked files with the rm command without affecting the others. For example, if you remove your hard-linked copy of project.main, dkraut’s copy is unchanged.

To see if two files are hard linked to each other, use the command ls -i. This will display the inode number of each file. If two files have the same inode number, then they are really the same file. For example,

 $ ls -i  /usr/bin/gcc 344135  /usr/bin/gcc $ ls -i  /usr/bin/cc 344135  /usr/bin/cc

shows that /usr/bin/gcc and /usr/bin/cc are hard linked. (Note: although you cannot hard link files across file systems, sometimes two files on different file systems will display the same inode number. This does not mean that they are linked. See Chapter 14 for a discussion of file systems.)

Symbolic Links

Symbolic links are created by using the ln command with the -s (symbolic) option. The following example shows how you could use ln to link a file in the /var file system to an entry in one of your directories within the /home file system:

 $ ln -s /var/X/docs/readme temp/x.readme

This will create a symbolic link called x.readme in the temp directory

The second argument to ln is optional; if you do not specify the name of the new file, it will create a symbolic link with the same name as the target file. So, for example

 $ ln -s /usr/bin/firefox/firefox

will create a file called firefox in the current directory that is a symbolic link to /usr/bin/firefox/firefox.

Symbolic links also enable you to link directories. The command

 $ ln -s /home/dkraut/work/cs106x/proj1/lib/Source Project

will create a directory, called Project, that is a link to the directory /homedkraut/work/cs106x/ proj1/lib/Source. This is useful for directories with long pathnames that you need to access often.

Removing Files

To get rid of files you no longer want or need, use the rm (remove) command. rm deletes the named files from the file system, as shown in the following example:

 $ ls notes     research     temp $ rm temp $ ls notes     research

The ls command shows that after you use rm to delete temp, the file is no longer there.

Removing Multiple Files

The rm command accepts several arguments and takes several options. If you specify more than one filename, it removes all of the files you named. The following command removes the two files left in the directory:

 $ rm notes research $ ls $

Remember that you can remove several files with similar names by using wildcard characters to specify them with a single pattern. The following will remove all files in the current directory:

 $ rm *

Caution 

Do not use rm* unless you really mean to delete every file in your current directory.

Similarly, if you use a common suffix to help group files dealing with a single topic, for example .rlf to identify notes to user rlf, you can delete all of them at once with the following command:

 $ rm *.rlf

Safely Removing Files

Almost every user has accidentally deleted files. In the preceding example, if you accidentally hit the SPACEBAR between the * and the extension and type

 $ rm * .rlf

you will delete all of the files in the current directory As typed, this command says to remove all files (*), and then remove a file named .rlf.

To avoid accidentally removing files, use rm with the -i (interactive) option. When you use this option, rm prints the name of each file and waits for your response before deleting it. To go ahead and delete the file, type y. Responding n or hitting ENTER will keep the file rather than deleting it. For example, in a directory that contains the files notes, research, and temp, the interactive option to rm gives you the following:

 $ rm -i * notes: y research: <ENTER> temp: y

Your responses cause rm to delete both notes and temp, but not research.

New users may find it very helpful to change the rm command to always use the -i option. This can be done with an alias. Chapter 4 describes how to add this alias to your configuration files.

Restoring Files

When you remove a file using the rm command, it is gone. If you make a mistake, you can only hope that the file is available somewhere on a backup file system (on a tape or disk).

You can call your system administrator and ask to have the file you removed, say /home/you/Work/temp, restored from backup. If it has been saved, it can be restored for you. Systems differ widely in how, and how often, they are backed up. On a heavily supported system, all files are copied to a backup system every day and saved for some number of days, weeks, or months. On some systems, backups are done less frequently, perhaps weekly On personal workstations, backups occur when you get around to doing them. In any case, you will have lost all changes made since the last backup. (Backing up and restoring are discussed in Chapter 14.) You cannot, as a user, restore a file by attempting to recover pieces of the file left stored on disk.

Creating a Directory

You can create new directories in your file system with the mkdir (make directory) command. It is used as follows:

 $ pwd Work $ ls notes    research temp $ mkdir New $ ls notes    New research    temp

In this example, you are in the Work directory, which contains the files notes, research, and temp, and you use mkdir to create a new directory (called New) within Work.

Removing a Directory

There are two ways to remove or delete a directory If the directory is empty (it contains no files or subdirectories), you can use the rmdir (remove directory) command. If you try to use rmdir on a directory that is not empty, you’ll get an error message. The following removes the directory New added in the preceding example:

 $ rmdir New

To remove a directory that is not empty, together with all of the files and subdirectories it contains, use rm with the -r (recursive) option, as shown here:

 $ rm -r Work

The -r option instructs rm to delete all of the files it finds in Work and then go to each of the subdirectories and delete all of their files, and so forth, concluding by deleting Work itself.

Since rm -r removes all of the contents of a directory, be very careful in using it. You can add the -i option to step through all the files and directories, removing or leaving them one at a time.

 $ rm -ir Work rm:  descend into directory 'Work'? y rm:  remove regular empty file 'Work/final'? y rm:  remove regular empty file 'Work/save'? <RETURN> $ ls Work save

In this example, the file final is deleted, but because save is not, the directory Work is not deleted, either.

Notice, by the way, that when a command is being run with two or more options (in this case, -r and -i), the options can be combined (in this case, -ir). This is optional; the command rm -i -r could have been used instead in the preceding example. The order of the options does not matter (rm -ir is the same as rm -ri). This applies to most UNIX commands, not just rm.

Getting Information About File Types

Sometimes you just want to know what kind of information a file contains. For example, you may decide to put all your shell scripts together in one directory You know that several scripts are scattered about in several directories, but you don’t know their names, or you aren’t sure you remember all of them. Or you may want to print all of the text files in the current directory, whatever their content.

You can use several of the commands already discussed to get limited information about file contents. For example, ls -l shows you if a file is executable-either a compiled program or a shell script (batch file). But the most complete and most useful command for getting information about the type of information contained in files is file.

file reports the type of information contained in each of the files you give it. The following shows typical output from using file on all of the files in the current directory:

 $ file * Backup:        directory cx:            commands text draft3:        ascii text fields:        ascii text linkfile:      symbolic link to dirlink mmxtest:       [nt] roff, tbl, or eqn input text pq:            executable send:          English text tag:           data

You can use file to check on the type of information contained in a file before you print it. The preceding example tells you that you should use the troff formatter before printing mmxtest, and that you should not try to print pq, since it is an executable program, not a text file.

To determine the contents of a file, file reads information from the file header and compares it to entries in the file /etc/magic. This can be used to identify a number of basic file types-for example, whether the file is a compiled program. For text files, it also examines the first 512 bytes to try to make finer distinctions-for example, among formatter source files, C program source files, and shell scripts. Once in a while this detailed classification of text files can be incorrect, although basic distinctions between text and data are reliable.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net