Linux Bible. Christopher Negus
Чтение книги онлайн.
Читать онлайн книгу Linux Bible - Christopher Negus страница 50
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 a nameserver 100.100.100.100 . w q resendit
With these lines added to a script run by the root user, the ed
text editor adds the IP address of a DNS server to the /etc/resolv.conf
file.
Using brace expansion characters
By using curly braces ({}
), you can expand out a set of characters across filenames, directory names, or other arguments to which you give commands. For example, if you want to create a set of files such as memo1
through memo5
, you can do that as follows:
$ touch memo{1,2,3,4,5} $ ls memo1 memo2 memo3 memo4 memo5
The items that are expanded don't have to be numbers or even single digits. For example, you could use ranges of numbers or digits. You could also use any string of characters, as long as you separate them with commas. Here are some examples:
$ touch {John,Bill,Sally}-{Breakfast,Lunch,Dinner} $ ls Bill-Breakfast Bill-Lunch John-Dinner Sally-Breakfast Sally-Lunch Bill-Dinner John-Breakfast John-Lunch Sally-Dinner $ rm -f {John,Bill,Sally}-{Breakfast,Lunch,Dinner} $ touch {a..f}{1..5} $ ls a1 a3 a5 b2 b4 c1 c3 c5 d2 d4 e1 e3 e5 f2 f4 a2 a4 b1 b3 b5 c2 c4 d1 d3 d5 e2 e4 f1 f3 f5
In the first example, the use of two sets of braces means John, Bill, and Sally each have filenames associated with Breakfast, Lunch, and Dinner. If I had made a mistake, I could easily recall the command and change touch
to rm -f
to delete all of the files. In the next example, the use of two dots between letters a
and f
and numbers 1
and 5
specifies the ranges to be used. Note the files that were created from those few characters.
Listing Files and Directories
The ls
command is the most common command used to list information about files and directories. Many options available with the ls
command allow you to gather different sets of files and directories as well as to view different kinds of information about them.
By default, when you type the ls
command, the output shows you all non-hidden files and directories contained in the current directory. When you type ls
, however, many Linux systems (including Fedora and RHEL) assign an alias ls
to add options. To see if ls
is aliased, enter the following:
$ alias ls alias ls='ls --color=auto'
The --color=auto
option causes different types of files and directories to be displayed in different colors. So, return to the $HOME/test
directory created earlier in the chapter, add a couple of different types of files, and then see what they look like with the ls
command.
$ cd $HOME/test $ touch scriptx.sh apple $ chmod 755 scriptx.sh $ mkdir Stuff $ ln -s apple pointer_to_apple $ ls apple pointer_to_apple scriptx.sh Stuff
Although you can't see it in the preceding code example, the directory Stuff
shows up in blue, pointer_to_apple
(a symbolic link) appears as aqua, and scriptx.sh
(which is an executable file) appears in green. All other regular files show up in black. Typing ls -l
to see a long listing of those files can make these different types of files clearer still:
$ ls -l total 4 -rw-rw-r--. 1 joe joe 0 Dec 18 13:38 apple lrwxrwxrwx. 1 joe joe 5 Dec 18 13:46 pointer_to_apple -> apple -rwxr-xr-x. 1 joe joe 0 Dec 18 13:37 scriptx.sh drwxrwxr-x. 2 joe joe 4096 Dec 18 13:38 Stuff
As you look at the long listing, notice that the first character of each line shows the type of file. A hyphen (-
) indicates a regular file, d
indicates a directory, and l
(lowercase L) indicates a symbolic link. An executable file (a script or binary file that runs as a command) has execute bits turned on (x
). See more on execute bits in the upcoming section “Understanding File Permissions and Ownership.”
You should become familiar with the contents of your home directory next. Use the -l
and -a
options to ls
.
$ ls -la /home/joe total 158 drwxrwxrwx 2 joe sales 4096 May 12 13:55 . drwxr-xr-x 3 root root 4096 May 10 01:49 .. -rw------- 1 joe sales 2204 May 18 21:30 .bash_history -rw-r--r-- 1 joe sales 24 May 10 01:50 .bash_logout -rw-r--r-- 1 joe sales 230 May 10 01:50 .bash_profile -rw-r--r-- 1 joe sales 124 May 10 01:50 .bashrc drw-r--r-- 1 joe sales 4096 May 10 01:50 .kde -rw-rw-r-- 1 joe sales 149872 May 11 22:49 letter ^ ^ ^ ^ ^ ^ ^ col 1 col 2 col 3 col 4 col 5 col 6 col 7
Displaying a long list (-l
option) of the contents of your home directory shows you more about file sizes and directories. The total
line shows the total amount of disk space used by the files in the list (158 kilobytes in this example). Adding the all files option (-a
) displays files that begin with a dot (.
). Directories such as the current directory (.
) and the parent directory (..
)—the directory above the current directory—are noted as directories by the letter d
at the beginning of each entry. Each directory begins with a d
and each file begins with a dash (-
).
The file and directory names are shown in column 7. In this example, a dot (.
) represents /home/joe
and two dots (..
) represent /home
—the parent directory of /joe
. Most of the files