| UNIX Tutorial | 4. Managing files | ||||||||||||||
|
Commands covered in this section: mkdir, rmdir, cp, mv, rm,chmod Now that you have a basic understanding of the filesystem and how to navigate it, you can begin creating and managing your own files and directories. Let's begin with directories. You create directories with mkdir and remove them with rmdir. Exercise 4.1
Create two new directories with the mkdir command (use the ls command to confirm the new directories were created).rmdir will only remove empty directories. If a directory contains files, you must remove them first before using rmdir. Alternatively, you can recursively delete the directory and its contents using the appropriate option with the rm command as discussed later in this section. Let's move on to working with files. First, you'll need to copy some existing files from elsewhere in the filesystem into your test directory. You use the cp command to do this. Exercise 4.2 Before going on, move into the new "test" directory you just created. You will use this area to experiment (thus minimizing any potential damage to other files in your home directory!).Since cp, mv, and rm have the potential to destroy data, they offer an interactive option which prompts you before proceeding. This is invoked with the "-i" option. Your system administrator may have already configured your account to use this option by default. Both cp and mv require two arguments: the existing location and the destination of the file to be copied or moved. The destination can be another file or a directory. Let's compare the results in each case. Exercise 4.3 First, let's make a new directory within the test directory.The shell allows the use of wildcards to reference more than one file at a time. The "?" character stands for a single character with any value and the "*" character stands for any number of characters with any value. For example, "host*" would match "hosts" and "hosts2" in the above example. Exercise 4.4 Make yet another directory.You can recursively copy a directory and its contents to another location. It is also possible to delete a directory and its contents. These can be accomplished by using the "-r" option with cp and rm, respectively. Exercise 4.5 Copy the "junk" directory and its contents to a new location.Be careful with rm -r; using this command in the wrong place (e.g., your home directory) can have disastrous consquences! UNIX provides permissions (also known as modes) to restrict access to your files and directories. These are manipulated with the chmod ("change modes") command. Recall that ls -l shows a long listing of files and directories. The first column in this long listing is a 10-character string indicating the type of file (regular file, directory, link, etc.) and the access permissions for the file's owner, group, and other users. This is broken down in Table 4.1. Table 4.1 The first character identifies the file type. The next three characters indicate access rights for the file's owner, the following three show access rights for the group, and the final three show access rights for all other users. A hyphen in a given access right's position means that right is denied. The following example shows a directory for which the owner has read/write/execute access, the group has read/execute access, and all others have no access:
drwxr-x---
The chmod command uses a special argument (composed of three parts as summarized in Table 4.2) to assign the desired rights to files or directories. Table 4.2 By putting together the entity, operator, and access rights, you can construct an argument for chmod which will assign the desired rights. In the following example, the user will have read/write access, the group will have read access, and others will have no access:
u=rw-,g=r--,o=---
Exercise 4.6
Add all rights for owner and revoke all rights for group and others on the "hosts" file. |
|||||||||||||||
|
< previous | table of contents | next > |
|||||||||||||||