File Management Commands in Linux Explained

Mastering basic file management commands in Linux is essential for efficient system administration and organization. Commands such as df, du, diff, find, locate, and cp offer powerful tools for monitoring disk usage, comparing files, searching for specific files, and managing file copies effectively. This tutorial explains these commands through practical examples.

The df command

The df (disk free) command prints the available disk space of the hard disk. You can use this command to know how much disk space has already been consumed or used and how much disk space remains or is available in each partition.

It uses the following syntax.

#df

The following image shows an example of the df command with the output.

the df command

By default, this command uses block size to print the used and available space. To print the same output in GB/MB/KB, use the –h option with this command.

#df -h

The following image shows how the df command formats the output when executed with the –h option.

the df -h command

The du command

The du (disk used) command prints the size of files. You can use this command to know which file consumes how much disk space in a directory. This command uses the following syntax.

#du

By default, this command prints the size of all files available in the current directory. To print the size of files available in another directory, specify the directory's full path as an argument with this command. For example, use the following command to know the size of all available files in the /boot directory.

#du /boot

Similar to the df command, this command also uses block size to display the size of files. To show the output in GB/MB/KB, use the –h option.

#du -h /boot

The following images show the du command output with and without the –h option.

the du command

The diff command

The diff command compares two files and prints the difference between them. This command is mainly used to determine the changes made in a file by comparing it with its backup copy.

The diff command uses the following syntax.

#diff file1 file2

Let's take a simple example to understand how the diff command works.

Create a file with some dummy text. Make a backup copy of this file. Make some changes in the original file and use the diff command to figure out those changes.

the diff command

The find command

The find command searches the specified file in the selected location. This command needs two arguments: the location where you want to search the file and the file name you wish to search. This command uses the following syntax.

#find [where to search] –name [what to search]

For example, the following command searches for the hostname file in the /etc directory.

#find /etc –named hostname

The following image shows the output of this command.

the find command

The locate command

Like the find command, the locate command also searches the specified file. This command maintains and uses a database of all files and directories. It uses the following syntax.

#locate [what to locate]

The following image shows an example of the locate command.

the locate command

Differences between the find command and the locate command

Both commands search the specified file in different ways. The find command performs a real-time search for the selected file in the specified location. Meanwhile, the locate command uses a database to search for the specified file.

The find command is slow but works in all situations. The locate command is fast but may only work in some situations. To understand it practically, create a file and search it from both commands.

The following figure shows this exercise.

Difference between find and locate command

As shown in the above image, the locate command fails to find the recently created file.

Based on the situation, you can choose and use any one command from both or can use both commands. Use the locate command if you have less time, such as in an exam. Use the find command if you need accurate results, such as troubleshooting the system.

Updating locate command database

By default, the locate command updates its database once a day. You can manually update this database by running the following command if required.

#/etc/cron.daily/mlocate

Once the database is updated, you can search for recently added files. To verify this, let's update the locate database and search for recent files. The following image shows this exercise with the output.

Updating locate database

The cp command

The cp command makes the copy of supplied files and directories in the destination directory. This command needs two arguments: the name of the source files and directories and the path of the destination directory.

It uses the following syntax.

$cp [name of source files or directories] [path of destination directory]

The following are commonly used options with the cp command.

-a:- This option is used to copy the file with all properties and permissions. If the file is copied without this option, the copied file gets permission based on the destination directory.

-R:- This option is used to copy all files and directories, including the sub-directories, from the source to the destination.

The following image shows examples of the cp command.

the cp command

By default, the cp command does not copy hidden files. To copy hidden files, we have to use the –a option. Using the –R option or * argument also recursively copies all the hidden files. In such a case, the –a option is not required to make the copy of hidden files. However, it may require maintaining the permissions and properties of files at the destination.

The mv command

The mv command moves the supplied files and directories to the destination directory. This command works the same as the cp command. The only difference between the cp command and the mv command is that after making the copy of supplied files and directories at the destination, the mv command removes them from the source. In contrast, the cp command does not remove them from the source.

It uses the following syntax.

$mv [source files or directories] [path of destination directory]

The following image shows examples of the mv command.

the mv command

The mv command does not support the -R option. To perform the recursive move, use the wild card * as an argument with the source directory.

This tutorial is part of the tutorial series Linux disk management concepts and configurations. Other parts of this series are the following.

Chapter 01  Linux disk management terminology
Chapter 02  File Management Commands in Linux
Chapter 03  Linux file system types explained
Chapter 04  Adding and removing hard disks in VMware Workstation
Chapter 05  List view and find hard disk names in Linux
Chapter 06  The fdisk command on Linux explained
Chapter 07  Manage Linux disk partition with the gdisk command
Chapter 08  The /etc/fstab file on Linux explained
Chapter 09  Linux disk management with the parted command
Chapter 10  The mkfs command on Linux
Chapter 11  The mount command on Linux temporary mounting
Chapter 12  The swap space on Linux explained
Chapter 13  How to create a swap partition in Linux
Chapter 14  How to configure LVM in Linux step-by-step
Chapter 15  How to configure RAID in Linux step-by-step

Conclusion

By practicing the commands in this tutorial, you can enhance your proficiency in navigating and managing files in a Linux environment, making you a more compelling user or administrator. Understanding the differences between commands like `find` and `locate` can help you choose the right tool for the task, ensuring accurate results or quick search capabilities depending on your needs. Additionally, learning to update the `locate` command’s database ensures you can access the most current file information.

ComputerNetworkingNotes Linux Tutorials File Management Commands in Linux Explained

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com