My Linux Command Cheatsheet

Basics
mv oldfilename newfilename ### rename file
mv *identifier* /path/ ### move multiple files
cp /path/filename /path ### copy file to new location
./ ### current directory
../ ### move back one directory from current directory
pwd ### prints path
find / -name filename ### find file, looking through whole system
find / -name *.txt ### find all files with specific file extension
find / -type d -name “*jvm*” ### find directory name
find / -name “*.jar” -type f -mmin +$((60*24)) -exec ls -halt {} + | head -10 ### find the last 10 most recently edited har files older that 24 hours
find / -name main.jar -type f -delete; ### find and delete all files with filename
find / -type f -mtime -2 ### all files edited in last 2 days
rm -r -f /path/to/directory ### delete whole directory including all files without prompt
rm -f -R — */ ### removes all folders in current directory without prompt
ll -lt ### list all files in directory with details, can also just use ll
ls ### just list filenames in directory
du -a / | sort -n -r | head -n 30 ### show top 30 directories using disk space

ls -l –block-size=M ### list files in MB in current folder
ls -lh ### list files in kb, mb, gb for current folder… better one!
firewall-cmd –add-port=9990/tcp
firewall-cmd –list-all
less /path/to/filename ### basic editor
cat /path/to/filename ### outputs content of file
ps -aux | grep tomcat ### find process id of some process whose name you know

netstat -tulpn ###
history | tail -20 ### out the 20 most recent items from the linux command history
whereis ###
echo $PATH | tr ‘:’ ‘\n’ ### list the content of path, dividing into individual lines

sed
sed -i ‘/pattern to match/d’ /path/to/filename ###

cd /proc/7175/fd ### then ll to see all the connections…. fd is a directory..
### The set of file descriptors open in a process can be accessed under the path /proc/PID/fd/, where PID is the process identifier, which is really cool way to find the logs for any process is writing to

namei -m /var/log/tomcat/errors.log ### show the file permissions of all the folders down to the file

chown tomcat:tomcat /path/to/file ### change owner of file
chmod 777 /path/to/file ### grant read write execute permissions to all users for given file/folder
0: (000) No permission.
1: (001) Execute permission.
2: (010) Write permission.
3: (011) Write and execute permissions.
4: (100) Read permission.
5: (101) Read and execute permissions.
6: (110) Read and write permissions.
7: (111) Read, write, and execute permissions.

cat /etc/os-release ### OS info
lscpu ### CPU info

grep “some text” /path/to/file ### find all instances of given text in file
grep -Ril “some text” / ### find all files with instances of the text, just list the files, not the lines of text
grep -rnw “php” ### r or -R is recursive, -n is line number, and -w stands for match the whole word… can add l for just the file name
printenv | grep ‘AWS\|VAULT’ ### grep or condition \|
egrep ### grep -E
grep -A 3 “test” ### 3 lines after match
grep -B 3 “test” ### 3 lines before match
grep -C 3 “test” ### 3 lines before and after
grep -r “test” /path/to/dir ### grep search all files in a dir
grep -rh “test” /path/to/dir ### grep search all files in a dir but don’t output filename
grep -rl “test” /path/to/dir ### grep search all files in a dir and only output the filenames with matches

tail -f /var/log/tomcat/catalina.log ### show new text being added to the file live
tail -f /var/log/tomcat/MSTR.log | grep -E -v ‘systemd|amazon-ssm-agent|dhclient|UserState’ ### tail everything except the lines with keywords listed

Ctrl + Z ### stops current command executing
Ctrl + C ### exits cursor from current executing command
Ctrl + r ### loop through history of commands
Ctrl + l ### clear terminal window, can also type ‘clear’

nslookup – query the dns for resource records
yum install -y sysstat >> /dev/NULL;
iostat; ### Report Central Processing Unit (CPU) statistics and input/output statistics for devices and partitions
iostat -d 2 15 ### shows device report every 2 seconds, 20 times.
mpstat -P ALL 2 ### output every 2 seconds, CPU core usage per core
top ### shows list of processes, press capital E while in top to change data sizes kb -> mb -> gb, small e changes it for the individual processes
htop is better though, yum install htop, ### might need to run first yum install epel-release
df -h ### hardrive capacity and usage in readable format
free -m ### shows ram usage in mb

tar -xvf snowflake_linux_x8664_odbc-2.20.5.tgz ### unzip file

ping google.com 1> /dev/null ### redirect successes to the dev null file
grep -r hello /sys/ 2> /dev/null ### redirect errors to the dev null file

### generate json with sleep
yum install ksh;
awk -v n=100 -v seed=”$RANDOM” ‘BEGIN { srand(seed); for (i=0; i<n; ++i){ printf(“{\”name\”:\”Tom\”, \”age\”:25, \”car\”:\”Toyota\”, \”salary\”:\”%.2f\”}\n”, rand()); system(“ksh93 -c \”sleep 1.0\””); }}’
| y ### pipe y just prints yes in response to any prompt

&& ### bash term that means, if the previous command returns success, then do the next one e.g.
gradlew buildall && scp build/distributions/Artifacts.zip tomcatserveralias: && date

curl htts://somejsonendpoint.json | python -m json.tool ### pretty prints json responses! 😀

Windows Sub System for Linux
scp /mnt/c/Users/mog/Downloads/cleantomcatlogs.sh centos@5.555.55.55:/tmp/ ### transfer file via scp to ec2 instance to path /tmp/, will be prompted for password
PS1=’\u:\W\$ ‘ ### make command line prompt shorter

MiniScripts
### Remove all lines of given file that match the keywords passed into this array
#!/bin/bash
declare -a array_of_strings_to_remove;
array_of_strings_to_remove=(“ssl” “expected type.” “” “CacheAdmin”)
for i in “${array_of_strings_to_remove[@]}”; do sed -i “/$i/d” /mnt/c/Users/mogorman/Downloads/Errors.log; done

VIM
yum install vim :O
:set hlsearch
n is next down
N is next up
Pasting… without losing formatting.
:set paste
Then enter insert mode. You will see the status bar say insert (paste). Paste your code. Hit ESC to return to normal mode, and:
:set nopaste
:set number ### show line numbers of file
:q!
:wq!
Ctrl + F
Ctrl + u
Ctrl + a ### Go to start of line
Ctrl + e ### To to end of line
Ctrl + k ### Delete everything from cursor to EOL
Ctrl + x + backspace ### Delete everything from BOL to cursor
Ctrl + _ ### undo typing

Java/JVM
jps // jps -ll ### list jvm processes with detail
ps -o user= -p PIDHERE ### returns owner of process pid
sudo -u tomcat COMMAND ### run command as user tomcat
jstack ### for getting thread dump
jmap ### for getting the heap dump.

### Paste multiple commands to be run one after another
bash <<EOF
cmd2

sleep 60;
EOF

Published by

Leave a Reply

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