Useful Terminal Commands You Need to Use NOW!

Mar 30, 2021 · 2 mins read
Useful Terminal Commands You Need to Use NOW!

Linux is loaded with different commands. In this article, I’m just listing the ones that I frequently use all the time.

1. Monitor your PC

Task manager equivalent for Linux

# sudo apt install htop
$ htop

Monitor system temperature

# sudo apt install lm-sensors
$ sensors

Check ram usage

$ free -h

Check system specs

$ lscpu 
$ cat /proc/cpuinfo

Check disk usage

$ df -h

Check file space usage of current directory

$ du -sh *
$ du -h | sort -h

#### Print the file count per directory for the current directory level
$ du -a | cut -d/ -f2 | sort | uniq -c | sort -nr

Check if anyone is logged in

$ who

2. File Operations

Edit a file in the terminal

$ nano file.txt

Scroll through a file

$ less file.txt
# Press 'q' to quit

Clear contents in a fille

$ cat /dev/null > file.txt 

Rename a file to add extension easily

# renames file.md -> file.md.backup
mv /folder/file.md{,.backup}
$ tac file.txt

Tar compression decompression

# Compress a file
$ tar -czvf archive.tar.gz /path/to/directory-or-file

# Extract a file
$ tar -xzvf archive.tar.gz

# z means gzip
# To use bzip2 replace 'z' with 'j'
# gzip is faster but bzip2 makes smaller archives

Sort a file

$ sort < file.txt

Search for files

$ find . -name "*.mp3"
$ find . -type f -size +100M

Live read a system log

$ tail -f /var/log/app.log 
$ tail -f /var/log/app.log | grep ERROR

3. Command execution

Measure time of execution of a program

$ time anyprogram

Cron jobs to execute commands periodically

$ crontab -e

Get help from crontab.guru for making crons easily.

4. Networking

Get websites listed in a file

$ curl $(cat websites.txt)
# or (another form of command substitution)
$ curl `cat websites.txt`

Check occupied ports

$ lsof -i
$ lsof -ni

Port forward remote machine to your machine

$ ssh -L2000:localhost:1313 root@10.100.101.45
# ssh -L{local_port}:localhost:{remote_port} user@remote.ip

5. Wildcards, and Redirects

Remove all files except .html

rm -r !(*.html)

Redirecting

Just remember-

  • 0 - stdin, the standard input stream.
  • 1 - stdout, the standard output stream.
  • 2 - stderr, the standard error stream
Redirect stderr to file
command 2> file
Stderr and stdout to different files
command 2> error.txt 1> output.txt
Suppress error messages
command 2> /dev/null
Redirect stderr to stdout
command > file 2>&1

For wildcards, check the following

  1. Linux wildcards

Conclusion

That seems to be most of the Linux commands that I use all the time. If you have more suggestions to add, comment down below. Check out my other article where I show Terminal tricks to boost your productivity.

Sharing is caring!