The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a complete list, but a brief list of commands that can be useful when needed. The following will introduce how to use these commands one by one with examples.

1. uptime command

In Linux, the uptime command shows how long your system has been running and how many users are currently logged in. It also shows the load averages at intervals of 1 minute, 5 minutes, and 15 minutes.

$ uptime
 08:16:26 up 22 min,  1 user,  load average: 0.00, 0.03, 0.22

Check uptime version

Except for uptime (uptime) and version (version), the uptime command has no other options. If the time is less than 1 day, it only gives information in the form of hours:mins.

$ uptime -V
 procps version 3.2.8

2. w command

This command will display the currently logged in users and their processes, as well as the average load. In addition, it also displays the login name, tty name, remote host, login time, idle time, JCPU, PCPU, commands and processes.

# w
08:27:44 up 34 min,  1 user,  load average: 0.00, 0.00, 0.08
USER    TTY    FROM     LOGIN@   IDLE   JCPU   PCPU WHAT
tecmint  pts/0    192.168.50.1     07:59    0.00s  0.29s  0.09s w
 Available options
 -h: Do not display the title.
 -s: Do not display JCPU and PCPU.
 -f: Do not display field information.
 -V: (uppercase V)-display version.

3. users command

The users command displays the currently logged-in users. Except for help and version, this command has no other parameters.

# users
 Linuxconcept

4. who command

The who command only returns the user name, date, time, and host information; the who command is similar to the w command, unlike the w command, who does not output the information about the operation performed by the user, let’s take a look at the two commands who and w The difference.

# who
 linuxconcept pts / 0 2020-09-18 07:59 (192.168.50.1)
# w
 08:43:58 up 50 min,  1 user,  load average: 0.64, 0.18, 0.06
 USER    TTY    FROM    LOGIN@   IDLE   JCPU   PCPU WHAT
 linuxconcept  pts/0    192.168.50.1     07:59    0.00s  0.43s  0.10s w
 options for who command
 -b: Display the date and time of the last system restart.
 -r: Display the current run level.
 -a, -all: Display all accumulated information.

5. whoami command

The whoami command outputs the name of the current user; you can also use the “who am i” command to display the current user. If you log in as the root user with the sudo command, the “whoami” command returns that the root user is the current user, if you want to know who is logged in Which user is specific, use the “who am i” command.

# whoami
 linuxconcept

6. ls command

The ls command displays a list of files in human readable format.

# ls -l
 total 114
 dr-xr-xr-x.   2 root root  4096 Sep 18 08:46 bin
 dr-xr-xr-x.   5 root root  1024 Sep  8 15:49 boot

Sort files according to the last modification time.

# ls -ltr
 total 40
 -rw-r--r--. 1 root root  6546 Sep 17 18:42 install.log.syslog
 -rw-r--r--. 1 root root 22435 Sep 17 18:45 install.log
 -rw-------. 1 root root  1003 Sep 17 18:45 anaconda-ks.cfg

7. crontab command

You can use the crontab command and the -l option to list the scheduled tasks of the current user.

# crontab -l
 00 10 * * * /bin/ls >/ls.txt

You can use the -e option to edit crontab. In the following example, you will use the VI editing tool to open the scheduled task, make the necessary changes, and press the :wq key to exit, which will automatically save the settings.

# crontab -e

8.  less command

The less command allows you to quickly view files; you can page up and down and press “q” to exit the less window.

# less install.log
 Installing setup-2.8.14-10.el6.noarch
 warning: setup-2.8.14-10.el6.noarch: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
 Installing filesystem-2.4.30-2.1.el6.i686
 Installing ca-certificates-2010.63-3.el6.noarch
 Installing xml-common-0.6.3-32.el6.noarch
 nstalling tzdata-2010l-1.el6.noarch
 Installing iso-codes-3.16-2.el6.noarch

9. more command

The more command allows you to quickly view files and display detailed information as a percentage. You can page up and down and press “q” to exit the more window.

# more install.log
 Installing setup-2.8.14-10.el6.noarch
 warning: setup-2.8.14-10.el6.noarch: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
 Installing filesystem-2.4.30-2.1.el6.i686
 Installing ca-certificates-2010.63-3.el6.noarch
 Installing xml-common-0.6.3-32.el6.noarch
 Installing tzdata-2010l-1.el6.noarch
 Installing iso-codes-3.16-2.el6.noarch
 --More--(10%)

