Change Permissions Recursively


chmod -R

You've probably noticed by now that many Linux commands allow you to apply them recursively to files and directories, and chmod is no different. With the -R (or --recursive) option, you can change the permissions of hundreds of file system objects in secondsjust be sure that's what you want to do.

$ pwd /home/scott/pictures/libby $ ls -lF drwxrw---- 2 scott scott ... by_pool/ -rw-r--r-- 1 scott scott ... libby_arrowrock.jpg -rw-r--r-- 1 scott scott ... libby.jpg drwxrw---- 2 scott scott ... on_floor/ $ ls -l * -rw-r--r-- 1 scott scott ... libby_arrowrock.jpg -rw-r--r-- 1 scott scott ... libby.jpg by_pool: -rw-r--r-- 1 scott scott ... libby_by_pool_02.jpg -rwxr-xr-x 2 scott scott ... lieberman_pool.jpg on_floor: -rw-r--r-- 1 scott scott ... libby_on_floor_01.jpg -rw-r--r-- 1 scott scott ... libby_on_floor_02.jpg $ chgrp -R family * $ chmod -R 660 * chmod: 'by_pool' : Permission denied chmod: 'on_floor' : Permission denied 


"Permission denied?" What happened? Take a look at Table 7.2. If a file is executable, it can be run as a program, but a directory must be executable to allow users access inside it to read its files and subdirectories. Running chmod -R 660 * removed the x permission from everythingfiles and directories. When chmod went to report what it had done, it couldn't because it couldn't read inside those directories, since they were no longer executable.

So what should you do? There really isn't a simple answer. You could run chmod using a wildcard that only affects files of a certain type, like this:

$ chmod -R 660 *.jpg 


That would only affect images and not directories, so you wouldn't have any issues. If you have files of more than one type, however, it can quickly grow tedious, as you'll have to run chmod once for every file type.

If you have many subdirectories within subdirectories, or too many file types to deal with, you can be really clever and use the find command to look for all files that are not directories and then change their permissions. You'll learn more about that in Chapter 10, "The find Command."

The big takeaway here: When changing permissions recursively, be careful. You might not get what you expected and end up preventing access to files and subdirectories accidentally.



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net