Linux Bible. Christopher Negus

Чтение книги онлайн.

Читать онлайн книгу Linux Bible - Christopher Negus страница 53

Linux Bible - Christopher Negus

Скачать книгу

go-rwx file

      Likewise, the following examples start with all permissions closed (---------). The plus sign is used with chmod to turn permissions on.

      The following chmod command results in this permission: rw-------

       $ chmod u+rw files

      The following chmod command results in this permission: --x--x--x

       $ chmod a+x files

      The following chmod command results in this permission: r-xr-x---

       $ chmod ug+rx files

      Using letters to change permission recursively with chmod generally 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 myapps directory. 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 umask to see what your umask value is. For example:

       $ umask 0002

      If you ignore the leading zero for the moment, the umask value masks what is considered to be fully opened permissions for a file 666 or a directory 777. The umask value of 002 results in permission for a directory of 775 (rwxrwxr-x). That same umask results in a file permission of 644 (rw-rw-r--). (Execute permissions are off by default for regular files.)

      To change your umask value temporarily, run the umask command. Then try creating some files and directories to see how the umask value 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 umask value permanently, add a umask command to the .bashrc file in your home directory (near the end of that file). The next time you open a shell, your umask is set to whatever value you chose.

      Changing file ownership

      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.txt in 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 chown command changed the user to joe but 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 chown command 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/myusb directory, 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

      Commands for moving, copying, and deleting files are fairly straightforward. To change the location of a file, use the mv command. To copy a file from one location to another, use the cp command. To remove a file, use the rm command. 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/

      By default, the mv command overwrites any existing files if the file to which you are moving exists. However, many Linux systems alias the mv command so that it uses the -i option (which causes mv to 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 cp command 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 abc to the new name def in the same directory, whereas the second copies abc to your home directory (~), keeping the name abc. The two recursive (-r) copies copy the bash-completion directory and all of the files it contains, first to new /tmp/a/ and /tmp/b/ directories. If you run ls -l on those two directories, you see that for the cp command 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 cp command typically also is aliased with the -i option in order to prevent you from inadvertently overwriting files.

      As

Скачать книгу