Linux Bible. Christopher Negus
you see a plus sign at the end of the permission bits (for example, -rw-rw-r--+), it means that extended attributes (+), such as Access Control Lists (ACLs), are set on the file. A dot at the end (.) indicates that SELinux is set on the file.
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 ...
Understanding File Permissions and Ownership
After you've worked with Linux for a while, you are almost sure to get a Permission denied
message. Permissions associated with files and directories in Linux were designed to keep users from accessing other users' private files and to protect important system files.
The nine bits assigned to each file for permissions define the access that you and others have to your file. Permission bits for a regular file appear as -rwxrwxrwx
. Those bits are used to define who can read, write, or execute the file.
NOTE
For a regular file, a dash appears in front of the nine-bit permissions indicator. Instead of a dash, you might see a d
(for a directory), l
(for a symbolic link), b
(for a block device), c
(for a character device), s
(for a socket), or p
(for a named pipe).
Of the nine-bit permissions, the first three bits apply to the owner's permission, the next three apply to the group assigned to the file, and the last three apply to all others. The r
stands for read, the w
stands for write, and the x
stands for execute permissions. If a dash appears instead of the letter, it means that permission is turned off for that associated read, write, or execute bit.
Because files and directories are different types of elements, read, write, and execute permissions on files and directories mean different things. Table 4.2 explains what you can do with each of them.
TABLE 4.2 Setting Read, Write, and Execute Permissions
Permission | File | Directory |
Read | View what's in the file. | See what files and subdirectories it contains. |
Write | Change the file's content, rename it, or delete it. | Add files or subdirectories to the directory. Remove files or directories from the directory. |
Execute | Run the file as a program. | Change to the directory as the current directory, search through the directory, or execute a program from the directory. Access file metadata (file size, time stamps, and so on) of files in that directory. |
As noted earlier, you can see the permission