RHCSA Series: How to Perform File and Directory Management – Part 2

In this article, RHCSA Part 2: File and directory management, we will review some essential skills that are required in the day-to-day tasks of a system administrator.

RHCSA: Perform File and Directory Management – Part 2
RHCSA: Perform File and Directory Management – Part 2

Create, Delete, Copy, and Move Files and Directories

File and directory management is a critical competence that every system administrator should possess. This 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.

touch command example
touch command example

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

file command example
file command example

and rm [filename] to delete it.

Linux  rm command examples
rm command example

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

mkdir command example
mkdir command example

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.

Input and Output Redirection and Pipelining

The command line environment provides two very useful features that allows to redirect the input and output of commands from and to files, and to send 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.

  1. Standard input (aka stdin) is by default attached to the keyboard. In other words, the keyboard is the standard input device to enter commands to the command line.
  2. Standard output (aka stdout) is by default attached to the screen, the device that “receives” the output of commands and display them on the screen.
  3. Standard error (aka stderr), is where the status messages of a command is 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.

Linux input output redirect
Input and Output Example

To more easily identify these special files, they are each assigned a file descriptor, an abstract representation that is used to access them. The essential thing to understand is that these files, just like others, can be redirected. What this means is that you can capture the output from a file or script and send it as input to another file, command, or script. This will allow you to store on disk, for example, 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.

Redirection Operator Effect
> Redirects standard output to a file containing standard output. If the destination file exists, it will be overwritten.
>> Appends standard output to a file.
2> Redirects standard error to a file containing standard output. If the destination file exists, it will be overwritten.
2>> Appends standard error to the existing file.
&> Redirects both standard output and standard error to a file; if the specified file exists, it will be overwritten.
< Uses the specified file as standard input.
<> The specified file is used for both standard input and standard output.

As opposed to redirection, pipelining is performed by adding a vertical bar (|) after a command and before another one.

Remember:

  1. Redirection is used to send the output of a command to a file, or to send a file as input to a command.
  2. Pipelining is used to send the output of a command to another command as input.

Examples Of Redirection and Pipelining

Example 1: Redirecting the output of a command to a file

There will be times when you will need to iterate over a list of files. To do that, you can first save that list to a file and then read that file line by line. While it is true that you can iterate over the output of ls directly, this example serves to illustrate redirection.

# ls -1 /var/mail > mail.txt
Redirect output of command tot a file
Redirect output of command tot a file
Example 2: Redirecting both stdout and stderr to /dev/null

In case we want to prevent both stdout and stderr to be displayed on the screen, we can redirect both file descriptors to /dev/null. Note how the output changes when the redirection is implemented for the same command.

# ls /var /tecmint
# ls /var/ /tecmint &> /dev/null
Redirecting stdout and stderr ouput to /dev/null
Redirecting stdout and stderr ouput to /dev/null
Example 3: Using a file as input to a command

While the classic syntax of the cat command is as follows.

# cat [file(s)]

You can also send a file as input, using the correct redirection operator.

# cat < mail.txt
Linux cat command examples
cat command example
Example 4: Sending the output of a command as input to another

If you have a large directory or process listing and want to be able to locate a certain file or process at a glance, you will want to pipeline the listing to grep.

Note that we use to pipelines in the following example. The first one looks for the required keyword, while the second one will eliminate the actual grep command from the results. This example lists all the processes associated with the apache user.

# ps -ef | grep apache | grep -v grep
Send output of command as input to another
Send output of command as input to another
Gabriel Cánepa
Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

20 thoughts on “RHCSA Series: How to Perform File and Directory Management – Part 2”

  1. I have not understand why we used second grep in below example.if first grep is capable of providing details of all apache process , what is use of second one ?

    # ps -ef | grep apache | grep -v grep.

    Reply
    • Compare the output without the second pipeline and the second grep and you’ll find your answer :). Hint: also check the purpose of the -v option of grep.

      Reply
  2. Hello Gabriel,

    Nice article, also correct the typo: Note that we use to(2) pipelines in the following example > Note that we use two(2) pipelines in the following example.

    Reply
    • There is no typo. Compare the output without the second pipeline and the second grep and you’ll understand why I used two greps and two pipelines :). Hint: also check the purpose of the -v option of grep.

      Reply
  3. thank u for the demonstration. I got ask u a question i.e i had a file 200M and created a hardlink for this file the actual disk space will 400M for both files or 200M.since u run ls -li will show both file point to same inode and they had the same size

    Reply
    • @Faisal,
      The process of creating a hard link does NOT duplicate the file. So if you have a 200 MB file and create a hard link to it, the hard link itself will not occupy another extra 200 MB. One way you can check this is by doing
      du -sch /full/path/to/directory/*
      where /full/path/to/directory/ is the directory where you have the 200 MB file. Do that and then delete the hard link, then try again. You will not see a difference in the disk usage.

      Reply
  4. In the ‘hard & soft link’, maybe the phrase ‘On the other hand, creating a hard link results in an extra disk usage of 5 bytes.’ should be ‘On the other hand, creating a soft link results in an extra disk usage of 5 bytes.’, isn’t it?

    Reply
    • @Eduardo,
      Good catch! Yes, that is a typo. Thanks for bringing that up to our attention. I’ll ask Ravi to correct it.

      Reply
      • It should be corrected by now, more than a year now. Still, points to hard link. If you wish can also update that why hard links can’t be created for directories.

        Reply
  5. While ‘&> ‘ works in récent Bash,
    ‘2>&1 ‘ Will redirect stdout “to where stderr is” on close to every shell around.

    Reply
    • @Cyrille,
      You are correct. But I wonder if anyone today is using a Bash version that is old enough to not accept ‘&>’. If they do, they better update it :).

      Reply
  6. Small mistake, in tar compression flags you have commit (should be uppercase J instead lowercase j for xz compression ).

    PS.
    Thanks for that two articles, for me is some kind of reapet …

    Reply
    • @Tomas,
      You are correct my friend! That was a typo. Ravi will correct it when he sees this comment. Thank you for your contribution.

      Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.