10.  cp command

Copy files from source to destination, keeping the same pattern.

# cp -p fileA fileB

The system will prompt you before overwriting the file.

# cp -i fileA fileB

11.  mv command

Rename fileA to fileB; -i option will prompt before overwriting; if the file already exists, it will ask for confirmation.

# mv -i fileA fileB

12.  cat command

The cat command is used to view multiple files at the same time.

# cat fileA fileB

If a file cannot be displayed on a screen/page, you can use the cat command to combine the more and less commands to view the contents of the file.

# cat install.log | less
      or
# cat install.log | more

13.  cd command (change directory)

With the help of the cd command (change directory), it will enter the fileA directory.

# cd / fileA

14.  pwd command (output working directory)

The pwd command will return the current working directory.

# pwd
/root

15.  sort command

Sort text files line by line in ascending order. If you use the -r option, it will be sorted in descending order.

#sort fileA.txt
#sort -r fileA.txt

16.  vi command

For most UNIX-like operating systems, vi is the most popular text editor. The following example uses the -R option to open a file in read-only mode and press “:q” to exit the vi window.

# vi -R /etc/shadows

17.  ssh command (secure shell)

The ssh command is used to log in to the remote host; for example, the following ssh example will use the user as narad to connect to the host (192.168.50.2).

# ssh narad@192.168.50.2

To check the version of ssh, use the option -V (uppercase) to display the version of ssh.

# ssh -V
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010

18.  ftp or sftp command

The ftp or sftp command is used to connect to a remote ftp host; ftp refers to a file transfer protocol, and sftp is a secure file transfer protocol. For example, the following command will connect to the ftp host (192.168.50.2).

# ftp 192.168.50.2
# sftp 192.168.50.2

Just as you can use mput to upload multiple files to a remote host, we can also use mget to download multiple files from a remote host.

# ftp > mput *.txt
# ftp > mget *.txt

19.  service command

The service command calls the script located in the /etc/init.d/ directory and executes the script . There are two ways to start any service; for example, we use the service command to start a service named httpd.

# service httpd start
# /etc/init.d/httpd start

20.  free command

The free command displays information about free memory, total memory, and exchanges, in bytes.

# free
 total           used            free         shared     buffers    cached
 Mem:       1030800     735944     294856          0      51648     547696
 -/+ buffers/cache:     136600     894200
 Swap:      2064376          0    2064376

The free command with the -t option displays the total memory used and the memory that can be used, in bytes.

# free  -t
 total       used       free     shared    buffers     cached
 Mem:       1030800     736096     294704          0      51720     547704
 -/+ buffers/cache:     136672     894128
 Swap:      2064376          0    2064376
 Total:     3095176     736096    2359080

21.  top command

The top command displays the processor activity of the system, and also displays the tasks managed by the kernel in real time. It displays the processor and memory used; if you use the top command with the “u” option, it will display the specific user process details The information is as follows: press “O” (uppercase) to sort the way you want, press “q” to exit the top screen.

# top -u linuxconcept
 top - 11:13:11 up  3:19,  2 users,  load average: 0.00, 0.00, 0.00
 Tasks: 116 total,   1 running, 115 sleeping,   0 stopped,   0 zombie
 Cpu (s): 0.0% us, 0.3% sy, 0.0% ni, 99.7% id, 0.0% wa, 0.0% hi, 0.0% si, 0.0% st
 Mem:   1030800k total,   736188k used,   294612k free,    51760k buffers
 Swap:  2064376k total,        0k used,  2064376k free,   547704k cached
 PID   USER          PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1889 linuxconcept   20   0 11468 1648  920 S  0.0  0.2   0:00.59 sshd
 1890 linuxconcept   20   0  5124 1668 1416 S  0.0  0.2   0:00.44 bash
 6698 linuxconcept   20   0 11600 1668  924 S  0.0  0.2   0:01.19 sshd
 6699 linuxconcept   20   0  5124 1596 1352 S  0.0  0.2   0:00.11 bash

22.   tar command

The tar command is used to compress files and folders under Linux; for example, the following command will create a compressed archive for the /home directory with the file name archive-name.tar.

