Sie sind auf Seite 1von 6

UNIX FILE AND DIRECTORY COMMANDS

Academic Technology Support California State University, Los Angeles


May 2000
JC UNIX2

(v1.0)

Unix is the operating system on the Sun computers used by faculty and students on this campus. This handout describes some basic Unix commands for manipulating files and directories. More information on the Unix environment and other basic commands are available in the BEGINNER'S SET OF UNIX COMMANDS handout.

Directory
(A.K.A. Folders) For Macintosh and Windows users, the analogous concept for the File Folder or the Folder is called the directory on the Unix systems. Directories on Unix, like their counterpart on the desktop systems, are for collecting together files and subdirectories for better organization.

Location
Your home directory was created for you when you received your NIS account. You can think of this as your main directory, where all of your data and files are to be stored. Normally, the home directory is also the location, or the working directory, where you start from when you log on to a Unix machine. Within your home directory, though, you can create sub-directories, and then, subsub-directories within your sub-directories, and so forth. Of course, your home directory itself is located within some other directory on your server machine. Moving about within a large, multi-level hierarchy like this, you would need to verify where you are from time to time. pwd Example:
uranus% pwd /c/bus/uliu6 uranus% cd mail uranus% pwd /c/bus/uliu6/mail uranus% (An example of someones home directorys location.) (Change the current working directory to mail.) (Where user uliu6s mail directory is located.)

Reports where the (current) working directory is located at.

________________________________ Unix File and Directory Commands 1

Viewing a Directorys Content


To view the contents of a directory, use the command ls. Like most commands on Unix, there are many options available to suit the needs of individual users, and a few of them are listed below. ls ls dir_name ls -a ls -l ls -F Lists the contents of the current directory in a compact format. Lists the contents of the directory dir_name. Lists the all contents of the current directoryincluding the hidden filesin a compact format. Lists the contents of the current directory, with full details on each item. (File size, permissions, item type, etc.) Lists the contents of the current directory in a compact format, but adding a trailing / character to identify all the subdirectories.

Examples:
ls ~/ ls -al ~/mail ls -F -a

(Lists the contents of the home directory) (Lists all contents of the directory mail located within the home directory, and displays the full details.) (Lists all the contents of the current directory, with trailing / for all the directories.)

Changing Directory
The command cd is used to change the current working directory. (For instance, to go from the home directory into a sub-directory.) cd dir_name cd .. cd Goes into the named directory. (i.e. Sets current working directory to the named directory.) Goes into the parent directory. (i.e. the directory where the current directory is located within.) Goes back to your home directory. (i.e. Sets the current working directory to the home directory.)

Creating Directory
To better organize your files, it is often helpful to create sub-directories within your home directory. For example, you can create a directory called Eng101 to store all your English 101 essays, and another directory called Anth250 to store all your Anthropology 250 papers. To create a directory, the command is mkdir. Keep in mind that the Unix file system is case sensitive; Soc101 and soc101 are treated as two different names, and so you can create two directories with those names, even though you probably meant to have just one of them. To make things easier for yourself, do not use any punctuation marks or spaces for your directory name, except the underscore _ symbol.

________________________________ Unix File and Directory Commands 2

mkdir dir_name Example:


mkdir Eng102

Create a (sub)directory with the specified name.

(This will create a directory named Eng102.)

Deleting Directories
When a directory is not needed anymore, you can remove it to reduce clutter. There are two different ways of doing this: rm -r and rmdir. Delete only items you no longer need; you will not be able to retrieve them once they are erased! rmdir dir_name rm -r dir_name Deletes the named directory, if it is empty. Deletes the named directory and all of its contents.

Files (In Directories)


Files on Unix are not all that different from those on Windows or Macintosh. For organizing files on Unix, you can move, delete, duplicate, rename, and have a quick view of the contents of (some of) your files. Many file operations are also applicable to the directory, which helps to keep down the number of commands to remember.

Moving Files
To move a file from the current directory to another, use the mv command. mv file_name dest_dir mv dir1 dir2 Moves the named file into the specified directory. Moves the directory dir1 into the directory dir2.

Copying Files
To duplicate a file in another directory, or to make a copy under a different name, use the cp command. cp file_name1 file_name2 cp file_name dir_name cp -r dir_name1 dir_name2 Makes a duplicate of the file file_name1, under the name file_name2. Duplicates the file file_name, and places it inside the directory dir_name. Duplicates the directory dir_name1, and places the duplicate in dir_name2. Or, if dir_name2 doesnt exist, the duplicate would be named dir_name2.

