DEV Community

Kevin Naidoo
Kevin Naidoo

Posted on • Updated on • Originally published at kevincoder.co.za

Linux commands you should know

I use open-source tooling which predominantly runs on Linux servers, therefore often I find myself in the terminal running commands to do various tasks.

In this guide - I'm going to give you a cheat sheet(sort of) of some useful and interesting commands I use often.

Look inside a process

Have a running process and no idea what it's doing?

Using "strace", you can easily peek inside a running process and output its log info using the below command:

sudo strace -p 3612485 -e write
Enter fullscreen mode Exit fullscreen mode

Stop and move a process to the background

# Press ctrl+z and type the following:
bg
Enter fullscreen mode Exit fullscreen mode

Bad internet?

Want to run a command in a remote terminal but scared that a disconnect might stop the process prematurely?

There are two options for this, "nohup" or "screen". Screen opens the terminal in a window so even if your network disconnects - the screen will still persist.

The "nohup" command does something similar except it keeps the process running even if your network disconnects and writes to a log file.

nohup python run_my_awesome_script.py &
Enter fullscreen mode Exit fullscreen mode

We use the "&" to push the process to the background immediately so that we can continue working in the terminal. Omitting this will basically lock your terminal on that command until it finishes.

To use screens:

screen -S MyScreen
Enter fullscreen mode Exit fullscreen mode

To exit and keep the screen open - enter "ctrl+a+d". If you want to go back to the screen:

# ScreenNameHere - optional - if you have only one open screen.
screen -r ScreenNameHere
Enter fullscreen mode Exit fullscreen mode

What is running on port XYZ?

lsof -i :5000

# Or
netstat -tupln | grep :5000

# Or - this one may not always give you the best results
# - as it also searches process IDs

ps aux | grep 5000
Enter fullscreen mode Exit fullscreen mode

Will generate a list of processes that are using the specified port.

Sync and copy files

Rsync is a very powerful tool for syncing files incrementally, I won't go too in-depth on what the flags mean - please check the manual, however, I will cover some common use cases for rsync.

Find and copy log files that are older than 180 minutes to a backup directory.

find /var/log/nginx -type f -mmin +180 -exec rsync -av {} /backups/nginx/logs \;
Enter fullscreen mode Exit fullscreen mode

Sync all files in directory /var/lib/mysql to a remote server. Set the SSH port to "9023"

rsync -rav -e 'ssh -p 9023' --progress /var/lib/mysql/ root@192.168.1.1:/var/lib/mysql2/
Enter fullscreen mode Exit fullscreen mode

Simple copy - if you just want to copy files between two machines and don't care for incremental syncs.

scp -P9023 -r somefiles/ root@192.168.1.1:/tmp/
Enter fullscreen mode Exit fullscreen mode

Compress and decompress files

 tar -czvf somestuff.tar.gz somestuff/

# And unpack
tar -xzvf somestuff.tar.gz

# Compress a single file
gzip -9 data.xml

# Uncompress single file
gzip -d data.xml.gz
Enter fullscreen mode Exit fullscreen mode

Metrics

# Open system monitor tool
htop

# View memory usage
free -m

# View processes
ps aux

# View CPU information
cat /proc/cpuinfo

# Disk space usage
df -h

# File/directory sizes in the current directory
du -h

# To 20 biggest files & folders in directory
du -h | sort -rh | head -n 20
Enter fullscreen mode Exit fullscreen mode

Conclusion

These just scratch the surface of useful Linux commands that you will use often.

I didn't cover "grep", "awk", "sed" and so on, however, I do have an earlier article that does cover some of these - which you can check out here.

Hopefully, this is useful to you, please feel free to comment down below if you have any other suggestions or would like a future article on a specific area you would like me to cover.

Top comments (2)

Collapse
 
moopet profile image
Ben Sinclair

To exit and keep the screen open - enter "ctrl+alt+d"

This is a typo, it should be <ctrl-a><d> as two separate keypresses.

Collapse
 
kwnaidoo profile image
Kevin Naidoo

Thanks so much for spotting this. I will fix.