Linux CheatSheet
?
R
BashList of commonly used commands on Linux and macOS. Ping, DNS, Compression, Root, etc.
1# Update DNS Servers
2echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.conf
3echo "nameserver 8.8.4.4" | sudo tee -a /etc/resolv.conf
4
5# Check DNS on MacOS
6scutil --dns | grep 'nameserver\[[0-9]*\]'
7
8# Create a text file without a text editor
9echo "I can \"write\" without double quotes" >> file.txt
10
11
12# Check filesize via Curl
13curl -sI https://coding-cloud.com | grep -i Content-Length
14
15
16# Change timezone to UTC
17sudo ln -sf /usr/share/zoneinfo/UTC /etc/localtime
18
19# Find a folder
20 find . -name ".staging"
21
22# Check the size of a directory
23du -hs /path/to/directory
24sudo du -sh ./* | awk -F '\t' '$1 ~ /G$/'
25
26# Count files inside folders (recursively)
27find . -type f | wc -l
28
29# Check the name of the Distro
30cat /etc/issue
31
32# Disable auto updates
33systemctl disable --now apt-daily{,-upgrade}.{timer,service}
34
35# Install Vim
36apt-get update && apt-get install vim -y
37
38# take ownership of everything in your ~/.npm directory (must be run with sudo):
39sudo chown -R $USER:`id -g -n $USER` ~/.npm
40
41# optionally check to see if anything is owned by root:
42ls -la ~/.npm
43
44# Folder size check (depth 1)
45du -h -d 1
46
47# Unzip a file
48tar -xvf {file.zip} -C /dest/directory/
49
50# Check Byte Size of a file **S3 also uses this method of counting letters
51wc -c < impressions-010618.gz
52
53# Rename all files (.js to .ts)
54for f in *.js; do mv $f `basename $f .js`.ts; done;
55
56# Find string inside files in folder
57grep -R --exclude-dir=node_modules --exclude-dir=.history 'POST' .
58
59# Print hostname
60hostname
61
62# Find file by partial name
63find . -name "*5b489cfa3dd81f000f50fd3b*"
64
65# Check system Uptime
66uptime
67
68# Check IP Address
69 curl -s http://checkip.dyndns.org/
70
71# Power Off Machine
72shutdown -h now
73
74# Check how many Node processes are running
75 ps -aef | grep 'node' | wc -l
76
77# Check how much memory is free (MB)
78free -h
79
80# Remove all files in current folder
81rm -rf *
82
83# Remove all files and folders
84rm -rf /path/to/directory/{*,.*}
85
86# Kill any process by name
87sudo pkill java
88
89# Kill any process using port
90kill -9 $(lsof -t -i:3232)
91
92# Check if SSH Agent is enabled
93eval "$(ssh-agent -s)"
94
95# GIT Permission Denied (debug and heal ssh agent configs)
96ssh -vT ubuntu@xx.yyy.zzz
97
98# Show SSH Keys
99ssh-add -L
100
101# Remove all files except one folder (bash only)
102shopt -s extglob
103rm -- !(default)
104
105# Check with Shell is running
106echo $0
107
108# Check which Distribution is running
109cat /etc/*release
110
111# Restart Shell
112exec $SHELL -l
113
114# Find a file
115sudo find / -type f -iname "*.pem"
116
117# Find strings in files
118grep -Ril "petstore" .
119
120# Find Files with Inode info
121sudo find . -xdev -type f | cut -d "/" -f 2 | sort | uniq -c | sort -n
122
123# Chmod on current folder, subfolders and files
124chmod -R 775 .
125
126# Change permissions to only current user
127chmod 600 ~/revocation.crt
128
129# View lines within range
130sed -n 5,8p file
131
132# View specific line number of a file
133sed -n 10p access.log
134
135# View first line of a file
136head -1 access.log
137
138# Count entries on access.log file
139cut -d'"' -f3 access.log | wc -l
140
141# Enter into a root shell
142sudo -i
143whoami
144
145#Write on a read only file
146:w !sudo tee %
147
148#Change ownership of folder and files for current user
149sudo chown -R $(whoami) data
150
151# Identify IP Address with ping
152ping -c 1 coderecipes.org
153
154# Ping with interval
155ping -i 5 8.8.8.8
156
157# View DNS of a domain
158nslookup
159set type=mx, set type=cname, ..
160coderecipes.org
161
162# Update Package Manager
163apt-get update
164
165# Install ping
166apt-get update
167apt-get install -y iputils-ping
168
169# View all Environment Variables
170env
171
172# View DNS registar info
173dig www.coderecipes.org
174
175# Add SSH Key and Restart SSH Service (Mac)
176ssh-add coderecipes.pem
177sudo launchctl stop com.openssh.sshd
178
179# Exit screen session
180Ctrl-a Ctrl-d
181
182# Create a command alias
183alias dps="docker ps"
184
185# Count Files in Folder
186ls -1 | wc -l
187
188# Ping 10 times per second
189ping -n -i 0.1 8.8.8.8
190
191# Ping 5 times
192ping -c 5 8.8.8.8
193
194# View Tarball file contents
195tar -tf ubuntu-test1.tar
196
197# View a File size in MB
198ls -lh /tmp/ubuntu-test1.tar
199
200# CPU Utilization report, the Device Utilization report and the Network Filesystem report
201iostat
202
203# Write a text file without an editor (eg. a docker stripped linux distro)
204echo 'content' > /tmp/test.txt
205
206# View Disk Space
207df -H
208
209# Zip the current Folder
210zip -r myzfile.zip .
211
212# Switch user to root
213sudo su
214
215# Check Kernel Version
216uname -a
217
218# Print all Environment Variables
219printenv
220
221# List all running processes
222ps -elf
223
224# Ping Google DNS Service
225ping 8.8.8.8
226
227# View Hosts
228cat /etc/hosts
229
230# Create an Environment Variable
231export DOCKER_HOST="tcp://192.168.1.0"
232
233# Print Environment Variable
234printenv | more
235
236# View all Groups
237cat /etc/group
238
239# Check who is listening on a port (macOS)
240lsof -n -i4TCP | grep LISTEN
241lsof -n -i | grep LISTEN
242lsof -i -n -P | grep TCP
243
244
245# Other commands
246kill -9 PID //kill process
247ps a //show all running processes on mac / linux
248
249# Sort processes by memory usage
250ps aux --sort -rss
251Created on 11/16/2017