File Management Commands in Linux Explained
This tutorial explains basic file management commands (including supported options, parameters and arguments) in Linux with practical examples. Learn how to use df command, du command, mount command, diff command, find command, locate command and sort command step by step in detail.
df
This is handy command to check available free space. Run du command
If you feel difficulty in understanding the blocks use -h switch with du command
Now outputs of du command look more users friendly. You could skip tmpfs and /dev/sr0 as tmpfs stand for temporary space and /dev/sr0 is my media device. This output is very useful when you need to manage disk. You could get an idea about which partition need more space or which partition is has unused free space. Linux LVM gives you an ability to change partition size without losing any data. With LVM you could reduce the size of partition which has unnecessary free space or you could expand the size of partition which requires more space. du command is very helpful when you need to make such a decision. As output of this command show size of my root partition is 7.7G and currently I am using 2.3G and available free space is 5.1G which is fine. Currently none of my partition requires more space. During the practice of LVM we would use this more frequently.
du
This is useful command to check the size of file. While df commands show the available space in partitions, du commands show the size of files in partitions. you could use df command to check the space used by each partitions. if you need more detail about any specific partition like which file is consuming more space then you could use du command.
For example we would like to know
how much space is used by /boot partition?
how much space is available in /boot partition?
what is the size of each files and directories in /boot partition?
To get the answer of these questions we would first execute df command with -h switch. It would gives us the answer of first and second question. To know the answer of third question use du command with -h switch.
You may get confuse from output. As df commands show boot partition is using 27 MB while du command is showing that /boot is using 21 MB so where is remaining 6 MB space?. This space is used by hidden files. You could use du command with -a switch to show the hidden files.
mount
mount is the another helpful command. During the practice we would create and format partitions. mount command would show the file system type of partition. and it also help to know to the type of mount.
diff command
diff command find the difference between files. In this article we would see how diff command can help us in RHCE exam.
Everything in Linux is managed through the several configuration files. During the exam you need to change the setting in several configuration files. You should always take backup before making any change in configuration file during the exam.
You can easily find out the changes which you have made if you have backup copy of configuration file.
Example of diff
We would modify vsftpd.conf file for practice.
ftp use vsftpd.conf configuration file for its setting. RHCE exam objective include ftp so you may have to configure it during the exam.
Create a directory and take backup of vsftpd.conf file
Start the ftp service
ftp by default allow anonymous login. Open vsftpd.conf
Your task is to disallow anonymous login.
Change configuration setting so ftp disable anonymous login and save the file
Notice that I made wrong setting. It would stop vsftpd service. Try to restart vsftpd service.
vsftpd service failed with error. To troubleshoot this we need to check what settings we did in configuration file. Here comes the magic of diff command. With diff command we can easily find the modified settings.
Now we know the modified settings. diff command also tell us the line number of modified settings.
Open vsftpd.conf file again
Display line number (To display line number use ESC + : + set nu)
To fix the issue delete line no 13 [contain text sdf] and save the file.
Now restart the vsftpd service again.
Restore the original configuration file back after doing this practice.
find
find command need two arguments file name and location. Syntax of find command is
#find [location] -name [file name ]
- find :- command
- location :- where you want to search the file
- -name :- option to specify the file name
- file name :- name of file which you want to search
For example to search vsftpd.conf [FTP configuration file] file we would use following command
#find / -name vsftpd.conf
This would start search from top level root directory and list the found.
Searching from root directory should be your last resources. When you perform search form root directory find command scan the entire Linux system for the desired file. It is time consuming process. Use subdirectories whenever you know it. For example if we know that vsftpd.conf file is located in /etc directory we should use following command
#find /etc -name vsftpd.conf
find command accepts wildcard. Wildcard allows us to find a file even we know only few characters of file name. For example our desired file starts from vs and have .conf in the end but we do not know the middle characters. In this case we would find it in following way
#find /etc -name vs*.conf
Wildcards
* | Any number of alphanumeric characters |
? | Single alphanumeric characters |
Example of wildcards
Create a directory and move in it
Make some blank files for practice of find command. Use touch command to create files.
Find the files those start from f and end with .conf
It would returns with following error
find: paths must precede expression
find command expand the wild card while it parse. So if result contain single match it would return without any error. Like in above example we searched for vs*.conf and it returned with correct result. But if result contains more than one match it would return with find: paths must precede expression error. It is because what find parsing in this case will look like
#find /root/practices_of_find -name file1.conf filek.conf
how to solve find: paths must precede expression error
solution of find: paths must precede expression error is very simple. Put the file name in quotes. It would stop the shell (bash) expanding your wildcards.
Find the files which
- have file in staring
- later one character could be anything
- ends with .conf
locate
find command is too time consuming specially in 2 hour RHCE exam. use locate command instead of find in exam. locate command use a database of installed files and directories. locate command database updated only once in a day.
Syntax of locate command is following
#locate [file name]
locate command search form its database so it does not require path.
Major drawback of locate command is that it update its database only once in a day. For example you can find demo.conf which we created in above example from find command but not from locate command.
database of locate command is updated from /etc/cron.daily/mlocate.cron script. We can manually run this script.
Now we can find demo.conf also from locate command
Tips
- Update locate command database as soon as possible and use locate command whenever you need to search any file.
- Use find command when locate does not works. Try to specify as much path as you remember when using find command.
sort command
sort command allow you to sort the content of file. With sort command you can sort the contents in several ways. By default, the sort command sorts the contents in alphabetical order depending on the first letter in each line.
In this article we would cover following topic
- Example of sort command
- How to sort a file in alphabetical order in linux
- How to sort by column in linux
- How to sort in reverse order in linux
- How to merge files with sort
- How to sort files by size
- List of options used with sort command
Example of sort command
Create a sample file with dummy names and age
$cat > test_file
How to sort a file in alphabetical order in linux
To sort this file alphabetically depending on name run following command
$sort test_file
How to sort by column in linux
To sort this file depending on age run following command
$sort -k2 test_file
-k2 is the option which refers to the second column. You can specify other column also. Suppose that file contain 8 columns and your desired column number is 6 than you should use -k6.
How to sort in reverse order in linux
To sort in reverse order use -r option with sort command. You can also combine it with other options. To sort in reverse order of second column run following command
$sort -r -k2 test_file
How to merge files with sort
-m option allows us merge files in a single file.
How to save sort output in file
By default sort command will print out on standard output. Nothing is going to write in file. To save output in file either use -o option or use redirect.
Use redirect method to save the output of sort command
Use -o option to save the output of sort command in file
How to sort number in linux
Use -n option to sort based on number. Create a simple file with numbers and use default sort order.
It is sorted alphabetically.
To sort this based on number use -n option
How to sort files by size
You can sort files by size with use of sort command. ls command is used to list the contents of directory.
Use sort command with ls command to sort the files by size. As you can see in above image that size have column no 5. To sort the output of ls command based on size run following command
$sort ls -l | sort -k5
List of options used with sort command
-r | Sorts in reverse order |
-s | Stabilize sort by disabling last-resort comparison |
-t | Use SEP instead of non-blank to blank transition |
-u | If line is duplicated only display once |
-b | Ignores blank spaces at beginning of the line. |
-c | Check whether input is sorted or not. |
-d | Use dictionary sort order and ignores the punctuation. |
-f | Ignores caps |
-k | Start a key at POS1, end it at POS2 (origin 1) |
-m | Merges two or more input files into one file. |
-M | Treats the first three letters in the line as a month (such as jun.) |
-n | Sorts by the beginning of the number at the beginning of the line. |
-o | Write result to FILE instead of standard output |
-o outputfile | Save the sorted output to a file. |
We have listed most frequently options used with sort command. To get a full list of all options with details read man page of sort command
$man sort
Created By ComputerNetworkingNotes Published On 2018-01-22 13:01:19 Last Updated 2018-02-25 02:17:56