# tar -cvf archive-name.tar /home

To decompress the tar compressed archive file, please use the options shown below.

# tar -xvf archive-name.tar

23.   grep command

grep can search for specific strings in files; only the tecmint users in the /etc/passwd file are displayed. We can use the -i option to ignore case.

# grep linuxconcept /etc/passwd
 linuxconcept:x:500:500::/home/linuxconcept:/bin/bash

24.   find command

The find command is used to search for files, strings and directories; in the following example, the find command searches for the word tecmint in the “/” partition and returns the output.

# find / -name linuxconcept
 / var / spool / mail / linuxconcept
 /home/linuxconcept
 /root/home/linuxconcept

25.   lsof command

lsof means “list all open files”; below, the lsof command lists all files opened by the tecmint user.

# lsof -u linuxconcept
 COMMAND  PID    USER   FD   TYPE     DEVICE SIZE/OFF   NODE NAME
 sshd 1889 linuxconcept pwd DIR 253,0 4096 2 /
 sshd    1889 linuxconcept  txt    REG      253,0   532336 298069 /usr/sbin/sshd
 sshd    1889 linuxconcept  DEL    REG      253,0          412940 /lib/libcom_err.so.2.1
 sshd    1889 linuxconcept  DEL    REG      253,0          393156 /lib/ld-2.12.so
 sshd    1889 linuxconcept  DEL    REG      253,0          298643 /usr/lib/libcrypto.so.1.0.0
 sshd    1889 linuxconcept  DEL    REG      253,0          393173 /lib/libnsl-2.12.so
 sshd    1889 linuxconcept  DEL    REG      253,0          412937 /lib/libkrb5support.so.0.1
 sshd    1889 linuxconcept  DEL    REG      253,0          412961 /lib/libplc4.so

26.   last command

With the last command, we can observe the user’s activities in the system; this command can also be executed as a normal user, it will display complete user information, such as terminal, time, date, system restart or startup, and kernel version, This is a useful command for troubleshooting.

# last
 linuxconcept  pts/1        192.168.50.1     Tue Sep 18 08:50   still logged in
 linuxconcept  pts/0        192.168.50.1     Tue Sep 18 07:59   still logged in
 reboot   system boot  2.6.32-279.el6.i Tue Sep 18 07:54 - 11:38  (03:43)
 root     pts/1        192.168.50.1     Sun Sep 16 10:40 - down   (03:53)
 root     pts/0        :0.0             Sun Sep 16 10:36 - 13:09  (02:32)
 root     tty1         :0               Sun Sep 16 10:07 - down   (04:26)
 reboot   system boot  2.6.32-279.el6.i Sun Sep 16 09:57 - 14:33  (04:35)
 narad pts / 2 192.168.50.1 Thu Sep 13 08:07 - down (01:15) 

You can use last with username to learn about the activities of a specific user, as shown below.

# last linuxconcept
 linuxconcept  pts/1        192.168.50.1     Tue Sep 18 08:50   still logged in
 linuxconcpet  pts/0        192.168.50.1     Tue Sep 18 07:59   still logged in
 linuxconcept pts / 1 192.168.50.1 Thu Sep 13 08:07 - down (01:15)
 linuxconcept  pts/4        192.168.50.1     Wed Sep 12 10:12 - 12:29  (02:17) 

27.   ps command

The ps command displays information about the processes running in the system; the following example only shows the init process.

# ps -ef | grep init
 root         1     0  0 07:53 ?        00:00:04 /sbin/init
 root      7508  6825  0 11:48 pts/1    00:00:00 grep init

28.   kill command

Use the kill command to terminate the process; first use the ps command to find the process id, as shown below, and then use the kill -9 command to terminate the process.

# ps -ef | grep init
 root         1     0  0 07:53 ?        00:00:04 /sbin/init
 root      7508  6825  0 11:48 pts/1    00:00:00 grep init
# kill- 9 7508

29.   rm command

The rm command is used to clear or delete files without prompting for confirmation.

# rm filename

Use the -i option to confirm before deleting; using the two options “-r” and “-f” will forcefully delete the file without confirming it.

# rm -i test.txt
rm: remove regular file `test.txt'?

30.   mkdir command

The mkdir command is used to create a directory under Linux.

# mkdir directoryname

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles