Learning file management commands

Lead Image © Benoit Chartron and Federico Rostagno, 123RF.com

File Power

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.

Moving and Editing Directories and 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).

Figure 1: The cp command allows you to be both cautious and flexible. Here, the root user ensures that files with the same name as those being copied are not overwritten and that the owner of the files does not change.

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.

Figure 2: Creating a symbolic link with ln is a space-saving way of having the same file in two places at the same time.

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.

Figure 3: The mv command does double duty, both moving files and renaming them.

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).

Figure 4: The rmdir command is much safer to use than rm -r, because it can't delete directories that still have files in them.

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.

Navigating and Editing Directories and Files

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.

Figure 5: Using the shortcuts in the change directory command are much faster than typing out the entire name of a directory. They require one or two characters – far fewer than when typing the names of most directories in your home.

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).

Figure 6: Many distributions create an alias for the ls command so that it automatically displays different types of files with different colors.

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).

Figure 7: The file command identifies the format of files, helping you identify them.

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



-amin <minutes>

Minutes since a file was accessed.

-cmin <minutes>

Minutes since a file was changed.

-atime <days>

Days since a file was accessed.

-amin <days>

Days since a file was changed.

-group <group>

Files that belong to a particular user group.

-user <user>

Files that belong to a particular user.

-maxdepth <number>

The maximum level of sub-directories in which to search.

-mindepth <number>

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.

-perm <permissions>

Designated permissions.

-e <filetype>

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.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content