Previous Section  < Day Day Up >  Next Section

Recipe 9.3. Doing Batch Operations with chmod

9.3.1 Problem

You need to set permissions on all the files or a directory, or on batches of files.

9.3.2 Solution

chmod supports operating on lists of files. You can also use find or shell wildcards to generate a list.

To make several files read-only for everyone, you can use chmod as follows:

$ chmod 444 file.txt file2.txt file3.txt

To make all files in the current directory readable/writable, for the file owner and group, without changing the directory permissions, use:

$ find . -type f  -exec chmod -v 660 {  } \;

You can also change all files belonging to a particular user. This example starts at the root of the filesystem:

$ find / -user terri -exec chmod -v 660 {  } \;

You can set permissions for a directory and its contents, including subdirectories, with the -R (recursive) flag:

$ chmod -R -v 755 /shared

This example makes all the .txt files in the current directory readable/writable to the owner, and world-readable:

$ chmod -v 644 *.txt

To change all files in the current directory that begin with your chosen string, use:

$ chmod -v 644 apt*

9.3.3 See Also

  • info chmod

  • Linux in a Nutshell

  • Chapter 4 of LPI Linux Certification in a Nutshell for exhaustive detail on permissions and ownership, right down to the binary level

    Previous Section  < Day Day Up >  Next Section