Linux Admin Commands

In this segment, you will learn about some more system admin commands that are used in Linux. We will perform some interesting operations in the file itself, including sorting the file, finding certain words or characters in the file and much more.
touch command
- In Linux, a file is always associated with timestamps, and every file stores the information of last access time, last modification time and last change time. So, whenever we open or modify a given file, its timestamps change.
- Touch is one such command if we wish to fiddle around with the file timestamps.
grep command
- grep searches for PATTERNS in each file. PATTERNS are one or more characters separated by newline characters, and grep prints each line that matches a pattern.
Summary of commands
The admin commands that were discussed in the video above can be summarised as follows:
- touch: This command is used to change the timestamps (the last access time, the last modification time and the last change time) of a file. If no file is present in the system and passed as an argument, then the command will create a file without any type of content inside it.
- touch -a filename: This command is used to change the access time of a file.
- touch -m filename: This command is used to change the modification time of a file.
- find: This command is used to search for a file in the file system by using some criteria such as the file name, the last access time, the last modification time, file permissions or the owner or size of the file.
- grep: This command is used to search for a string of characters in a file. If the string is found, the command will print all the lines containing the string. We can also use this command as follows:
- grep -i “string” filename: This command makes the string case insensitive. For example, the command will treat “upgrad”, “UPgrad” and “upGRAD” in the same manner.
- grep -c “string” filename: This command can be used to find the number of lines containing the string.
- grep -v “string” filename: This command can be used to display the lines in a file that does not contain the string.
- grep -l file1 file2 file3: This command can be used to display the name of the file out of multiple files containing the string.
- sudo: Linux does not allow all users to access some parts of the system in order to prevent sensitive files from being compromised. This command can be used to access such restricted files and operations. It temporarily elevates the privileges of the users and allows them to complete sensitive tasks without logging in as the root user. This command asks you for your personal password and confirms your request to execute a command by checking a file. Administrators can provide certain users or groups with the access to some or all commands without requiring those users to know the root password. Note that all the user information is stored in the sudoers file.
Try them out!
1. sort: This command is used to rearrange the contents in the file line by line. By default, sorting is done line by line, in which numbers are kept before letters, and lowercase letters are kept before uppercase letters. We can also use this command as follows:
a) sort -r filename: This command is used to sort the input file text in the reverse order i.e. from largest value to the smallest value. Suppose we have a file called “os.txt” which contains the following data:
Ubuntu
Debian
Symbian
Windows
OS
Linux
Android
Now if you run the following command, what do you think will be printed?
sort -r os.txt
You guessed it right! It will print all the names of the OSes in the reverse order:
Windows
Ubuntu
Symbian
OS
Linux
Debian
Android
b) sort -n filename: This command is used to sort a file that contains numerical data. It sorts the input file numerically.
c) sort -nr filename: This command is used to sort a file containing numeric data in the reverse order.
d) sort -u filename: This command is used to remove duplicates while sorting. Suppose we have a file called “os.txt” which contains the following data:
Ubuntu
Debian
Fedora
Symbian
Windows
OS
Linux
Fedora
Android
Now if you run the following command, what do you think will be printed?
sort -u os.txt
You guessed it right! It will print all the names of the OSes with duplicates removed:
Android
Debian
Fedora
Linux
OS
Symbian
Ubuntu
Windows
2. sed: This stands for Stream Editor. This command is mainly used for performing various operations on a file such as text substitution, find and replace. It can also be used for insertion and deletion. The main advantage of using this command is that you can edit your file without even opening it.
Suppose we have a file called “city.txt” which contains the following data:
Delhi is one of the most important cities in India.
It has a wide variety of arts & cultural centers.
Delhi is also surrounded by a river on its periphery.
Now suppose that we have to replace the city “Delhi” with “Kolkata”. How can we do it? You guessed it right! Let us run this commands & see what output it gives us on the terminal.
sed 's/Delhi/Kolkata/' city.txt
Output on the terminal:
Kolkata is one of the most important cities in India.
It has a wide variety of arts & cultural centers.
Kolkata is also surrounded by a river on its periphery.
3. clear: This command is used to clear the terminal screen.
In the next segment, Sandeep will talk about the applications of a few more administrative commands.