Linux Commands Cheat Sheet

A collection of linux commands oriented towards server setup and maintenance.

Tar & Zip

This unpacks a tar file into a folder called “packagename”:
tar -xvzf packageName.tar.gzCode language: Bash (bash)
Add folders and files one by one to a zip file called “artifacts.zip”:
zip -rm artifacts.zip dist
zip -rm artifacts.zip server
zip -rm artifacts.zip package.json
zip -rm artifacts.zip package-lock.jsonCode language: Bash (bash)

File Transfer

Download file from internet with curl (-o is the destination on your machine):
curl -o file.tar.gz https://example.com/file.tar.gzCode language: JavaScript (javascript)
Copy down single file from server:
sudo scp -i [PATH TO PEM KEY] [USERNAME]@[REMOTE-HOST]:[REMOTE-PATH]/file.ext ./[LOCAL-PATH]/
Copy down entire directory:
sudo scp -i [PATH TO PEM KEY] -r [USERNAME]@[REMOTE-HOST]:[REMOTE-PATH] .

File/Directory Operations


Removes all node_modules folders recursively:
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +Code language: JavaScript (javascript)

Permissions

Make file executable:
sudo chmod +x /pathtofile.sh

Users

List all users:
less /etc/passwd
List all groups:
less /etc/group

About Your Machine and Its Resources

This tells you what version of linux or at least linux kernel you are running:
uname -a
Learn more about your machine’s memory:
free -m
top
htop
Check available space on the hard drive partition:
df -h
Is any swap configured:
sudo swapon -sCode language: Bash (bash)
How often your system swaps data out of RAM to the swap space?
cat /proc/sys/vm/swappiness
How much the system will choose to cache inode and dentry information over other data?
cat /proc/sys/vm/vfs_cache_pressure

Network/DNS Inspection

dig google.comCode language: Bash (bash)
nslookup google.comCode language: Bash (bash)

Processes

Find a process and kill it:

Run this ‘ps’ command to find the process, in this case the MySQL Daemon “mysqld”

ps -ax | grep mysqld

On a Mac, you will likely get output that looks like this:

  114 ??         3:59.55 /usr/local/mysql/bin/mysqld –user=_mysql –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data –plugin-dir=/usr/local/mysql/lib/plugin –log-error=/usr/local/mysql/data/mysqld.local.err –pid-file=/usr/local/mysql/data/mysqld.local.pid –port=3306

Then, run kill with the process ID of the process you wan to kill:

kill -9 processId

In this case:

kill -9 114

Systemd Services

Service Start/Stop:
sudo systemctl restart some-service
sudo systemctl start some-svc
sudo systemctl stop some-svc
If you made edits to a service file, run this first before restarting:
sudo systemctl daemon-reload
Learn about a running service’s status, read latest log output:
sudo systemctl status some-svc -n200
Get output of all services:
sudo journalctl -xe
Get output of just the service you are interested in, in reverse chronological order (last entry at top):
sudo journalctl -r -u some-svcCode language: Shell Session (shell)
Get output of just the service you are interested in, for the current boot only:
sudo journalctl -b -u some-svcCode language: Shell Session (shell)
Get output of just the service you are interested in, in real time (follow mode):
sudo journalctl -f -u some-svcCode language: Shell Session (shell)
Set up a service to start on machine start up:
sudo systemctl enable some-svc
Edit a service:
sudo systemctl edit --full some-svc.serviceCode language: Bash (bash)

Edit/List Cron Jobs

crontab -e
crontab -l

Environment

Print all environment variables:
printenv
Print one environment variable:
echo $PATHCode language: Bash (bash)

Leave a Reply

Your email address will not be published. Required fields are marked *