Читать книгу Linux Bible - Christopher Negus - Страница 124
Using Basic Filesystem Commands
ОглавлениеI want to introduce you to a few simple commands for getting around the filesystem to start out. If you want to follow along, log in and open a shell. When you log in to a Linux system and open a shell, you are placed in your home directory. As a Linux user, most of the files you save and work with will probably be in that directory or in subdirectories that you create. Table 4.1 shows commands to create and use files and directories.
TABLE 4.1 Commands to Create and Use Files
Command | Result |
cd | Changes to another directory |
pwd | Prints the name of the current (or present) working directory |
mkdir | Creates a directory |
chmod | Changes the permission on a file or directory |
ls | Lists the contents of a directory |
One of the most basic commands that you use from the shell is cd
. The cd
command can be used with no options (to take you to your home directory) or with full or relative paths. Consider the following commands:
$ cd /usr/share/ $ pwd /usr/share $ cd doc $ pwd /usr/share/doc $ cd $ pwd /home/chris
The /usr/share
option represents the absolute path to a directory on the system. Because it begins with a slash (/
), this path tells the shell to start at the root of the filesystem and take you to the share
directory that exists in the usr
directory. The doc
option to the cd
command looks for a directory called doc
that is relative to the current directory. So that command made /usr/share/doc
your current directory.
After that, by typing cd
alone, you are returned to your home directory. If you ever wonder where you are in the filesystem, the pwd
command can help you. Here are a few other interesting cd
command options:
$ cd ~ $ pwd /home/chris $ cd ~/Music $ pwd /home/chris/Music $ cd ../../../usr $ pwd /usr
The tilde (~
) represents your home directory. So cd ~
takes you there. You can use the tilde to refer to directories relative to your home directory as well, such as /home/chris/Music
with ~/Music
. Typing a name as an option takes you to a directory below the current directory, but you can use two dots (..
) to go to a directory above the current directory. The example shown takes you up three directory levels (to /
), and then takes you into the /usr
directory.
The following steps lead you through the process of creating directories within your home directory and moving among your directories, with a mention of setting appropriate file permissions:
1 Go to your home directory. To do this, simply type cd in a shell and press Enter. (For other ways of referring to your home directory, see the sidebar “Identifying Directories.”)
2 To make sure that you're in your home directory, type pwd. When I do this, I get the following response (yours will reflect your home directory):$ pwd /home/joe
3 Create a new directory called test in your home directory, as follows:$ mkdir test
4 Check the permissions of the directory:$ ls -ld test drwxr-xr-x 2 joe sales 1024 Jan 24 12:17 testThis listing shows that test is a directory (d). The d is followed by the permissions (rwxr-xr-x), which are explained later in the section “Understanding File Permissions and Ownership.” The rest of the information indicates the owner (joe), the group (sales), and the date that the files in the directory were most recently modified (Jan 24 at 12:17 p.m.).NOTEWhen you add a new user in Fedora and Red Hat Enterprise Linux, the user is assigned to a group of the same name by default. For example, in the preceding text, the user joe would be assigned to the group joe. This approach to assigning groups is referred to as the user private group scheme.For now, enter the following: $ chmod 700 testThis step changes the permissions of the directory to give you complete access and everyone else no access at all. (The new permissions should read rwx------.)
5 Make the test directory your current directory as follows:$ cd test $ pwd /home/joe/test
If you followed along, at this point a subdirectory of your home directory called test
is your current working directory. You can create files and directories in the test
directory along with the descriptions in the rest of this chapter.