output the result of the operation: Three files were changed to rw-------
(read and write by user only).
However, when the command is issued again, no output is returned. This is because -c
prints only the changes that it made. Files that already match the permissions you are setting are left unchanged and therefore are not printed.
There are two other parameters of interest: --reference
and -R
. The first enables you to specify a file to use as a template for permissions rather than specifying permissions yourself. For example, if you want all files in the current directory to have the same permissions as the file /home/paul/myfile.txt
, you would use this:
$ chmod --reference /home/paul/myfile.txt *
You can use -R
to enable recursive operation, which means you can use it to chmod
a directory and it changes the permissions of that directory and all files and subdirectories under that directory. You could use chmod -R 600 /home
to change every file and directory under /home
to become read/write to their owners.
Copying Files with cp
Like mv
, which is covered later, cp
is a command that is easily used and mastered. However, two marvelous parameters rarely see much use (which is a shame!) despite their power. These are --parents
and -u
, the first of which copies the full path of the file into the new directory. The second copies only if the source file is newer than the destination.
Using --parents
requires a little explanation, so here is an example. You have a file, /home/paul/desktop/documents/work/notes.txt
, and want to copy it to your /home/paul/backup folder
. You could just do a normal cp
, but that would give you /home/paul/backup/notes.txt
, so how would you know where that came from later? If you use --parents
, the file is copied to /home/paul/backup/desktop/documents/work/notes.txt
.
The -u
parameter is perfect for synchronizing two directories because it enables you to run a command like cp -Ru myfiles myotherfiles
and have cp
recopy only files that have changed. The -R
parameter means
Printing Disk Use with du
The du
command prints the size of each file and directory that is inside the current directory. Its most basic use is as easy as it gets:
$ du
That query outputs a long list of directories and how much space their files take up. You can modify that with the -a parameter, which instructs du to print the size of individual files and directories. Another useful parameter is -h, which makes du use human-readable sizes like 18M (18MB) rather than 17532 (the same number in bytes, unrounded). The final useful basic option is -c
, which prints the total size of files.
So, using du
, you can get a printout of the size of each file in your home directory, in human-readable format, and with a summary at the end, like this:
$ du -ahc /home/paul
Two advanced parameters deal with filenames you want excluded from your count. The first is -- exclude
, which enables you to specify a pattern that should be used to exclude files. This pattern is a standard shell file-matching pattern as opposed to a regular expression, which means you can use ?
to match a single character or *
to match 0 or many characters. You can specify multiple --exclude
parameters to exclude several patterns. For example:
$ du --exclude='*.xml' --exclude='*.xsl'
Of course, typing numerous --exclude
parameters repeatedly is a waste of time, so you can use -X
to specify a file that has the list of patterns you want excluded. The file should look like this:
*.xml
*.xsl
That is, each pattern you want excluded should be on a line by itself. If that file were called xml_exclude.txt
, you could use it in place of the previous example like this:
$ du -X xml_exclude.txt
You can make your exclusion file as long as you need, or you can just specify multiple -X
parameters.
Running du
in a directory where several files are hard-linked to the same inode counts the size of the file only once. If you want to count each hard link separately for some reason, use the -l
parameter (lowercase
Finding Files by Searching with find
The find
command is one of the darkest and least understood areas of Linux, but it is also one of the most powerful. Admittedly, the find
command does not help itself by using X-style parameters. The UNIX standard is -c
, -s
, and so on, whereas the GNU standard is --dosomething
, --mooby
, and so forth. X-style parameters merge the two by having words preceded by only one dash.
However, the biggest problem with find
is that it has more options than most people can remember — it truly is capable of doing most things you could want. The most basic use is this:
$ find -name '*.txt'
That query searches the current directory and all subdirectories for files that end in .txt
. The previous search finds files ending in .txt
but not .TXT
, .Txt
, or other case variations. To search without case sensitivity, use -iname
instead of - name
. You can optionally specify where the search should start before the -name
parameter, as follows:
$ find /home -name '*.txt'
Another useful test is -size
, which lets you specify how big the files should be to match. You can specify your size in kilobytes and optionally also use +
or -
to specify greater than or less than. For example:
$ find /home -name '*.txt' -size 100k
$ find /home -name '*.txt' -size +100k
$ find /home -name '*.txt' -size -100k
The first brings up files of exactly 100KB, the second only files greater than 100KB, and the last only files less than 100KB.
Moving on, the -user
option enables you to specify the user that owns the files you are looking for. So, to search for all files in /home
that end with .txt
, are under 100KB,