Читать книгу Linux Bible - Christopher Negus - Страница 130

Identifying Directories

Оглавление

When you need to identify your home directory on a shell command line, you can use the following:

$HOME This environment variable stores your home directory name.
~ The tilde (~) represents your home directory on the command line.

You can also use the tilde to identify someone else's home directory. For example, ~joe would be expanded to the joe home directory (probably /home/joe). So, if I wanted to go to the directory /home/joe/test, I could enter cd ~joe/test to get there.

Other special ways of identifying directories in the shell include the following:

. A single dot (.) refers to the current directory.
.. Two dots (..) refer to a directory directly above the current directory.
$PWD This environment variable refers to the current working directory.
$OLDPWD This environment variable refers to the previous working directory before you changed to the current one. (Entering cd – returns you to the directory represented by $OLDPWD.)

As I mentioned earlier, there are many useful options for the ls command. Return to the $HOME/test directory in which you've been working. Here are some examples of ls options. Don't worry if the output doesn't exactly match what is in your directory at this point.

Any file or directory beginning with a dot (.) is considered hidden and is not displayed by default with ls. These dot files are typically configuration files or directories that need to be in your home directory but don't need to be seen in your daily work. The -a lets you see those files.

The -t option displays files in the order in which they were most recently modified. With the -F option, a backslash (/) appears at the end of directory names, an asterisk (*) is added to executable files, and an at sign (@) is shown next to symbolic links.

To show hidden and non-hidden files:

 $ ls -a . apple docs grapefruit pointer_to_apple .stuff watermelon .. banana grape .hiddendir script.sh .tmpfile

To list all files by time most recently modified:

 $ ls -at .tmpfile .hiddendir .. docs watermelon banana script.sh . .stuff pointer_to_apple grapefruit apple grape

To list files and append file-type indicators:

 $ ls -F apple banana docs/ grape grapefruit pointer_to_apple@ script.sh* watermelon

To avoid displaying certain files or directories when you use ls, use the --hide= option. In the next set of examples, any file beginning with g does not appear in the output. Using a -d option on a directory shows information about that directory instead of showing the files and directories the directory contains. The -R option lists all files in the current directory as well as any files or directories that are associated with the original directory. The -S option lists files by size.

To exclude any files beginning with the letter g in the list:

 $ ls --hide=g* apple banana docs pointer_to_apple script.sh watermelon

To list info about a directory instead of the files it contains:

 $ ls -ld $HOME/test/ drwxrwxr-x. 4 joe joe 4096 Dec 18 22:00 /home/joe/test/

To create multiple directory layers (-p is needed):

 $ mkdir -p $HOME/test/documents/memos/

To list all files and directories recursively from current directory down:

 $ ls -R ...

To list files by size:

 $ ls -S ...

Linux Bible

Подняться наверх