File Power
We give you an overview of commands for moving, editing, compressing, and generally manipulating files.
We give you an overview of commands for moving, editing, compressing, and generally manipulating files.
GNU/Linux treats everything as a file. For this reason, learning file management commands should be among your first priorities in learning about the operating system. These commands are easy to remember because their names are usually abbreviations of their actions – for example, mv
for move and ls
for list – but their options can take time to learn.
Basically, file management commands fall into three categories: directory and file movement, navigation and editing, and compression. Commands in all three categories are typically more powerful (and potentially more dangerous) than their desktop equivalents, thanks mainly to file globbing, or the use of standard patterns to refer to multiple files.
The most basic command for moving directories and files is cp
. Its structure is simple:
<C>cp <options> <filesourcefile> <target><C>
By default, cp
overwrites any files of the same name in the target directory, but you can be cautious and use the -b
option to back up any files that are overwritten or the -u
option to overwrite only files that are newer than the ones in the target directory (Figure 1).
Also, you can add --preserve=mode
to choose to preserve file attributes, such as owner
or timestamp
, or --no-preserve=mode
to have them changed in the files' new location. Whether or not you preserve attributes is especially important when you are logged in as root and moving around files owned by another user – say, for a backup of the /home
directory.
Sometimes, you might not want to waste hard drive space on multiple copies of the same file, in which case you might prefer to use ln -s
file link to create a symbolic link, or pointer, to the original file, which takes up much less space (Figure 2). Later, if you copy these symbolic links to a backup, you can use cp -L
to ensure that the original file, not the link, is used.
Alternatively, you might prefer to move a file with mv
, which takes many of the same options as cp
. Also, you can use mv
to rename a file, giving it the same directory path but a different final name (Figure 3). To change the name of the file garden.png
while keeping it in the same directory, for example, you could use mv ./garden.png ./sun-yat-sen-gardens.png
.
As you copy or move files, you might want to create a new directory with mkdir
. This is a relatively straightforward command, but you can fine-tune it with --mode=octal-permissions
to set permissions for the new directory or create the directories immediately above it by adding the -p
(parent) option.
To delete, use rm
(remove) for files and directories and rmdir
for directories. Don't forget that, unlike the desktop, the Bash shell has no Trash folder. The closest you can get is to create a special folder and move files to it instead of using rm
or rmdir
.
By default, rm
works only on files. To delete directories with it, you have to use the -r
option. As you might imagine, rm -r
can remove key system files when used thoughtlessly; thus, some users prefer to add --preserve-root
when running the command anywhere near the root directory. In comparison, rmdir
is a much safer option, because it works only on empty directories (Figure 4).
A completely different approach to file management is taken by dd
, an old Unix utility that copies bytes or blocks rather than files. Used mainly by administrators, dd
has a non-standard syntax that is far too complex to detail here. Briefly, though, dd
can be used for such tasks as creating an ISO image from a CD/DVD, wiping a disk by filling it with random data, and duplicating a partition or master boot record. Just remember to construct your dd
command carefully and to double-check it. Even more than rm
, the dd
command can be hazardous to your system if you are inattentive.
You probably already know that you move around the directory tree with the command cd <directory>
– a command so simple that it has no options. You might not know, however, that cd
has several shortcuts: cd ..
moves to the directory immediately above the current one; cd -
returns you to the previous directory; and cd ~
returns you to your home directory (Figure 5). Combined with the command history in a virtual terminal, these shortcuts are enough to give you the equivalent of the back and forward buttons in a web browser.
Once you are in a directory, use ls
to view the contents. In many distributions, you will find that ls
is actually an alias of ls --color
, which displays different types of files in different colors. Sometimes, it is an alias of ls --color --classify
, which adds the use of symbols such as / to indicate a directory or * to indicate an executable file (Figure 6). For many users, these options are more than enough. However, sooner or later, you will likely need the -a
option, which displays hidden files – those whose names start with a period. To pinpoint a file, you might use -l
to display file attributes. To help sort files with ls
, various options let you sort by size (-s
), time (-t
), or extension (-X
).
All this information can easily occupy more lines than your terminal window displays, so you might want to pipe the command through less (ls | less
) so that only one screen full of information is visible at a time. If you are trying to identify a file, file
is a supplement to ls
, identifying the type of file (Figure 7). If you have symbolic links, you can add the -L
option so that you can identify the type of the original file. Also, you can use -z
to view the contents of compressed files (more on this later).
Yet another tool for tracking down files is find
. The find
command takes so many options that I list only some of the most important ones in Table 1. When you have located a file, you can use the touch
command to edit its timestamps. For example, typing
Table 1
find Command Options
Option | Information |
---|---|
|
Minutes since a file was accessed. |
|
Minutes since a file was changed. |
|
Days since a file was accessed. |
|
Days since a file was changed. |
|
Files that belong to a particular user group. |
|
Files that belong to a particular user. |
|
The maximum level of sub-directories in which to search. |
|
The minimum level of sub-directories in which to search for newer file names; files that are newer than the one mentioned in the option. |
|
Designated permissions. |
|
Excluded files of a certain sort from the search. Common file types include ascii and compress. |
touch -a grocery list.txt 0910311200.00
would change the access time to noon on October 31, 2009, and you can use the same date format after -m
to change the last modification time. Similarly, -t=YYMMDD.ss
changes the date and the time that the file was created. Also note that the time starts with the last two digits of the year and ends with the seconds.
Pages: 6
Price $15.99
(incl. VAT)