Using the command line under Linux is routine; usually we memorize forty or fifty commands, but some rarely used yet useful commands are overlooked by most people. This post collects a small subset of them.

sudo !!

Some commands report an error when run without superuser privileges, after which sudo !! can be used to invoke the previous command. !! grabs the last command.

python command

With a simple python command we can produce a simple web page that displays the folder structure over HTTP, accessible from a browser on port 8000 until an interrupt signal is sent.

$ python -m SimpleHTTPServer

mtr

The mtr command combines the functionality of the ping and traceroute commands; mtr may need to be installed with apt/yum before use

$ sudo apt install mtr #Debian based systems
$ yum install mtr #Red Hat based systems

Ctrl+x+e

To automate daily tasks, administrators need to open an editor by typing vi, vim, nano and so on. Simply pressing Ctrl-x-e quickly from the command line lets you start working in the editor.

nl

The nl command adds line numbers to a file, that is, it adds line numbers to the specified file.

shuf

This command randomly selects lines/files/folders from a file or folder. It can be appended to the ls command to pick things at random.

$ ls|shuf -n1  #randomly select one
$ ls|shuf -n2  #randomly select two. And so on

ss

ss stands for socket statistics. This command investigates sockets and shows information similar to the netstat command. It can show more TCP and state information than other tools.

last

This command shows historical information about the last logged-in users (by searching the file “/var/log/wtmp”, it displays the list of logged-in and logged-out users along with their tty’s).

curl ifconfig.me

This command is used to obtain your external IP address; using curl may require installing the relevant packages.

tree

Displays the current folder in a tree structure.

pstree

Displays all processes of the currently running commands and the related child processes, with the output shown in a tree structure.

Every command typed in the terminal is recorded in history, which can be viewed with the history command. When typing a command in the terminal you can put a space in front of the command, so that your command is not recorded in history.

stat

The stat command in Linux is used to display status information about a file or file system. When a file name is used as the argument, the command returns all information about the file, including file size, blocks, permissions, access time, modification time, status change time and so on.

pv

This command simulates the text output of any pipeline, including piped output. The pv package may need to be installed first before use.

mount | column -t

Displays all mounted file systems in a neat format.

Ctrl + l

This is more of a shortcut key, and has the same effect as clear.

at

This command is similar to the cron command and can also be used to schedule a task or execute a command at a specific moment.

$ echo "ls -l > /dev/pts/0" | at 14:012
$ echo "ls -l > /dev/pts/0" | at 2:12 PM

du -h -max-depth=1

This command outputs the sizes of the subdirectories of the current directory in human-readable form. This is very useful when checking system usage.

expr

expr performs simple arithmetic in the terminal.

look

Look up a word from the English dictionary in the terminal to avoid confusion.

$ look car  #show all words in the dictionary starting with 'car'

yes

This command keeps outputting the given string until an interrupt command is received. This is very useful in scripting and system administration.

factor

This command outputs all the factors of the given number.

ping -i n -a IP_address

When waiting or keeping your eyes on the terminal waiting for a command’s response or for a server connection: a sound is emitted when the server is reachable (make sure your system is not muted). The parameter n is the waiting time of the command.

#this command PINGs once every 60 seconds
$ ping -i 60 -a www.google.com

tac

This command outputs the content of a text file in reverse order, that is, from the last line to the first line.

strace

strace is a debugging tool mainly used for troubleshooting on Linux. It may not be installed by default on your system, and you may need to install the required packages with apt or yum.

$ strace pwd  #use the strace command to trace the execution of a command

For the concrete command parameters of strace, see the man page.

disown -a && exit

disown can keep a task running in the background even after the terminal session is closed.

$ <command> disown -a && exit

To detach an already long-running task from the terminal again (translator’s note: it may already be running in the background, but not detached from the terminal), use the jobs command to find the job number and then use disown %n, where %n is the job number. To verify that the job is indeed running, use the ps or top command. The nohup command is also an alternative to the disown command.

getconf LONG_BIT

Shows whether your machine architecture is 32bit or 64bit.

Displaying the date

This can be considered a script: enter the following command at the terminal prompt and you will see the date and time in the top right corner of the terminal.

$  while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &

watch -t -n1 “date + %T | figlet”

Before using it, make sure the figlet package is installed on the system. This command displays an animated digital clock in the terminal by piping the output to figlet.

host and dig

host is a DNS lookup tool, and the dig command is used to resolve domain names, giving more domain name information than nslookup.

dstat

dstat is a versatile tool that generates statistics based on system resources. dstat may not be installed by default on your system. Use apt or yum to install it before using this colorful generator of system information.

bind -p

This command shows all the shortcuts available in the BASH shell.

lsb_release

The lsb_release command prints information specific to the distribution. If lsb_release is not yet installed, you can install the package with apt on Debian-based distributions and with yum on Red Hat based systems.

Note: the option ‘-a’ shows all available information about the version, ID, description, release number and codename.

nc -zv localhost

The parameter num is any port number; this command checks whether the port is open or closed.

curl ipinfo.io

This command outputs and provides the geographic location of the IP address.

find . -user root

This command outputs the files in the current directory owned by the (root) user (translator’s note: that is, with owner root). Below are all the files owned by the ‘root’ user listed in the current directory.

sudo apt-get build-dep ffmpeg

This command automatically builds the dependencies when the corresponding package is installed. As a result, the package installation process will be very smooth and very easy.

lsof -iTCP: -sTCP:LISTEN

This command outputs the names of the processes/services that are using port .

pdftk

pdftk is used to merge several pdf files; make sure the pdftk package is installed on the system before use.

ps -LF -u user_name

This command outputs the processes and threads of a user. The option “L” (list threads), the option “-F” (full formatting)

startx - :1

Share an X session. If you frequently need to log in and out of the desktop (as different users), then startx comes to the rescue. This command creates a new session, thus avoiding repeated logging in and out within one session. To switch between X sessions we can use the key combinations ctrl+Alt+F7 and ctrl+Alt+F8.

Note: the shortcuts ctrl+Alt+F1~F6 are for console sessions, while ctrl+Alt+F7~F12 serve X sessions. So we have 6 console sessions and 6 X sessions, and there is no need to log in and out frequently. The ordering above applies to most distributions, though different distributions may implement it differently.

Reference

Linux.China - open source community