20 Essential Unix/Linux Command-Line Tricks to Boost Your Productivity
This article compiles twenty practical Unix/Linux command-line techniques—including fast file deletion, output logging, directory restoration, permission locking, password‑protecting files, screen resetting, human‑readable output, user listing, batch removal, process monitoring, command repetition, timed reminders, navigation shortcuts, in‑less editing, directory tree creation, multi‑directory copying, diff comparison, text formatting, and tee usage—each illustrated with exact commands to enhance terminal efficiency.
1. Delete a large file
If a massive log file (e.g., 200 GB) makes rm or ls unresponsive, truncate it before removal:
> /path/to/file.log
# Or use the shell redirection syntax
: > /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.terminal.session Run any commands (e.g., ls, date, sudo service foo stop) and then exit the session with exit, logout, or Ctrl‑D. The recorded file can be viewed with more, less, or cat.
3. Restore a deleted /tmp directory
Recreate the standard temporary directory with correct permissions and ownership:
mkdir /tmp
chmod 1777 /tmp
chown root:root /tmp
ls -ld /tmp4. Lock a folder
To deny access to /downloads for non‑root users, set restrictive permissions: chmod 0000 /downloads Root can still access it; to restore normal access:
chmod 0755 /downloads5. Password‑protect a file in Vim
Open a file with encryption using Vim’s +X flag: vim +X filename Alternatively, after editing, execute :X inside Vim to set a password.
6. Clear garbled screen output
Reset the terminal display:
reset7. Human‑readable format for common commands
Append -h or -H to GNU/BSD utilities to get sizes in K, M, G, etc.
ls -lh
# human‑readable sizes
df -h
df -k
free -b
free -k
free -m
free -g
du -h
stat -c %A /boot
sort -h -a file
lscpu
lscpu -e
lscpu -e=cpu,node
tree -h
tree -h /boot8. Show known user information
On Linux, use lslogins; on BSD, use logins:
lslogins
loginsSample output lists UID, username, password lock status, last login, etc.
9. Remove files accidentally extracted in the wrong directory
If a tarball was extracted under /var/www/html instead of the intended project directory, 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 htop11. Re‑run the previous command
Use !! to execute the last command, optionally with sudo:
!!
sudo !!Run the most recent command starting with a specific prefix:
!foo
sudo !serviceUse !$ to repeat the last argument of the previous command:
sudo vi !$12. Set a timed reminder to leave the terminal
Schedule a reminder with the leave command (time format hhmm, 12‑hour clock assumed):
leave +hhmm13. Quick directory navigation shortcuts
Return to the previous directory: cd - Go directly to the home directory: cd Define a search path for cd with CDPATH: export CDPATH=/var/www:/nas10 Now cd html jumps to /var/www/html without typing the full path.
14. Edit a file while viewing it with less
Press v inside less to open the file in the editor specified by $EDITOR. After editing, return to less to continue browsing.
15. List all directories or files on the system
List every directory and pipe through less:
find / -type d | less
find $HOME -type d -ls | lessList every regular file similarly:
find / -type f | less
find $HOME -type f -ls | less16. Create a directory tree in one command
Use mkdir -p with brace expansion:
mkdir -p /jail/{dev,bin,sbin,etc,usr,lib,lib64}
ls -l /jail/17. Copy a file to multiple directories at once
Instead of issuing several cp commands, feed the target directories to xargs:
echo /usr/dir1 /var/dir2 /nas/dir3 | xargs -n 1 cp -v /path/to/file18. Quickly find differences between two directories
Use diff on directory paths:
diff /tmp/r/ /tmp/s/19. Reformat text with fmt
Wrap paragraphs to a uniform width: fmt file.txt To split long lines without re‑flowing short ones:
fmt -s file.txt20. View output while writing it to a file
Pipe a command through tee to display on the screen and simultaneously log to a file:
mycoolapp arg1 arg2 input.file | tee my.log teeensures the program’s output is both visible and saved.
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.
