Important Linux Commands You Should Know with Examples

Linux system administrators spend a significant amount of their time at a command-line prompt. They often automate and troubleshoot tasks in this test environment. There is a saying, “graphical user interfaces make easy tasks easier, while command-line interfaces make difficult tasks possible“.

sudo

Sudo allows users to run programs using the security privileges of another user, generally root (superuser).

[[email protected] ~]$ sudo yum update
[sudo] password for Aps: 
Last metadata expiration check: 2:30:59 ago on Fri 24 Apr 2020 10:35:18 PM IST.

shutdown

The preferred method to shut down or reboot the system is to use the shutdown command.

How to Locate Applications using Linux terminal?

which

One way to locate programs is to employ the which utility.

[[email protected] ~]$ which firefox
/usr/bin/firefox

whereis

If which does not find the program, whereis is a good alternative because it looks for packages in a broader range of system directories:

[[email protected] ~]$ whereis firefox
firefox: /usr/bin/firefox /usr/lib64/firefox /etc/firefox /usr/share/man/man1/firefox.1.gz

 

Accessing Directories

When you first log into a system or open a terminal, the default directory should be your Home directory. You can print the exact path of this by typing echo $HOME.

[[email protected] ~]$ echo $HOME
/home/Aps

pwd

Displays the present working directory.

[[email protected] ~]$ pwd
/home/Aps

cd ~ or cd

Change to your home directory (shortcut name is ~ (tilde)).

[[email protected] Desktop]$ cd ~
[[email protected] ~]$
[[email protected] Desktop]$ cd
[[email protected] ~]$

 

cd ..

Change to parent directory (..)

[[email protected] Desktop]$ cd ..
[[email protected] ~]$

cd –

Change to previous directory (– (minus))

[[email protected] Desktop]$ pwd
/home/Aps/Desktop
[[email protected] Desktop]$ cd -
/home/Aps

 

What is Absolute pathname? 

An absolute pathname begins with the root directory and follows the tree, branch by branch, until it reaches the desired directory or file. Absolute paths always start with /.

Absolute pathname method
$ cd /usr/bin

 

What is Relative pathname?

A relative pathname starts from the present working directory. Relative paths never start with /.

Relative pathname method
$ cd ../../usr/bin

 

tree

The tree command is a good way to get a bird’s-eye view of the filesystem tree.

tree -d

Use tree -d to view just the directories and to suppress listing file names.

 

cd /

Changes your current directory to the root (/) directory (or path you supply).

ls

List the contents of the present working directory.

[[email protected] Desktop]$ ls
models.py thesocialtalks thesocialtalks.tar.xz

ls –a

List all files, including hidden files and directories (those whose name start with . ).

[[email protected] Desktop]$ ls -a
. .. models.py thesocialtalks thesocialtalks.tar.xz

tree

Displays a tree view of the filesystem.

 

cat

Used for viewing files that are not very long; it does not provide any scroll-back.

[[email protected] news]$ cat apps.py
from django.apps import AppConfig


class NewsConfig(AppConfig):
name = 'news'

tac

Used to look at a file backwards, starting with the last line.

[[email protected] news]$ tac apps.py 
name = 'news'
class NewsConfig(AppConfig):


from django.apps import AppConfig

 

tail

Used to print the last 10 lines of a file by default. You can change the number of lines by doing -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default.

[[email protected] templates]$ tail pagination.html
<a href="?page={{ page.previous_page_number }}">Previous</a>
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}">Next</a>
{% endif %}
</span>
</div>

head

The opposite of tail; by default, it prints the first 10 lines of a file.

 

touch

touch is often used to set or update the access, change, and modify times of files. By default, it resets a file’s timestamp to match the current time.

However, you can also create an empty file using touch:

[[email protected] Desktop]$ ls
models.py thesocialtalks thesocialtalks.tar.xz
[[email protected] Desktop]$ touch important.txt
[[email protected] Desktop]$ ls
important.txt models.py thesocialtalks thesocialtalks.tar.xz

touch provides several useful options. For example, the -t option allows you to set the date and timestamp of the file to a specific value, as in:

[[email protected] Desktop]$ ls -l important.txt 
-rw-rw-r--. 1 Aps Aps 0 Dec 15 2020 important.txt
[[email protected] Desktop]$ #reset date on important.txt 
[[email protected] Desktop]$ touch -t 201804301015 important.txt 
[[email protected] Desktop]$ ls -l important.txt 
-rw-rw-r--. 1 Aps Aps 0 Apr 30 2018 important.txt

 

mkdir

mkdir is used to create a directory:

[[email protected] test_file]$ ls
[[email protected] test_file]$ mkdir test_directory
[[email protected] test_file]$ ls
test_directory

It creates a sample directory named test_directory under the current directory.

