20 Essential Unix Command‑Line Tricks Every Sysadmin Should Know
This guide presents twenty practical Unix shell techniques—from safely deleting massive log files and recording terminal sessions to comparing directories, formatting output, and using shortcuts like !! and $!—providing clear commands and examples that help administrators work more efficiently and avoid common pitfalls.
1. Delete a large file safely
When a huge file (e.g., 200 GB) makes rm or ls hang, truncate it first, then remove it.
: > /path/to/file.log
rm /path/to/file.log2. Record terminal output
Use the script utility to capture everything displayed in the terminal.
script my.session
# …run commands…
exit # or Ctrl‑D to stop recordingInspect the session file with more, less or cat.
3. Re‑create a missing /tmp directory
If /tmp is removed, recreate it with the correct permissions and ownership.
mkdir /tmp
chmod 1777 /tmp
chown root:root /tmp
ls -ld /tmp4. Lock a directory from ordinary users
Set the directory mode to 0000 so non‑root users cannot list or enter it. Root can still traverse. chmod 0000 /downloads Restore normal access with:
chmod 0755 /downloads5. Password‑protect a file in vim
Open a file with encryption enabled: vim +X filename Or, after editing, type :X inside vim to set a password.
6. Clear garbled characters on the screen
Run the reset command to restore a clean terminal state.
reset7. Produce human‑readable sizes
Pass -h (or -H) to GNU/BSD utilities such as ls, df, du, free, stat, sort, lscpu, tree to display sizes in KiB, MiB, GiB, etc.
ls -lh
df -h
free -h
stat -c %A /boot
lscpu -e
tree -h /boot8. Show known user information
On Linux use lslogins; on BSD use logins to list users, UID, login times and other fields.
lslogins
logins9. Remove accidentally extracted files
If a tarball was unpacked in the wrong directory, delete the extracted files by feeding the tarball’s file list to rm:
cd /var/www/html/
/bin/rm -f "$(tar ztf /path/to/file.tar.gz)"10. Replace top with htop
htopprovides an interactive, colour‑rich view of processes.
sudo htop11. Re‑run the previous command
Enter !! to execute the last command again. Prefix with sudo to run it as root.
!!
sudo !!To repeat the most recent command that starts with a specific word, use !foo. Example: sudo !service runs the last service command as root.
12. Use the last argument of the previous command
The !$ expansion inserts the last argument of the preceding command.
sudo vi /etc/nginx/nginx.conf
/sbin/nginx -t -c /etc/nginx/nginx.conf
sudo vi !$13. Schedule a leave‑the‑terminal reminder
Use the leave command with a time in hhmm (12‑ or 24‑hour format).
leave +093014. Return to the previous directory
Use cd - to go back to the directory you were in before the last cd.
cd -15. Jump directly to your home directory
Simply type cd without arguments. cd Define CDPATH to shorten navigation, e.g. export CDPATH=/var/www:/nas10 lets you reach /var/www/html with cd html.
16. Create a directory tree in one command
Use mkdir -p with brace expansion to create multiple nested directories.
mkdir -p /jail/{dev,bin,sbin,etc,usr,lib,lib64}
ls -l /jail/17. Copy a file to many directories at once
Pipe a list of target directories to xargs instead of issuing several cp commands.
echo /usr/dir1 /var/dir2 /nas/dir3 | xargs -n 1 cp -v /path/to/file18. Quickly find differences between two directories
Use diff on the directory paths to see line‑by‑line differences.
diff /tmp/r/ /tmp/s/19. Reformat text files
The fmt command wraps paragraphs to a uniform width. Use -s to split long lines without re‑flowing short ones.
fmt file.txt
fmt -s file.txt20. View output while writing it to a file
Pipe a command through tee to display its output on the screen and simultaneously save it.
mycoolapp arg1 arg2 input.file | tee my.logOriginal source: http://www.cyberciti.biz/open-source/command-line-hacks/20-unix-command-line-tricks-part-i/
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.
