The commands listed below should help you better navigate, manage, and search Linux systems. The Linux commands listed are also useful in grabbing more information when troubleshooting. These command-line tips apply to all Linux systems and distros, both on virtual and physical machines.
1. List and show all IP addresses associated with all network interfaces.
You may know this as the much longer command ip address show
.
ip a
Example output:
[root@web ~]# ip a ... 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 ... inet xxx.xx.xxx.xx/32 brd xxx.xx.xxx.xx scope global eth0 ... 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 ... inet 192.168.0.2/24 brd 192.168.0.255 scope global eth1
2. List non-hidden files and subfolders in the current directory.
Use -R
for recursive, -a
to include hidden files or -l
to use per-line listing format.
ls
example output:
[root@web /]# ls -l total 36 drwx--x--x. 5 root root 76 Aug 11 03:28 backup lrwxrwxrwx. 1 root root 7 Oct 30 2019 bin -> usr/bin dr-xr-xr-x. 5 root root 4096 Jun 26 05:45 boot drwxr-xr-x. 20 root root 3120 Jun 6 06:07 dev drwxr-xr-x. 99 root root 12288 Aug 12 07:40 etc drwxr-xr-x. 8 root root 146 Feb 17 00:04 home ...
3. Display disk space usage.
Use -i
to list inode information instead of block usage. Use -h
to print sizes in powers of 1024 (e.g., 1023M).
df -h
Example output:
[user@server ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 17G 0 17G 0% /dev /dev/mapper/root 313G 161G 153G 52% / /dev/sdb1 1014M 266M 749M 27% /boot ...
4. Display memory usage.
Use -h
to show all output fields automatically scaled to the shortest three digit unit and display the units of print out. Or use -m
to display the amount of memory in mebibytes.
free -m
Example output:
[root@web /]# free -h total used free shared buff/cache available Mem: 32G 2.0G 24G 1.6G 6.1G 28G Swap: 16G 64M 16G
[root@web /]# free -m total used free shared buff/cache available Mem: 33016 2021 24746 1640 6248 28957 Swap: 16639 64 16575
5. Run multiple commands in one line using ;
.
;
Example:
sudo apt update ; apt upgrade
Then optionally you can add the final command it to a bash script.
6. Find large files.
Or install ncdu
and execute from the command line.
find [directory] -size [set minimum size]
Example:
find /home/ -size +1000000k
7. Display a tree of processes.
Add -P
to show PIDs. PIDs are shown as decimal numbers in parentheses after each process name.
pstree -P
Example output:
xxx@host:~$ pstree systemd─┬─accounts-daemon───2*[{accounts-daemon}] ├─agetty ├─apache2───3*[apache2───31*[{apache2}]] ├─atd ├─cron ... ├─networkd-dispat───{networkd-dispat} ├─php-fpm7.4───5*[php-fpm7.4] ...
8. Show listing of last logged in users.
last
Example output:
[root@server ~]# last root pts/0 xxx.xxx.xxx.xxx Wed Aug 12 08:29 still logged in root pts/0 xxx.xx.xxx.xx Wed Jul 29 10:52 - 12:13 (01:21) root pts/0 xxx.xx.xx.xx Mon Jul 27 23:11 - 00:10 (00:58) root pts/0 xxx.xxx.xx.xx Wed Jul 15 23:46 - 00:01 (00:15)
9. Show list of currently logged in user sessions.
w
Example output:
root@host:~$ w 13:08:25 up 72 days, 8:00, 1 user, load average: 0.01, 0.08, 0.08 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT xxxx-user pts/0 xxx.xx.xxx.xx 13:04 0.00s 0.00s 0.00s w
10. Search a file for a pattern of characters, then display all matching lines.
grep
Example:
grep [options] pattern [files]
Example, grep directory recursively:
grep -r "texthere" /home/
Example, grep the word printf:
grep printf /path/filename.txt
Example, find previously used commands which include systemctl
history | grep systemctl
Example, find the last login(s) for username:
last | grep username
For Linux Top Interview Question Check Top 50 Linux Interview Questions and Answers