The following chmodcommand results in this permission: rw-------
$ chmod u+rw files
The following chmodcommand results in this permission: --x--x--x
$ chmod a+x files
The following chmodcommand results in this permission: r-xr-x---
$ chmod ug+rx files
Using letters to change permission recursively with chmodgenerally works better than using numbers because you can change bits selectively instead of changing all permission bits at once. For example, suppose that you want to remove write permission for “other” without changing any other permission bits on a set of files and directories. You could do the following:
$ chmod -R o-w $HOME/myapps
This example recursively removes write permissions for “other” on any files and directories below the myappsdirectory. If you had used numbers such as 644, execute permission would be turned off for directories; using 755, execute permission would be turned on for regular files. Using o-w, only one bit is turned off and all other bits are left alone.
Setting default file permission with umask
When you create a file as a regular user, it's given permission rw-rw-r--by default. A directory is given the permission rwxrwxr-x. For the root user, file and directory permission are rw-r--r--and rwxr-xr-x, respectively. These default values are determined by the value of umask. Enter umaskto see what your umaskvalue is. For example:
$ umask 0002
If you ignore the leading zero for the moment, the umaskvalue masks what is considered to be fully opened permissions for a file 666 or a directory 777. The umaskvalue of 002 results in permission for a directory of 775 ( rwxrwxr-x). That same umaskresults in a file permission of 644 ( rw-rw-r--). (Execute permissions are off by default for regular files.)
To change your umaskvalue temporarily, run the umaskcommand. Then try creating some files and directories to see how the umaskvalue affects how permissions are set. For example:
$ umask 777 ; touch file01 ; mkdir dir01 ; ls -ld file01 dir01 d---------. 2 joe joe 6 Dec 19 11:03 dir01 ----------. 1 joe joe 0 Dec 19 11:02 file01 $ umask 000 ; touch file02 ; mkdir dir02 ; ls -ld file02 dir02 drwxrwxrwx. 2 joe joe 6 Dec 19 11:00 dir02/ -rw-rw-rw-. 1 joe joe 0 Dec 19 10:59 file02 $ umask 022 ; touch file03 ; mkdir dir03 ; ls -ld file03 dir03 drwxr-xr-x. 2 joe joe 6 Dec 19 11:07 dir03 -rw-r--r--. 1 joe joe 0 Dec 19 11:07 file03
If you want to change your umaskvalue permanently, add a umaskcommand to the .bashrcfile in your home directory (near the end of that file). The next time you open a shell, your umaskis set to whatever value you chose.
As a regular user, you cannot change ownership of files or directories to have them belong to another user. You can change ownership as the root user. For example, suppose that you created a file called memo.txtin the user joe's home directory while you were root user. Here's how you could change it to be owned by joe:
# chown joe /home/joe/memo.txt # ls -l /home/joe/memo.txt -rw-r--r--. 1 joe root 0 Dec 19 11:23 /home/joe/memo.txt
Notice that the chowncommand changed the user to joebut left the group as root. To change both user and group to joe, you could enter the following instead:
# chown joe:joe /home/joe/memo.txt # ls -l /home/joe/memo.txt -rw-r--r--. 1 joe joe 0 Dec 19 11:23 /home/joe/memo.txt
The chowncommand can be use recursively as well. Using the recursive option ( -R) is helpful if you need to change a whole directory structure to ownership by a particular user. For example, if you inserted a USB drive, which is mounted on the /media/myusbdirectory, and you wanted to give full ownership of the contents of that drive to the user joe, you could enter the following:
# chown -R joe:joe /media/myusb
Moving, Copying, and Removing Files
Commands for moving, copying, and deleting files are fairly straightforward. To change the location of a file, use the mvcommand. To copy a file from one location to another, use the cpcommand. To remove a file, use the rmcommand. These commands can be used to act on individual files and directories or recursively to act on many files and directories at once. Here are some examples:
$ mv abc def $ mv abc ~ $ mv /home/joe/mymemos/ /home/joe/Documents/
The first mvcommand moves the file abcto the file defin the same directory (essentially renaming it), whereas the second mvcommand moves the file abcto your home directory ( ~). The next mvcommand moves the mymemosdirectory (and all its contents) to the /home/joe/Documentsdirectory.
By default, the mvcommand overwrites any existing files if the file to which you are moving exists. However, many Linux systems alias the mvcommand so that it uses the -ioption (which causes mvto prompt you before overwriting existing files). Here's how to check if that is true on your system:
$ alias mv alias mv='mv -i'
Here are some examples of using the cpcommand to copy files from one location to another:
$ cp abc def $ cp abc ~ $ cp -r /usr/share/doc/bash-completion* /tmp/a/ $ cp -ra /usr/share/doc/bash-completion* /tmp/b/
The first copy command ( cp) copies abcto the new name defin the same directory, whereas the second copies abcto your home directory ( ~), keeping the name abc. The two recursive ( -r) copies copy the bash-completiondirectory and all of the files it contains, first to new /tmp/a/and /tmp/b/directories. If you run ls -lon those two directories, you see that for the cpcommand run with the archive ( -a) option, the date/time stamps and permissions are maintained by the copy. Without the -a, current date/time stamps are used, and permissions are determined by your umask.
The cpcommand typically also is aliased with the -ioption in order to prevent you from inadvertently overwriting files.
As with the cpand mvcommands, rmis also usually aliased to include the -ioption. This can prevent the damage that can come from an inadvertent recursive remove ( -r) option. Here are some examples of the rmcommand:
$ rm abc $ rm *
The first remove command deletes the abcfile; the second removes all of the files in the current directory (except that it doesn't remove directories and/or any files that start with a dot). If you want to remove a directory, you need to use the recursive ( -r) option to rmor, for an empty directory, you can use the rmdircommand. Consider the following examples:
$ rmdir /home/joe/nothing/ $ rm -r /home/joe/bigdir/ $ rm -rf /home/joe/hugedir/
The rmdircommand in the preceding code only removes the directory ( nothing) if it is empty. The rm -rexample removes the directory bigdirand all of its contents (files and multiple levels of subdirectories), but it prompts you before each is removed. When you add the force option ( -f), the hugedirdirectory and all of its contents are immediately removed, without prompting.
Читать дальше