[[email protected] test_file]$ mkdir test_directory/test_dir
[[email protected] test_file]$ ls
test_directory
[[email protected] test_file]$ tree
.
└── test_directory
└── test_dir

2 directories, 0 files

It creates a sample directory called test_dir under test_directory.

rmdir

rmdir is used to remove a directory. The directory must be empty or the command will fail.

rm -rf

To remove a directory and all of its contents you have to do rm -rf.

rmdir works only on empty directories; otherwise you get an error.

While typing rm –rf is a fast and easy way to remove a whole filesystem tree recursively, it is extremely dangerous and should be used with the utmost care, especially when used by root (recall that recursive means drilling down through all sub-directories, all the way down a tree).

[[email protected] test_file]$ tree
.
└── test_directory
└── test_dir

2 directories, 0 files
[[email protected] test_file]$ rmdir test_directory/
rmdir: failed to remove 'test_directory/': Directory not empty
[[email protected] test_file]$ rm -rf test_directory/
[[email protected] test_file]$ ls

mv

  • Simply rename a file
  • Move a file to another location, while possibly changing its name at the same time.
[[email protected] test_file]$ ls
aps.txt
[[email protected] test_file]$ mv aps.txt apss.txt
[[email protected] test_file]$ ls
apss.txt

rm

Remove a file

[[email protected] test_file]$ ls
apss.txt
[[email protected] test_file]$ rm apss.txt 
[[email protected] test_file]$ ls

rm –f

Forcefully remove a file

[[email protected] test_file]$ touch apss.txt
[[email protected] test_file]$ ls
apss.txt
[[email protected] test_file]$ rm -f apss.txt 
[[email protected] test_file]$ ls

rm –i

Interactively remove a file

If you are not certain about removing files that match a pattern you supply, it is always good to run rm interactively (rm –i) to prompt before every removal.

[[email protected] test_file]$ touch apss.txt
[[email protected] test_file]$ ls
apss.txt
[[email protected] test_file]$ rm -i apss.txt 
rm: remove regular empty file 'apss.txt'? no 
[[email protected] test_file]$ ls
apss.txt
[[email protected] test_file]$ rm -i apss.txt 
rm: remove regular empty file 'apss.txt'? yes
[[email protected] test_file]$ ls

ping

ping is used to check whether or not a machine attached to the network can receive and send data; i.e. it confirms that the remote host is online and is responding.

To check the status of the remote host, at the command prompt, type ping <hostname>.

ping is frequently used for network testing and management; however, its usage can increase network load unacceptably. Hence, you can abort the execution of ping by typing CTRL-C, or by using the -c option, which limits the number of packets that ping will send before it quits. When execution stops, a summary is displayed.

[[email protected] ~]$ ping google.co.in
PING google.co.in (172.217.166.195) 56(84) bytes of data.
64 bytes from del03s13-in-f3.1e100.net (172.217.166.195): icmp_seq=1 ttl=57 time=25.4 ms
64 bytes from del03s13-in-f3.1e100.net (172.217.166.195): icmp_seq=2 ttl=57 time=45.2 ms
64 bytes from del03s13-in-f3.1e100.net (172.217.166.195): icmp_seq=3 ttl=57 time=83.8 ms
64 bytes from del03s13-in-f3.1e100.net (172.217.166.195): icmp_seq=4 ttl=57 time=56.2 ms
^C
--- google.co.in ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 25.366/52.651/83.826/21.125 ms

traceroute

traceroute is used to inspect the route which the data packet takes to reach the destination host, which makes it quite useful for troubleshooting network delays and errors. By using traceroute, you can isolate connectivity issues between hops, which helps resolve them faster.

To print the route taken by the packet to reach the network host, at the command prompt, type traceroute <address>.

[[email protected] ~]$ traceroute google.co.in
traceroute to google.co.in (172.217.166.195), 30 hops max, 60 byte packets
1 _gateway (192.168.0.1) 17.545 ms 17.377 ms 17.221 ms
2 * * *
3 163.53.87.1 (163.53.87.1) 98.981 ms 99.018 ms 98.684 ms
4 74.125.118.140 (74.125.118.140) 98.541 ms 98.548 ms 103.16.31.6 (103.16.31.6) 98.337 ms
5 108.170.251.113 (108.170.251.113) 98.182 ms 98.036 ms *
6 74.125.243.97 (74.125.243.97) 97.727 ms 172.253.67.88 (172.253.67.88) 80.929 ms 64.233.174.150 (64.233.174.150) 81.020 ms
7 del03s13-in-f3.1e100.net (172.217.166.195) 80.745 ms 80.750 ms 66.249.95.149 (66.249.95.149) 80.857 ms

 

  • Source –> https://www.linuxfoundation.org/

Leave a Reply