Читать книгу Linux Bible - Christopher Negus - Страница 129
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 in this example are dot (.
) files that are used to store GUI properties (.kde
directory) or shell properties (.bash
files). The only non-dot file in this list is the one named letter
. Column 3 shows the directory or file owner. The /home
directory is owned by root, and everything else is owned by the user joe, who belongs to the sales group (groups are listed in column 4).
In addition to the d
or -
, column 1 on each line contains the permissions set for that file or directory. Other information in the listing includes the number of hard links to the item (column 2), the size of each file in bytes (column 5), and the date and time each file was most recently modified (column 6).
Here are a few other facts about file and directory listings:
The number of characters shown for a directory (4096 bytes in these examples) reflects the size of the file containing information about the directory. Although this number can grow above 4096 bytes for a directory that contains lots of files, this number doesn't reflect the size of files contained in that directory.
The format of the time and date column can vary. Instead of displaying “May 12,” the date might be displayed as “2019-05-12,” depending upon the distribution and the language setting (LANG variable).
On occasion, instead of seeing the execute bit (x) set on an executable file, you may see an s in that spot instead. With an s appearing within either the owner (-rwsr-xr-x) or group (-rwxr-sr-x) permissions, or both (-rwsr-sr-x), the application can be run by any user, but ownership of the running process is assigned to the application's user/group instead of that of the user launching the command. This is referred to as a set UID or set GID program, respectively. For example, the mount command has permissions set as -rwsr-xr-x. This allows any user to run mount to list mounted filesystems (although you still have to be root to use mount to actually mount filesystems from the command line, in most cases).
If a t appears at the end of a directory, it indicates that the sticky bit is set for that directory (for example, drwxrwxr-t). By setting the sticky bit on a directory, the directory's owner can allow other users and groups to add files to the directory but prevent users from deleting each other's files in that directory. With a set GID assigned to a directory, any files created in that directory are assigned the same group as the directory's group. (If you see a capital S or T instead of the execute bits on a directory, it means that the set GID or sticky bit permission, respectively, was set, but for some reason the execute bit was not also turned on.)
If 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.