Linux Bible. Christopher Negus. Читать онлайн. Newlib. NEWLIB.NET

Автор: Christopher Negus
Издательство: John Wiley & Sons Limited
Серия:
Жанр произведения: Зарубежная компьютерная литература
Год издания: 0
isbn: 9781119578895
Скачать книгу
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.
>>

      The following are some examples of command lines where information is directed to and from files:

       $ mail root < ~/.bashrc $ man chmod | col -b > /tmp/chmod $ echo "I finished the project on $(date)" >> ~/projects

      In the first example, the content of the .bashrc file in the home directory is sent in a mail message to the computer's root user. The second command line formats the chmod man page (using the man command), removes extra back spaces (col -b), and sends the output to the file /tmp/chmod (erasing the previous /tmp/chmod file, if it exists). The final command results in the following text being added to the user's project file:

       I finished the project on Sat Jun 15 13:46:49 EDT 2019

      Another type of redirection, referred to as here text (also called here document), enables you to type text that can be used as standard input for a command. Here documents involve entering two less-than characters (<<) after a command, followed by a word. All typing following that word is taken as user input until the word is repeated on a line by itself. Here is an example:

       $ mail root cnegus rjones bdecker << thetext > I want to tell everyone that there will be a 10 a.m. > meeting in conference room B. Everyone should attend. > > -- James > thetext $

      This example sends a mail message to root, cnegus, rjones, and bdecker usernames. The text entered between <<thetext and thetext becomes the content of the message. A common use of here text is to use it with a text editor to create or add to a file from within a script:

       /bin/ed /etc/resolv.conf <<resendit