UNIX / LINUX CHEAT SHEET
FILE SYSTEM
ls β list items in current directory
ls -l β list items in current directory and show in long format to see perimissions, size, and modification date
ls -a β list all items in current directory, including hidden files
ls -F β list all items in current directory and show directories with a slash and executables with a star
ls dir β list all items in directory dir
cd dir β change directory to dir
cd .. β go up one directory
cd / β go to the root directory
cd ~ β go to to your home directory
cd - β go to the last directory you were just in
pwd β show present working directory
mkdir dir β make directory dir
rm file β remove file
rm -r dir β remove directory dir recursively
cp file1 file2 β copy file1 to file2
cp -r dir1 dir2 β copy directory dir1 to dir2 recursively
mv file1 file2 β move (rename) file1 to file2
ln -s file link β create symbolic link to file
touch file β create or update file
cat file β output the contents of file
less file β view file with page navigation
head file β output the first 10 lines of file
tail file β output the last 10 lines of file
tail -f file β output the contents of file as it grows, starting with the last 10 lines
vim file β edit file
alias name 'command' β create an alias for a command
SYSTEM
shutdown β shut down machine
reboot β restart machine
date β show the current date and time
whoami β who you are logged in as
finger user β display information about user
man command β show the manual for command
df β show disk usage
du β show directory space usage
free β show memory and swap usage
whereis app β show possible locations of app
which app β show which app will be run by default
PROCESS MANAGEMENT
ps β display your currently active processes
top β display all running processes
kill pid β kill process id pid
kill -9 pid β force kill process id pid
PERMISSIONS
ls -l β list items in current directory and show permissions
chmod ugo file β change permissions of file to ugo - u is the user's permissions, g is the group's permissions, and o is everyone else's permissions. The values of u, g, and o can be any number between 0 and 7.
7 β full permissions
6 β read and write only
5 β read and execute only
4 β read only
3 β write and execute only
2 β write only
1 β execute only
0 β no permissions
chmod 600 file β you can read and write - good for files
chmod 700 file β you can read, write, and execute - good for scripts
chmod 644 file β you can read and write, and everyone else can only read - good for web pages
chmod 755 file β you can read, write, and execute, and everyone else can read and execute - good for programs that you want to share
NETWORKING
wget file β download a file
curl file β download a file
scp user@host:file dir β secure copy a file from remote server to the dir directory on your machine
scp file user@host:dir β secure copy a file from your machine to the dir directory on a remote server
scp -r user@host:dir dir β secure copy the directory dir from remote server to the directory dir on your machine
ssh user@host β connect to host as user
ssh -p port user@host β connect to host on port as user
ssh-copy-id user@host β add your key to host for user to enable a keyed or passwordless login
ping host β ping host and output results
whois domain β get information for domain
dig domain β get DNS information for domain
dig -x host β reverse lookup host
lsof -i tcp:1337 β list all processes running on port 1337
SEARCHING
grep pattern files β search for pattern in files
grep -r pattern dir β search recursively for pattern in dir
grep -rn pattern dir β search recursively for pattern in dir and show the line number found
grep -r pattern dir --include='*.ext β search recursively for pattern in dir and only search in files with .ext extension
command | grep pattern β search for pattern in the output of command
find file β find all instances of file in real system
locate file β find all instances of file using indexed database built from the updatedb command. Much faster than find
sed -i 's/day/night/g' file β find all occurrences of day in a file and replace them with night - s means substitude and g means global - sed also supports regular expressions
COMPRESSION
tar cf file.tar files β create a tar named file.tar containing files
tar xf file.tar β extract the files from file.tar
tar czf file.tar.gz files β create a tar with Gzip compression
tar xzf file.tar.gz β extract a tar using Gzip
gzip file β compresses file and renames it to file.gz
gzip -d file.gz β decompresses file.gz back to file
SHORTCUTS
ctrl+a β move cursor to beginning of line
ctrl+f β move cursor to end of line
alt+f β move cursor forward 1 word
alt+b β move cursor backward 1 word
only file :- scp -i r_login.pem artifactory-oss-6.7.2.tar.gz ec2-user@34.222.132.1:/home/ec2-user for folder :- scp -ri r_login.pem artifactory-oss-6.7.2.tar.gz ec2-user@34.222.132.1:/home/ec2-user