Skip to main content

RHCSA Certification Course

Chapter #3: File and Directory Management in Linux

In this chapter, we cover key Linux sysadmin tasks like creating, moving, deleting files, redirection, and compression.

In this chapter, we will review some essential skills that are required to be used in the day-to-day tasks of a system administrator.

Create, Delete, Copy, and Move Files and Directories

File and directory management is a critical competence that every system administrator should possess, which includes the ability to create / delete text files from scratch (the core of each program’s configuration) and directories (where you will organize files and other directories), and to find out the type of existing files.

The touch command can be used not only to create empty files, but also to update the access and modification times of existing files (see Fig. 1):

Figure 1: Using touch to create empty files and to update access times

To view file contents, especially small text files, you can use commands like cat, less, or more. cat shows the entire file immediately, while less and more allow for scrolling through longer files.

cat filename.txt  
less filename.txt  

You can use file [filename] to determine a file’s type (this will come in handy before launching your preferred text editor to edit it), as shown in Fig. 2:

Figure 2: Determining a file’s type

and rm [filename] to delete it (see Fig. 3).

Figure 3: Deleting files

As for directories, you can create directories inside existing paths with mkdir [directory] or create a full path with mkdir -p [/full/path/to/directory]. Please refer to Fig. 4 for more details.

Figure 4: Creating directories and directory structure

When it comes to removing directories, you need to make sure that they’re empty before issuing the rmdir [directory] command, or use the more powerful (handle with care!) rm -rf [directory].

This last option will force remove recursively the [directory] and all its contents - so use it at your own risk.

To copy files and directories, use cp. To move or rename, use mv.

cp source.txt destination.txt        # Copy a file  
cp -r /dir1 /dir2                    # Copy a directory  
mv oldname.txt newname.txt          # Rename a file  
mv file.txt /target/location/       # Move a file  

Input and Output Redirection and Pipelining

The command line environment provides two very useful features that allow to redirect of the input and output of commands from and to files, and to sending of the output of a command to another, called redirection and pipelining, respectively.

To understand those two important concepts, we must first understand the three most important types of I/O (Input and Output) streams (or sequences) of characters, which are in fact special files, in the *nix sense of the word:

  • Standard input (aka stdin) is by default attached to the keyboard.
  • Standard output (aka stdout) is by default attached to the screen.
  • Standard error (aka stderr) is where the status messages of a command are sent to by default, which is also the screen.

In the following example, the output of ls /var is sent to stdout (the screen), as well as the result of ls /tecmint.

But in the latter case, it is stderr that is shown (see Fig. 5):

Figure 5: Viewing file descriptors

To more easily identify these special files, they are each assigned a file descriptor.
The essential thing to understand is that these files, just like others, can be redirected.

This means you can capture the output from a file or script and send it as input to another file, command, or script.
This allows you to store the output of commands for later processing or analysis.

To redirect stdin (fd 0), stdout (fd 1), or stderr (fd 2), the following operators are available (see Table 1):