Commonly Used Linux Commands Reference

Some commonly used Linux commands.

View the number of files in a directory

ls -l | wc -l

View the number of files in a directory and its subdirectories

find . -type f | wc -l

View the number of lines in a file

wc -l file.txt

Find file content and display a few lines before and after

grep -C 5 'hello' file.txt

Find the first few lines of a file

head -n 100 file.txt

Find the last few lines of a file

tail -n 100 file.txt
tail -f file.txt
tail -f -n 50 file.txt

View text files page by page

less file.txt
- Space or Enter or Down Arrow to scroll down
- b to scroll up
- q to quit
- /keyword to search down
- ?keyword to search up

more, can only scroll down

more file.txt
- Space or Enter to scroll down
- q to quit
- /keyword to search down

cat through pipe for pagination

cat file.txt | less
cat file.txt | more

Split files

Split by size:

split -b 100m -d --additional-suffix=.txt file.txt part_

Split by number of lines:

split -l 10000 -d --additional-suffix=.txt file.txt part_

Find the process on port 8082 and kill it

lsof -ti :8082 | xargs kill -9

No need to find the process by port first, then kill by PID

Directly kill Java-related processes

pkill -9 java

View ports

netstat -ntlp
lsof -i:8080

Test if a port is open

telnet example.com 8082

Check disk space

df -h     # Check disk space for all paths
du -sh *  # Check size of all files or directories in the current directory
du -sh .  # Check total size of the current directory

Check memory usage

free -h
free -s 2  # Refresh every two seconds
free -s 2 -c 5  # Refresh 5 times

Check GPU memory usage

nvidia-smi
watch -n 1 nvidia-smi

Compression/Decompression usage

zip archive.zip file1.txt file2.txt
zip -r myfolder.zip myfolder/  # Compress entire directory
zip -e secret.zip demo.txt  # Encrypt
zip -u existing.zip newfile.txt

Decompress:

unzip archive.zip
-d  # Specify directory
-l  # View only without decompressing
-o  # Overwrite
-x  # Exclude files

curl test HTTP requests

curl -X POST \
     -H "Content-Type: application/json" \
     -H "Authorization: Token abc123" \
     -u elastic:123456 \
     -d '{"query": {"match": {"_id": "584338" }}}' \
   'http://localhost:8082/test/_search'

Just a simple record for reference.

Leave a Comment