Operations 12 min read

20 Essential Unix/Linux Command‑Line Tricks Every Sysadmin Should Know

This guide compiles twenty practical Unix/Linux command‑line techniques—from safely deleting massive log files and recording terminal sessions to comparing directories, formatting text, and using tee for simultaneous output—providing sysadmins with quick, reliable shortcuts for everyday system management tasks.

21CTO
21CTO
21CTO
20 Essential Unix/Linux Command‑Line Tricks Every Sysadmin Should Know

1. Delete a large file

When a huge log file (e.g., 200 GB) makes rm or ls hang, truncate it first and then remove it: > /path/to/file.log or : > /path/to/file.log Finally delete it with:

rm /path/to/file.log

2. Record terminal output

Use the script utility to capture everything displayed in the terminal: script my.terminal.session Run any commands (e.g., ls, date, sudo service foo stop) and finish the recording with exit, logout or Ctrl‑D. View the log with more, less or cat on my.terminal.session.

3. Restore a deleted /tmp directory

If /tmp is accidentally removed, recreate it with the proper permissions:

mkdir /tmp
chmod 1777 /tmp
chown root:root /tmp

Verify with ls -ld /tmp.

4. Lock down a folder

To deny access to /downloads for privacy, set permissions to 0000. The root user can still access it; to restore normal access use:

chmod 0755 /downloads

5. Password‑protect a file in Vim

Encrypt a file directly from Vim: vim +X filename Or, before exiting Vim, run the :X command; Vim will prompt for a password.

6. Clear garbled screen output

Reset the terminal with:

reset

7. Human‑readable formats

Pass -h or -H to GNU/BSD utilities for easier size units:

ls -lh
df -h

# e.g., 1K 234M 2G

df -k
free -b

# bytes

free -k
free -m
free -g
du -h
stat -c %A /boot

# permissions

sort -h -a file
lscpu

# CPU info

lscpu -e
lscpu -e=cpu,node
tree -h

# human‑readable sizes

tree -h /boot

8. Show known user information

Linux: lslogins BSD: logins Typical output includes UID, username, password lock/deny flags, last login, and GECOS fields.

9. Remove accidentally extracted files

If a tarball was unpacked in the wrong directory (e.g., /var/www/html), navigate there and delete the extracted files without touching other content:

cd /var/www/html/
/bin/rm -f "$(tar ztf /path/to/file.tar.gz)"

10. Replace top with htop

For a more user‑friendly process viewer, run:

sudo htop

11. Re‑run the previous command

Use the history shortcut !! to execute the last command again. Variations: sudo !! # run as root !foo # run most recent command starting with “foo” sudo !service # run last command beginning with “service” as root !$ # last argument of previous command (e.g., sudo vi /etc/nginx/nginx.conf then sudo vi !$)

12. Set a leave‑me‑alone reminder

Schedule a terminal reminder with: leave +hhmm where hhmm is the time (12‑ or 24‑hour format) within the next 12 hours.

13. Quickly return to the previous directory

Use: cd - To jump to your home directory: cd Define CDPATH to shorten navigation, e.g.: export CDPATH=/var/www:/nas10 Now cd html jumps directly to /var/www/html.

14. Edit a file while viewing it with less

Press v inside less to open the file in the editor defined by $EDITOR (e.g., vim).

15. List all files and directories

All directories: find / -type d | less All directories under $HOME with details: find $HOME -type d -ls | less All files: find / -type f | less All files under $HOME with details:

find $HOME -type f -ls | less

16. Create a directory tree in one command

Use mkdir -p with brace expansion: mkdir -p /jail/{dev,bin,sbin,etc,usr,lib,lib64} Verify with ls -l /jail/.

17. Copy a file to multiple directories

Instead of repeating cp, pipe the target list to xargs:

echo /usr/dir1 /var/dir2 /nas/dir3 | xargs -n 1 cp -v /path/to/file

18. Quickly find differences between two directories

Use diff on the directories:

diff /tmp/r/ /tmp/s/
Finding differences between folders
Finding differences between folders

19. Reformat text

Use fmt to wrap paragraphs: fmt file.txt To split long lines without re‑justifying, add -s:

fmt -s file.txt

20. View output while writing it to a file

Pipe a command through tee to see the stream on screen and save it simultaneously:

mycoolapp arg1 arg2 input.file | tee my.log
tee

ensures the output appears on the terminal and is written to my.log.

Original source: http://www.cyberciti.biz/open-source/command-line-hacks/20-unix-command-line-tricks-part-i/ Translation by LCTT, translator: geekpi
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxUnixSysadminTips
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.