Вы находитесь на странице: 1из 4

Unix Operating System

Unix is a multi-tasking, multi-user operating system, which means that more than one user can use the system at the same time. It was developed in the early 1970's. Unix is a popular operating system for workstations, because it is powerful and flexible. It is less popular on personal computers, but with new versions of Unix, such as Linux, it is becoming more common. In UNIX, like DOS, files are grouped together in a directory structure. Each directory may contain several files and directories. A directory may contain other directories or files, but a file can only contain data. The file-system is arranged in a hierarchical, tree-like structure. The directories inside the directory structure are subdirectories. The top of the hierarchy is traditionally called "root". When you login to the system, you start in a predefined directory called your "home directory". For each user, his or her home directory is called the root. Users can not change which directory is their home directory.

Common Unix Commands


For the examples, assume the following directory structure:

Ctplab

Unix Notes

Mail

Sent Mail Backup


Assignment

Back2002

backupinfo.doc

project.doc

Absolute vs Relative Path Names


In command based operating systems such as DOS and Unix, it is very important to understand pathnames and the structure of the directory tree. In Unix, when you execute a command, the command will be performed on the current working directory, unless you specify another in the command. There are two ways to include the pathname in the command, either using the Absolute Pathname, or the Relative Pathname. Absolute Pathname The absolute pathname does not depend on your current working directory. It is written by starting at the root directory, down to the file or directory you wish to perform a command on. For example, using the tree above, if I wish to rename the file "project.txt" in the Assignment directory to project1.txt, using the absolute pathname, I would type the command: mv /home/ctplab/UnixNotes/Assignment/project.doc /home/ctplab/UnixNotes/Assignment/project1.doc It doesn't matter what my current directory is using the command above. I give the full pathname of the file, and the full pathname of the new file name. Relative Pathname In many cases it may be much easier to use the relative pathname of a file or directory in a command, because it can save typing long commands, and avoid unnecessary mistakes. The relative pathname is

path relative to the current directory. If my current directory is the UnixNotes directory, and I again wish to rename the project.doc file to project1.doc, using the relative path in the command I would type: mv Assignment/project.doc Assignment/project1.doc ~ Home Directory The tilde symbol (~) is a shortcut way of referring to your home directory. Say for example you are in the Back2002 directory. If you wanted to list what was in the mail directory you would have to type the long command: ls /home/ctplab/mail where /home/yourname is your home directory. Instead of typing this long command, you can use the tilde to say your home directory, and type the command: ls ~/mail.

Common Commands
pwd - Print Working Directory This command will print on the screen the fill pathname of your current directory. ls - List Command This command lists all files and directories in the current directory. Therefore if I type the ls command in my home directory, the result will be a listing of all files and directories in my home directory. *Note: the ls commands will only list files whose name does not begin with "." If a filename begins with a dot, it means it is a hidden file, and will not show up in the ls listing. ls -l - List Detail Command When you add a -l (lowercase l) to the ls command, this means long list. ls -l command will show a detailed list of files and directories in the current directory. mkdir - Make Directory Usage: mkdir <directory name> The mkdir command creates a new directory with the name <directoryname>. The new directory will be created in the current directory, unless you specify otherwise. Example: assume your current directory is /home/ctplab/UnixNotes/Backup, and you type the command: mkdir info This will create a new directory in the Backup directory. The full path name of the new directory is /home/ctplab/UnixNotes/Backup/info. But if you type the command: mkdir ~/info it will create a new directory in your home directory. The full path name of the new directory will be /home/ctplab/info cd - Change Directory Usage: cd <directory name> The cd command means you will change the current working directory to the one given. For example, cd UnixNotes will change the current directory to UnixNotes. If at this point you type ls, it will list the Backup and Assignment directories. The "." and ".." Directories When you do an ls on each directory in Unix you will see two directories listed, "." and ".." directories. "." This means your current directory. The command cd . means stay in the current directory.

".." This means the parent directory of the current directory. The parent directory is the directory above the current directory. If your current directory is UnixNotes, cd .. would change the current directory to the Home/ctplab directory. cp - Copy File Usage: cp <source> <destination> The cp command makes a copy of the file named <source> and saves the copy with the name <destination>. Note the names of the source and destination may include the full path name, if your source (fileto be copied) or destination (fileto copy to) are not in the current directory. The cp command is for files only. If you wish to copy a directory using the cp command, you must include the recursive option (cp -r) Example: If you enter the command: cp /home/ctplab/UnixNotes/backupinfo.doc /home/ctplab/UnixNotes/Assignment/assign.doc This command will make a copy of the file backupinfo.doc in the Backup directory, and save the copy in the Assignment directory with the name assign.doc. cp -r Recursive Copy Directory This command is used when you are copying a directory. The -r is the recursive flag which tells Unix to copy the directory and everything inside of it to the new directory. mv - Move/Rename File or Directory Usage: mv <source> <destination> This command moves the file or directory named <source> to <destination>. It also the command which is used to rename a file or directory. Example: Assume your current working directory is /home/ctplab/UnixNotes/Backup. If you enter the command: mv backupinfo.doc backinfo.doc, it will rename the file backupinfo.doc to backinfo.doc. Once the command is executed, there will be only one file in the directory Backup, with the name backinfo.doc. To move a directory, if you type the command: mv /home/ctplab/UnixNotes/Assignment /home/ctplab/UnixNotes/Backup, you move the entire directory (including all the files and directories it contains) into the Backup directory. rmdir - Remove Directory Usage: rmdir <directory name> The rmdir command removes/deletes a directory with the name <directory name>. To delete a directory, you must first delete all directories and files inside the directory you want to delete. rm - Remove/Delete a File Usage: rm <name> This command is used to delete one or more files, were <name> is the name of the file(s) to be deleted. You can use the wildcard (*) to delete more than one file. For example rm *.doc would remove all files in the current directory whose extension was .doc rm -r Remove/Delete a Directory and its contents The rm command can also be used to delete directories and everything that they contain. To delete a directory and it's contents, the -r flag is added to the rm command, rm -r. The -r option means recursive, and will delete the directory and everything it contains.

Exercises
1. Create the following directory structure in your home directory. Note code

courses ctp els

personal

VB

java projects assign Objects.doc

web

other

termp

1. Copy the projects directory under the courses directory. cp r ~/Note/code/java/projects ~/Note/courses 2. Use the OpenOffice Writer to create a file, lab.doc under the directory ctp. 3. Copy the file lab.doc to the root directory and rename it to lab3.doc cp ~/Note/courses/ctp/lab.doc ~/lab3.doc 4. Move the other directory under code. mv ~/Note/personal/other ~/Note/code 5. Delete the Java directory along with its subdirectories. rm r ~/Note/code/Java 6. Rename the code directory to script. mv ~/Note/code ~/Note/script 7. Display a detailed list of the contents of courses directory. ls l ~/Note/courses 8. Current directory: ~/Note/personal/web mkdir ../html 9. Current directory: ~/Note/courses/els cp ../ctp/lab.doc . 10. Current directory: ~/Note/personal cp r html web

Вам также может понравиться