________________________________ Unix File and Directory Commands 3

Renaming Files
To change a file name, use the command mv. (Note: Since its the same command for moving files, if youre not careful and specify an existing directory name instead of a new file name, you will end up moving the file into the directory instead of renaming it.) mv file_name1 file_name2 mv dir_name1 dir_name2 Renames the file file_name1 to file_name2. Renames the directory dir_name1 to dir_name2.

Deleting File
Deleting files is similar to deleting directories. The command is rm. Files erased typically cannot be recovered, so be careful. rm file_name Deletes the file named file_name.

Viewing Contents of File


In Unix, if you are working with text (ASCII) files, you can quickly view their contents using the commands more and less.

more file_name less file_name

Displays the content of the file file_name, one screen full at a time. Displays the content of the file file_name, one screen full at a time. (Note: not all users have direct access to this command by default.)

When using either command, you will need to press the space bar to advance from one screenful of text to the next. Press B to go back to the previous screen. With the less command, you will also need to press Q to quit viewing the file, whereas more will quit automatically when you reach the end of the file.

Opening and Editing Text Files


Different file types, such as word processing, spreadsheet, etc., are associated with specific application programs. Normally, to open a file of a specific type, the application that created it is required. However, for text (i.e. ASCII) files, most Unix systems have multiple text editors installed, and any one of them can get the job done. Think of the text editors as word processors, but without different fonts and styles or other similar functionalities. The most popular text editor on Unix is probably vi, which is versatile, powerful, and probably somewhat difficult for most beginners. Another popular editor is emacs, which is more powerful and more complicated, and even has its own programming language. The handout INTRODUCTION TO THE VI EDITOR is available for anyone who
________________________________ Unix File and Directory Commands 4

is interested in learning more about vi. Those who want learn emacs are advised to check out online information, or browse through bookstores computer sections for references. For simple tasks, though, you can use pico instead. When running, all of picos commands are listed on the bottom of the screen. Each command is a combination of the Control key (often abbreviated as Ctrl on the keyboard) and a letter key. For example, to exit from pico and save, you would press Control X. (In picos command menu, the ^ symbol is a notation for the meta key, which nowadays is normally mapped to the Control keyit does not mean press Shift 6!)

pico file_name

Activates pico, and has it open the file file_name, for editing. It will let you create the named file if it doesnt exist yet.

Forwarding Mail
Unix is a very flexible operating system, with many configuration possibilities for the administrator and end users alike. Since most of those options are specified via text files, they can be edited via the text editors introduced in the last section. For example, should you find checking both your CSULA e-mail (via either Webmail) and your own ISPs e-mail to be tedious, you can have your CSULA mail forwarded to your ISP account (or Hotmail, etc.). This can be set by using the hidden file .forward to specify the forwarding address. To set up mail forwarding, you should first telnet to your server (named after some celestial objects, such as jupiter, sol, etc.). Then, type the command ls -aF to show all the items, including files which start with a period. Those files, not reported by ls without the -a option, are commonly called hidden files. (Users with lots of items in their home directories may need to use ls -aF | more instead.) Mail forwarding is controlled by such a file, called .forward, which you can create in your home directory. (It normally does not exist, unless you are already forwarding your mail.) Now, to set up the forwarding, just use pico (or any other text editor):
pico .forward

In pico, type out the address for the mail to be forwarded to, then exit and save. You're done! Just for fun, heres a quicker way of doing this, without using a text editor:
echo "username@hostname" > .forward

To stop mail forwarding, just erase the file:


rm .forward

________________________________ Unix File and Directory Commands 5

Command Summary Chart


To do View Content File Operation
more file_name less file_name

Directory Operation
ls ls -a ls -F ls -l
(to list the directorys content) (to list the hidden files as well) (to identify the sub-directories with a /) (to list all the information) (to list the named dir.s content)

ls dir_name

Create Delete Move Copying

pico file_name (also to edit) rm file_name mv file_name dest_dir cp file_name1 file_name2 cp file_name dir_name

mkdir dir_name rmdir dir_name rm -r dir_name mv dir_name dest_dir cp -r dir_name1 dir_name2 mv dir_name new_name pwd cd dir_name cd .. (to go to the parent directory) cd (to go to the home directory)

mv file_name new_name Rename n/a Current Location n/a Change Directory

________________________________ Unix File and Directory Commands 6

Das könnte Ihnen auch gefallen