20 Essential Unix Command-Line Tricks Every Sysadmin Should Know
A concise collection of practical Unix/Linux command-line tips—including file deletion, output logging, terminal recording, permission handling, and more—designed to boost productivity and simplify common system administration tasks.
1. Quickly delete a large file
Truncate the file without reading its contents, then remove it:
: > /path/to/file.log
rm /path/to/file.log2. Record a terminal session
Use script to capture all input and output. script my.terminal.session Run commands, then end the recording with exit or Ctrl‑D. View the log with more, less or cat.
3. Recreate a deleted /tmp directory
mkdir /tmp
chmod 1777 /tmp
chown root:root /tmp
ls -ld /tmp4. Temporarily lock a directory
Set restrictive permissions and later restore them:
chmod 0000 /downloads # lock
chmod 0755 /downloads # unlock5. Password‑protect a file in Vim
vim +X filename # prompt for password on exit
# or, inside Vim: :X6. Clear a garbled terminal screen
reset7. Produce human‑readable output
Add -h (or -H) to GNU/BSD utilities:
ls -lh df -h(or df -k)
du -h free -b, -k, -m,
-g stat -c %A /boot8. List known user accounts
Linux: lslogins BSD:
logins9. Remove files accidentally extracted from a tarball
cd /var/www/html/
/bin/rm -f "$(tar ztf /path/to/file.tar.gz)"10. Better interactive process viewer
sudo htop11. Repeat the last command
!!– repeat entire command sudo !! – repeat as root !foo – repeat most recent command starting with
foo !$– reuse last argument
12. Schedule a terminal reminder
leave +hhmmReplace hhmm with the desired time (12‑hour clock, interpreted as the next occurrence within 12 hours).
13. Return to the previous or home directory
cd -– go back to the last directory cd – go to $HOME Define shortcut search paths with CDPATH:
export CDPATH=/var/www:/nas10
cd html # jumps to /var/www/html14. Edit a file while viewing it with less
Press v inside less to open the file in the editor defined by $EDITOR.
15. List all directories or files
# List all directories
find / -type d | less
# List directories under $HOME with details
find $HOME -type d -ls | less
# List all files
find / -type f | less
# List files under $HOME with details
find $HOME -type f -ls | less16. Create a full directory tree
mkdir -p /jail/{dev,bin,sbin,etc,usr,lib,lib64}
ls -l /jail/17. Copy a file to multiple destinations
echo /usr/dir1 /var/dir2 /nas/dir3 | xargs -n 1 cp -v /path/to/file18. Compare two directories
diff /tmp/r/ /tmp/s/Use diff to show line‑by‑line differences between the contents of the two directories.
19. Reformat text
fmt file.txt # reflow paragraphs
fmt -s file.txt # split long lines without re‑justifying20. View output while saving to a file
mycoolapp arg1 arg2 input.file | tee my.log teedisplays the command’s output on the terminal and writes the same data to my.log simultaneously.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
