Operations 12 min read

Linux Shell Tips and Tricks: 73 Useful Commands

This article compiles 73 practical Linux shell tips covering network checks, process control, file manipulation, system monitoring, version control, and various command-line shortcuts, providing concise examples and commands to enhance productivity and troubleshooting for system administrators and developers.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Linux Shell Tips and Tricks: 73 Useful Commands

1. Check if a remote port is open for bash: echo >/dev/tcp/8.8.8.8/53 && echo "open" 2. Send a process to the background: Ctrl + z 3. Bring a process to the foreground: fg 4. Generate a random hexadecimal string of n characters: openssl rand -hex n 5. Execute commands from a file in the current shell: source /home/user/file.name 6. Extract the first 5 characters of a variable: ${variable:0:5} 7. SSH debug mode: ssh -vvv user@ip_address 8. SSH with a PEM key: ssh user@ip_address -i key.pem 9. Use wget to download an entire website directory structure to a local folder:

wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs

10. Create multiple directories at once: mkdir -p /home/user/{test,test1,test2} 11. List the process tree including child processes: ps axwef 12. Create a WAR file: jar -cvf name.war file 13. Test disk write speed:

dd if=/dev/zero of=/tmp/output.img bs=8k count=256k; rm -rf /tmp/output.img

14. Test disk read speed: hdparm -Tt /dev/sda 15. Get the MD5 hash of a text string: echo -n "text" | md5sum 16. Check XML format: xmllint --noout file.xml 17. Extract a tar.gz archive into a new directory: tar zxvf package.tar.gz -C new_dir 18. Retrieve HTTP headers with curl: curl -I http://www.example.com 19. Change file or directory timestamp (YYMMDDhhmm): touch -t 0712250000 file 20. Use wget to perform an FTP download: wget -m ftp://username:password@hostname 21. Generate a random 16‑character password:

LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;

22. Quickly backup a file: cp some_file_name{,.bkp} 23. Access a Windows share directory: smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir 24. Execute a command from history (line 100): !100 25. Unzip a package to a directory: unzip package_name.zip -d dir_name 26. Input multiple lines of text (Ctrl + D to exit): cat > test.txt 27. Create an empty file or truncate an existing one: > test.txt 28. Sync time with Ubuntu NTP server: ntpdate ntp.ubuntu.com 29. Show all listening TCP4 ports:

netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'

30. Convert a qcow2 image to raw:

qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img precise-server-cloudimg-amd64-disk1.raw

31. Repeatedly run a command and display its output (default every 2 seconds): watch ps -ef 32. List all users: getent passwd 33. Remount root filesystem as read/write: mount -o remount,rw / 34. Bind‑mount a directory (no symlink): mount --bind /source /destination 35. Dynamically update a DNS server:

nsupdate <<EOF
update add $HOST 86400 A $IP
send
EOF

36. Recursively grep all directories: grep -r "some_text" /path/to/dir 37. List the top 10 largest files:

lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB " $9 }' | sort -n -u | tail

38. Show remaining memory (MB): free -m | grep cache | awk '/[0-9]/{ print $4" MB" }' 39. Open Vim and jump to the end of a file: vim + some_file_name 40. Clone a specific branch (master) with Git: git clone [email protected]:name/app.git -b master 41. Switch to another branch (develop) with Git: git checkout develop 42. Delete a branch (myfeature) with Git: git branch -d myfeature 43. Delete a remote branch with Git: git push origin :branchName 44. Push a new branch to a remote server: git push -u origin mynewfeature 45. Print the last cat command from history: !cat:p 46. Run the last cat command from history: !cat 47. Find all empty sub‑directories under /home/user: find /home/user -maxdepth 1 -type d -empty 48. Show lines 50‑60 of test.txt: < test.txt sed -n '50,60p' 49. Re‑execute the last command with sudo if needed: sudo !! 50. Create a temporary RAM filesystem (ramdisk) of 512 MiB: mount -t tmpfs tmpfs /tmpram -o size=512m 51. Grep whole words: grep -w "name" test.txt 52. Append text to a file with elevated privileges: echo "some text" | sudo tee -a /path/file 53. List all kill signal names: kill -l 54. Prevent the last session from being recorded in bash history: kill -9 $$ 55. Scan a network for open ports: nmap -p 8081 172.20.0.0/16 56. Set Git email address: git config --global user.email "[email protected]" 57. Sync with master when you have unpublished commits: git pull --rebase origin master 58. Move all files containing "txt" in their name to /home/user: find -iname "*txt*" -exec mv -v {} /home/user \; 59. Display files side‑by‑side by columns: paste test.txt test1.txt 60. Show a progress bar for data in a shell pipeline: pv data.log 61. Send data to a Graphite server using netcat:

echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000

62. Convert tabs to spaces: expand test.txt > test1.txt 63. Skip bash history for a command: < space >cmd 64. Return to the previous working directory: cd - 65. Split a large tar.gz archive into 100 MiB parts and later re‑assemble:

split –b 100m /path/to/large/archive /path/to/output/files
cat files* > archive

66. Get HTTP status code with curl: curl -sL -w "%{http_code}\n" www.example.com -o /dev/null 67. Harden MySQL installation and set root password: /usr/bin/mysql_secure_installation 68. When Ctrl + C does not work, use: Ctrl + \ 69. Get the owner of a file: stat -c %U file.txt 70. List block devices with filesystem information: lsblk -f 71. Find files whose names end with a space: find . -type f -exec egrep -l " +$" {} \; 72. Find files whose names contain a tab character: find . -type f -exec egrep -l '\t' {} \; 73. Print a line of 100 equal signs:

printf '%100s
' | tr ' ' =
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.

OperationsLinuxShellcommand-lineUnixTips
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

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.