File Management Commands in Linux Explained

This tutorial explains basic Linux file management commands in detail. Learn essential Linux commands for file management through practical examples.

Advertisements

The df command

The df (disk free) command prints available disk space of hard disk. To know how many disk space have been already consumed or used and how many disk space is still remaining or available in each partition of hard disk, you can use this command.

The df command uses the following syntax.

#df

Following image shows an example of df command with the output.

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

Following image shows how the df command formats the output when it is executed with the –h option.

df -h command

The du command

Advertisements

The du (disk used) command prints the size of files. To know which file is consuming how many disk space in a directory, you can use this command. 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 which are available in other directory, specify the full path of that directory as an argument with this command. For example to know the size of all available files in the /boot directory, use the following command.

#du /boot

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

#du -h /boot

Following images shows the output of du command with and without the –h option.

du command

The diff command

Advertisements

The diff command compares two files and prints the difference between them. Mostly, this command is used to figure out the changes made in a file by comparing it with its backup copy.

The diff command uses the following syntax.

#diff file1 file2

To understand how the diff command works, let’s take a simple example.

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

Following image shows this exercise step by step.

diff command

The find command

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

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

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

#find /etc –named hostname

Following image shows the output of this command.

find command

The locate command

Just like the find command, the locate command also searches the specified file. This command maintains and uses a database of all files and directories available in the system. This command uses the following syntax.

#locate [what to locate]

Following image shows an example of the locate command.

locate command

Differences between the find command and the locate command

Both commands search the specified file in different ways. The find command perform a real time search for the specified file in the specified location. Whereas, the locate command uses a database to search the specified file.

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

Following figure shows this exercise.

difference between find and locate command

As we can see in 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. If you have less time, such as in exam, use the locate command. If need accurate results, such as troubleshooting the system, use the find command.

Updating locate command database

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

#/etc/cron.daily/mlocate

Once database is updated, you can also search the recently added files. To verify it, let’s update the locate database and search the recently created file again. Following image shows this exercise with the output.

update 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 source files and directories and the path of destination directory.

The cp command uses the following syntax.

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

Common options used with the cp command are the following.

-a :- This option is used to copy the file with all properties and permissions. If 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 destination.

Following image shows some examples of the cp command.

cp command

By default, the cp command does not copy hidden files. To copy hidden files, we have to use the –a option. If the –R option or * argument is used, hidden files will also be copied. In such a case the –a option is not required to make the copy of hidden files. But it may require to maintain the permissions and properties of files at the destination.

The mv command

The mv command moves the supplied files and directories in the destination directory. This command works exactly 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 destination, the mv command remove them from the source whereas the cp command does not remove them from the source.

The mv command uses the following syntax.

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

Following image shows few examples of the mv command.

mv command

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

That’s all for this tutorial. If you like this tutorial, please don’t forget to share it with friends through your favorite social site.

Advertisements

ComputerNetworkingNotes RHCE 7 Study Guide File Management Commands in Linux Explained