Linux commands you should know
Tags: linux, beginners, bash, tutorial
Published: 2023-11-21 15:39:42

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
```

## Stop and move a process to the background

```
# Press ctrl+z and type the following:
bg
```

## 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.

```bash
nohup python run_my_awesome_script.py &
```

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:

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

```bash
# ScreenNameHere - optional - if you have only one open screen.
screen -r ScreenNameHere
``` 

## What is running on port XYZ?

```bash
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
```

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.

```bash
find /var/log/nginx -type f -mmin +180 -exec rsync -av {} /backups/nginx/logs \;
```

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

```bash
rsync -rav -e 'ssh -p 9023' --progress /var/lib/mysql/ root@192.168.1.1:/var/lib/mysql2/
```
Simple copy - if you just want to copy files between two machines and don't care for incremental syncs.

```bash
scp -P9023 -r somefiles/ root@192.168.1.1:/tmp/
```

## Compress and decompress files

```bash
 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
```

## Metrics

```bash
# 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
```

## 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](https://dev.to/kwnaidoo/linux-server-essentials-for-web-developers-49ni).

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.