English original link: https://www.lopezferrando.com/30-interesting-shell-commands/
1. Monitor command (runs every 2 seconds)
watch "ls -larth"
2. Use a port to kill the program
sudo fuser -k 8000/tcp
3. Limit the memory usage of the following commands
ulimit -Sv 1000 # 1000 KBs = 1 MB
ulimit -Sv unlimited # Remove limit
4. Rename selected files using regular expressions
rename 's/\.bak$/.txt/' *.bak
5. Get the full file path
readlink -f file.txt
6. List the contents of the tar.gz file and extract only one file
tar tf file.tgz
tar xf file.tgz filename
7. List files by file size
ls -lS
8. Traceroute
mtr google.com
9. Tips for finding files
find . -size 20c # By file size (20 bytes)
find . -name "*.gz" -delete # Delete files
find . -exec echo {} \; # One file by line ./file1 ./file2 ./file3 find . -exec echo {} \+ # All in the same line ./file1 ./file2 ./file3
10. Print infinite loop of text
yes
yes hello
11. Currently logged in user
w
12. Output result prepended line number
ls | nl
13. Grep uses Perl-style syntax ( \t
characters like this are allowed)
grep -P "\t"
14. Cat command reverse output (start from end)
tac file
15. Check the permissions of files in each directory
It is useful to detect permission errors, for example when configuring a web server.
namei -l /path/to/file.txt
16. Execute a command every time a file is modified
while inotifywait -e close_write document.tex
do
make
done
17. Copy to clipboard
cat file.txt | xclip -selection clipboard
18. Spelling and Grammar Checker in Latex
detex file.tex | diction -bs
You may need to install the following: sudo apt-get install diction texlive-extra-utils
.
19. Check resource usage
/usr/bin/time -v ls
20. Random lines of a file
cat file.txt | sort -R
cat file.txt | sort -R | head # Pick a random sambple
# Even better (suggested by xearl in Hacker news):
shuf file.txt
21. Keep program running after leaving SSH session
If the program does not require any interaction:
nohup ./script.sh &
If you need to type something manually, then leave:
./script.sh
<Type any input you want>
<Ctrl-Z> # send process to sleep
jobs -l # find out the job id
disown -h jobid # disown job bg # continue running in the background
screen
Of course, or can also be used tmux
for this purpose.
22. Run a command for a limited time
timeout 10s ./script.sh
# Restart every 30 minutes
while true; do timeout 30m ./script.sh; done
23. Merge lines from two sorted files
comm file1 file2
Print these three columns:
- row
file1
unique. - row
file2
unique. - Both in row
file1
andfile2
in row.
These columns can be dropped using the options -1
, -2
, .-3
split long file in file with same number of lines
split -l LINES -d file.txt output_prefix
25. Refresh the swap partition
If a program consumes too much memory, the swap partition fills up with the remaining memory, and when you go back to normal, everything is slow. Just reboot the swap partition to fix it:
sudo swapoff -a
sudo swapon -a
26. Fix superblock problem of ext4 file system
sudo fsck.ext4 -f -y /dev/sda1
sudo fsck.ext4 -v /dev/sda1
sudo mke2fs -n /dev/sda1
sudo e2fsck -n <first block number of previous list> /dev/sda1
27. Create empty file of given size
fallocate -l 1G test.img
28. Manipulate PDF files from command line
It is a better command to use than the join
, shuffle
, select
etc commands:pdftk
pdftk *.pdf cat output all.pdf # Join PDFs together
pdftk A=in.pdf cat A5 output out.pdf # Extract page from PDF
cpdf
Action content is also available :
cpdf -draft in.pdf -o out.pdf # Remove images
cpdf -blacktext in.pdf -o out.pdf # Convert all text to black color
29. Monitor progress based on generated output
# Write random data, encode it in base64 and monitor how fast it
# is being sent to /dev/null
cat /dev/urandom | base64 | pv -lbri2 > /dev/null
# pv options: # -l, lines # -b, total counter # -r, show rate # -i2, refresh every 2 seconds
30. Find a file's package in Ubuntu
apt-file update
apt-file search dir/file.h