Linux Bible. Christopher Negus

Чтение книги онлайн.

Читать онлайн книгу Linux Bible - Christopher Negus страница 49

Linux Bible - Christopher Negus

Скачать книгу

~ 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.

      Whether you are listing, moving, copying, removing, or otherwise acting on files in your Linux system, certain special characters, referred to as metacharacters and operators, help you to work with files more efficiently. Metacharacters can help you match one or more files without completely typing each filename. Operators enable you to direct information from one command or file to another command or file.

      Using file-matching metacharacters

      To save you some keystrokes and enable you to refer easily to a group of files, the bash shell lets you use metacharacters. Anytime you need to refer to a file or directory, such as to list, open, or remove it, you can use metacharacters to match the files you want. Here are some useful metacharacters for matching filenames:

* Matches any number of characters.
? Matches any one character.
[…] Matches any one of the characters between the brackets, which can include a hyphen-separated range of letters or numbers.

      Try out some of these file-matching metacharacters by first going to an empty directory (such as the test directory described in the previous section) and creating some empty files:

       $ touch apple banana grape grapefruit watermelon

      The touch command creates empty files. The commands that follow show you how to use shell metacharacters with the ls command to match filenames. Try the following commands to see whether you get the same responses:

       $ ls a* apple $ ls g* grape grapefruit $ ls g*t grapefruit $ ls *e* apple grape grapefruit watermelon $ ls *n* banana watermelon

      The first example matches any file that begins with a (apple). The next example matches any files that begin with g (grape, grapefruit). Next, files beginning with g and ending in t are matched (grapefruit). Next, any file that contains e in the name is matched (apple, grape, grapefruit, watermelon). Finally, any file that contains n is matched (banana, watermelon).

      Here are a few examples of pattern matching with the question mark (?):

       $ ls ????e apple grape $ ls g???e* grape grapefruit

      The first example matches any five-character file that ends in e (apple, grape). The second matches any file that begins with g and has e as its fifth character (grape, grapefruit).

      The following examples use braces to do pattern matching:

       $ ls [abw]* apple banana watermelon $ ls [agw]*[ne] apple grape watermelon

      In the first example, any file beginning with a, b, or w is matched. In the second, any file that begins with a, g, or w and also ends with either n or e is matched. You can also include ranges within brackets. For example:

       $ ls [a-g]* apple banana grape grapefruit

      Here, any filenames beginning with a letter from a through g are matched.

      Using file-redirection metacharacters

      Commands receive data from standard input and send it to standard output. Using pipes (described earlier), you can direct standard output from one command to the standard input of another. With files, you can use less than (<) and greater than (>) signs to direct data to and from files. Here are the file-redirection characters:

< Directs the contents of a file to the command. In most cases, this is the default action expected by the command and the use of the character is optional; using less bigfile is the same as less < bigfile.
> Directs the standard output of a command to a file. If the file exists, the content of that file is overwritten.
2> Directs standard error (error messages) to the file.
&> Directs both standard output and standard error to the file.
>>

Скачать книгу