lines, and so on, -s shows the first line of text, a single blank line, the next line of text, a single blank line, and so forth. When you combine -s and -n, cat numbers only the lines that are printed — the 10 blank lines shown as one will count as 1 line for numbering.

This command prints information about your CPU, stripping out multiple blank lines and numbering the output:

$ cat -sn /proc/cpuinfo

You can also use cat to print the contents of several files at once, like this:

$ cat -s myfile.txt myotherfile.txt

In that command, cat merges myfile.txt and myotherfile.txt on the output, stripping out multiple blank lines. The important thing is that cat does not distinguish between the files in the output — no filenames are printed, and no extra breaks between the two files. This allows you to treat the two as one or, by adding more files to the command line, to treat 20 files as 1.

Changing Directories with cd

Changing directories is surely something that has no options, right? Not so. cd is actually more flexible than most people realize. Unlike most of the other commands here, cd is not a command in itself — it is built in to bash (or whichever shell interpreter you are using), but it is still used like a command.

The most basic use of cd is this:

$ cd somedir

That command looks in the current directory for the somedir subdirectory, and then moves you into it. You can also specify an exact location for a directory, like this:

$ cd /home/paul/stuff/somedir

The first part of cd's magic lies in the characters (- and ~, a dash and a tilde). The first means 'switch to my previous directory,' and the second means 'switch to my home directory.' This conversation with cd shows this in action:

[paul@caitlin ~]$ cd /usr/local

[paul@caitlin local]$ cd bin

[paul@caitlin bin]$ cd -

/usr/local

[paul@caitlin local]$ cd ~

[paul@caitlin ~]$

In the first line, we change to /usr/local and get no output from the command. In the second line, we change to bin, which is a subdirectory of /usr/local. Next, cd - is used to change back to the previous directory. This time, bash prints the name of the previous directory so we know where we are. Finally, cd ~ is used to change back to the home directory, although if you want to save an extra few keystrokes, just typing cd by itself is equivalent to cd ~.

The second part of cd's magic is its capability to look for directories in predefined locations. When you specify an absolute path to a directory (that is, one starting with a /), cd always switches to that exact location. However, if you specify a relative subdirectory — for example, cd subdir — you can tell cd to what location you would like that to be relative. This is accomplished with the CDPATH environment variable. If this variable is not set, cd always uses the current directory as the base; however, you can set it to any number of other directories.

The next example shows a test of this. It starts in /home/paul/empty, an empty directory, and the lines are numbered for later reference:

1 [paul@caitlin empty]$ pwd

2 /home/paul/empty

3 [paul@caitlin empty]$ ls

4 [paul@caitlin empty]$ mkdir local

5 [paul@caitlin empty]$ ls

6 local

7 [paul@caitlin empty]$ cd local

8 [paul@caitlin local]$ cd ..

9 [paul@caitlin empty]$ export CDPATH=/usr

10 [paul@caitlin empty]$ cd local

11 /usr/local

12 [paul@caitlin empty]$ cd -

13 /home/paul/empty

14 [paul@caitlin empty]$ export CDPATH=.:/usr

15 [paul@caitlin empty]$ cd local

16 /home/paul/empty/local

17 [paul@caitlin local]$

Lines 1-3 show that you are in /home/paul/empty and that it is indeed empty — ls had no output. Lines 4-6 show the local subdirectory being made so that /home/paul/empty/local exists. Lines 7 and 8 show you can cd into /home/paul/empty/local and back out again.

In line 9, CDPATH is set to /usr. This was chosen because Fedora has the directory /usr/local, which means the current directory (/home/paul/empty) and the CDPATH directory (/usr) both have a local subdirectory. In line 10, while in the /home/paul/empty directory, cd local is used. This time, bash switches to /usr/local and even prints the new directory to ensure that you know what it has done.

Lines 12 and 13 move you back to the previous directory, /home/paul/empty. In line 14, CDPATH is set to be .:/usr. The : is the directory separator, so this means bash should look first in the current directory, ., and then in the /usr directory. In line 15, cd local is issued again, this time moving to /home/paul/empty/local. Note that bash has still printed the new directory — it does that whenever it looks up a directory in CDPATH.

Changing File Access Permissions with chmod

What you learned about chmod earlier can be greatly extended through one simple parameter: -c. This instructs chmod to print a list of all the changes it made as part of its operation, which means you can capture the output and use it for other purposes. For example:

[paul@caitlin tmp]$ chmod -c 600 *

mode of `1.txt' changed to 0600 (rw-------)

mode of `2.txt' changed to 0600 (rw-------)

mode of `3.txt' changed to 0600 (rw-------)

[paul@caitlin tmp]$ chmod -c 600 *

[paul@caitlin tmp]$

There the chmod command is issued with -c, and you can see it has

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату