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.
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.2explains 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 for any file or directory by typing the ls -ld
command. The named file or directory appears as those shown in this example:
$ ls -ld ch3 test -rw-rw-r-- 1 joe sales 4983 Jan 18 22:13 ch3 drwxr-xr-x 2 joe sales 1024 Jan 24 13:47 test
The first line shows that the ch3
file has read and write permission for the owner and the group. All other users have read permission, which means that they can view the file but cannot change its contents or remove it. The second line shows the test
directory (indicated by the letter d
before the permission bits). The owner has read, write, and execute permissions while the group and other users have only read and execute permissions. As a result, the owner can add, change, or delete files in that directory, and everyone else can only read the contents, change to that directory, and list the contents of the directory. (If you had not used the -d
options to ls
, you would have listed files in the test directory instead of permissions of that directory.)
Changing permissions with chmod (numbers)
If you own a file, you can use the chmod
command to change the permission on it as you please. In one method of doing this, each permission (read, write, and execute) is assigned a number—r=4, w=2, and x=1—and you use each set's total number to establish the permission. For example, to make permissions wide open for yourself as owner, you would set the first number to 7 (4+2+1), and then you would give the group and others read-only permission by setting both the second and third numbers to 4 (4+0+0), so that the final number is 744. Any combination of permissions can result from 0 (no permission) through 7 (full permission).
Here are some examples of how to change permission on a file (named file
) and what the resulting permission would be:
The following chmod
command results in this permission: rwxrwxrwx
# chmod 777 file
The following chmod
command results in this permission: rwxr-xr-x
# chmod 755 file
The following chmod
command results in this permission: rw-r--r--
# chmod 644 file
The following chmod
command results in this permission: ---------
# chmod 000 file
The chmod
command also can be used recursively. For example, suppose that you wanted to give an entire directory structure 755 permission ( rwxr-xr-x
), starting at the $HOME/myapps
directory. To do that, you could use the -R
option, as follows:
$ chmod -R 755 $HOME/myapps
All files and directories below, and including, the myapps
directory in your home directory will have 755 permissions set. Because the numbers approach to setting permission changes all permission bits at once, it's more common to use letters to change permission bits recursively over a large set of files.
Changing permissions with chmod (letters)
You can also turn file permissions on and off using plus ( +
) and minus ( –
) signs, respectively, along with letters to indicate what changes and for whom. Using letters, for each file you can change permission for the user ( u
), group ( g
), other ( o
), and all users ( a
). What you would change includes the read ( r
), write ( w
), and execute ( x
) bits. For example, start with a file that has all permissions open ( rwxrwxrwx
). Run the following chmod
commands using minus sign options. The resulting permissions are shown to the right of each command.
The following chmod
command results in this permission: r-xr-xr-x
$ chmod a-w file
The following chmod
command results in this permission: rwxrwxrw-
$ chmod o-x file
The following chmod
command results in this permission: rwx------
$ chmod go-rwx file
Likewise, the following examples start with all permissions closed ( ---------
). The plus sign is used with chmod
to turn permissions on.
Читать дальше