CLI Commands and Cheat Sheet
- Linux Command Line Interface Cheat Sheet. Main source.
- Source for Curl command.
* # wildcard? # represents
cd # go to home directorycd .. # go up one levelcd /etc # go to /etc
ls # list files and subfoldersls -l # with detailsls -a # display hidden files/foldersls X # list the files
cmp A B # compare two files A and B, no output if identicaldiff A B # compare two files A and B, outputs differencepwd # display current path working directorymkdir Y # create new Y foldermv A B # move file in location A to location B
cp A B # move file in location A to location Bcp -r Y Z # recursively copy directory Y and its contents to directory Z
rm X # delete X permanentlyrm -r Y # recursively remove directory Yrm -f X # forcibly remove file Xrm -rf Y # forcibly remove directory Yrmdir Y # remove directory, only if empty
open X # open X in default appopen -e-X # open X in default text editortouch X # create empty file X
cat X # view contents of Xcat -b X # also display line numberswc X # display word count of X
head X # display the first 10 lines of Xhead -n 4 X # show the first 4 lines of Xls *.c | head -n 5 # display first 5 items of a list of *.c files
tail X # display last 10 lines of Xtail -n +1 X # display entire contents of X, with headers and file namestail -f X # display the last 10 lines of Xless # read a file with foward and backwards navigationcat file.txt | less # pipe file.txt and read it with fwd and bwd navigation
ln -s A S # create symbolic path A to link name SInput/Output Redirection
Section titled “Input/Output Redirection”echo TEXT # display line of TEXT or contents of a variableecho -e TEXT # also interprets escape characters in TEXT (e.g. \n new line, \b backlash, \t tab)cmd1 | cmd2 # | is the pipe character, feeds output of cmd1 to cmd2 (e.g. ps aux | grep python3)cmd > file # redirect output of cmd to filecmd >& file # redirect output of cmd to file, overwrites pre-existing contentcmd > /dev/ null # suppress the output of cmdcmd >> file # append output of cmd into filecmd < file # read input cmd from filecmd <<< string # input text string to cmdcmd 2> foo # redirect error messages of cmd to foocmd 2>> foo # append error messages of cmd to foocmd &> file # redirect output and errors messages of cmd to fileSearch and Filter
Section titled “Search and Filter”grep patt /path/to/src # search for a text pattern patt in X (e.g. ps aux | grep python3)grep -r patt /path/to/src # search recursively for text pattern pattgrep -v patt X # return lines in X not matching specified pattgrep -l patt X # write to standard output the names of files containing pattgrep -i patt X # perform case-sensitive matching on X, ignore case of patt
find # find filesfind /path/to/src -name "*.sh" # ind all files in /path/to/src matching patter "*.sh" in filenamefind /home -size +100M # find all files in the /home directory larger than 100MB
locate name # find files and directories by namesort X # arrange lines of text in X alphabetically or numericallyArchives
Section titled “Archives”tar # manipulate archives with .tar extensiontar -v # get verbose output (e.g. tar -tvf)tar -cf archive.tar Y # create tar file name archive.tar containing Ytar -xf archive.tar # extract archive.tartar -tf archive.tar # list contents of archive.tartar -czf archive.tar.gz Y # create gzip-compressed tar named archive.tag.gz containing Ytar -xzf archive.tar.gz # extract gzip-compressed tar named archive.tar.gztar -cjf archive.tar.bz2 Y # extract bzip2-compressed tar named archive.tar.bz2 containing Ytar -xjf archive.tar.bz2 # extract bzip2-compressed tar naned archive.tar.bz2
gzip # manipulate archives with .gz extensiongzip Y # create gzip archive named T.gz containing Ygzip -l Y.gz # list contents of gzip archive Y.gz
bzip2 # manipulate archives with .bz2 extensionbzip2 Y # create bzip2 archive named Y.bz2 containing Y
zip -r Z.zip Y # zip Y to the archive Z.zipunzip Z.zip # unzip Z.zip to current directoryunzip -l Z.zip # list contents of Z.zipFile Transfer
Section titled “File Transfer”ssh user@access # connect to access as userssh access # connect to access as local usernamessh -p port user@access # connect access as user using port
scp [user1@]host1:[path1] [user2@]host2:[path2] # login to hostN as userN for N=1,2# (e.g. scp alice@pi:/home/source bob@arduino:/destination)scp -P port [user1@]host1:[path1] [user2@]host2:[path2] # connect hostN as userN using port for N=1,2scp -r [user1@]host1:[path1] [user2@]host2:[path2] # recursively cp all files/folders path1 to path2
sftp [user@]access # loging to access as user via SSH, no user means use local usersftp access # connect access as your local usernamesftp -P port user@access # connect to access as user using port
rsync -a [path1] [path2] # synchronize [path1] to [path2], preserves symbolic links, attr, etc.rsync -avz host1:[path1] [path2] # synchronize [path1] on remote host1, to local [path2]File Permissions
Section titled “File Permissions”chmod permission file # change file/folder permissions (can be [u/g/o/a][+/-/=][r/w/x])chown user2 file # change owner of file to user2chgrp group2 file # change group of file to group2
chmod +x testfile # allow all users to execute the filechmod u-w testfile # forbid current user from writing/changing filechmod u+wx,g-x,o=rx testfile # simultaneously add write and execute permissions to user# remove execute permission from gourp, and set permissions of other uses to only read and write| Octal | Permission(s) | Equivalent |
|---|---|---|
| 0 | No permissions | -rwx |
| 1 | Execute only | =x |
| 2 | Write only | =w |
| 3 | Write and Execute (2+1=3) | =wx |
| 4 | Read only | =r |
| 5 | Read and Execute (4+1=5) | =rx |
| 6 | Read and Write (4+2=6) | =rw |
| 7 | All permissions (4+2+1=7) | =rwx |
chmod 777 testfile # all all users to execute filechmod 177 testfile # execute-only to user (u); group (g) and others (o), read and executy only.chmod 365 testfile # user (u) write+execute-only; group (g) read+write-only; others (o) r+exe onlyCurl Commands
Section titled “Curl Commands”# Wttr.incurl https://wttr.in/MSP?format=%l+%C+%h+%w+%t+feels+like+%f# IP Cowcurl ip.wtf/moo# Find and Listen# Find everyone on the network, then start spoofing each MAC until you find a valid one.nmap -sn 192.168.0.1-255System Information
Section titled “System Information”uname # show system infouname -a # detailed system infouname -r # kernel release info (version too)uptime # how long the system is runningsudo # superusercal # show calendar with today highlighteddate # show current date and time of machinehalt # stop system immediatelyshutdown # shut down the systemreboot # restart the systemlast reboot # show reboot historyman <command> # show manual for given <command>hostname # show system host namehostname -I # display IP address of hortcat /etc/*-release # current version of distro installerDisk Usage
Section titled “Disk Usage”df # display free disk spacedu # show file/folder sizes on diskdu -ah # disk usage in human readable format (e.g. KB, MB, etc)du -sh # total disk usage of current directorydu -h # free and used space on mounted filesystemsdu -i # free and used inodes on mounted filesystemsfdisk -l # list disk partitions, sizes, and typesfree -h # display free and used memory in human readable unitsfree -m # display free and used memory in MBfree -g # display free and used memory in GBProcess Management and Performance Monitoring
Section titled “Process Management and Performance Monitoring”& # add this character to run command/process in the background
ps # show process status (e.g. ps aux | grep python3)(all user, show user column, show unattached)ps -e # print all running processesps -ef # print detailed overviewps -U root -u root # display all processes running under rootps -eo pid,user,command # display only columns pid, user, command in ps output
top # display sorted info about processeshtop # display sort info abotu processes with visual highlightsatop # display the detialed info about processes and hardware
kill PID # kill process with process ID PIDkillall proc1 # kills all processes containing proc1 in their names
lsof # list all open fileslsof -u root # list all files opened by root (e.g. lsof -u root | less)
mpstat 2 # processor statistics updated every 2 secondsvmstat 1 # virtual memory statistics updated 1 secondiostat 3 # system input/output statistics updated every 3 seconds
tail -n 100 /var/log/messages # display last 100 lines in system logs
tcpdump -i eth0 # capture and display all packets on interface eth0tcpdump -i eth0 port 80 # monitor all traffic on interface eth0 port 80
watch df -h # execute df -h and show periodic updates (Ctrl + C to exit)User Management
Section titled “User Management”who # who is logged inw # display what users are online and what doingusers # list current userswhoami # display what user you are logged in asid # display the use ID and group ID for current userlast # display the last users who have logged onto the systemgroupadd gp1 # create group name gp1useradd -c "Alice Bob" -m ab1 # create account ab1 with comment "Alice Bob" and create new /homeuserdel ab1 # delete account name ab1usermod -aG gp1 ab1 # add the account ab1 to the group gp1Networking
Section titled “Networking”ifconfig # display network interfaces with IP addressesifconfig -a # display all network interfaces, eve if down, with IP addressesifconfig eth0 # display IP address abd details of the eth0 interfaceip a # another way to display all network interfaces with IP addressesethtools eth0 # query or control network driver and hardware settings on interface eth0
netstat # print open sockets, routing tables, inteace statistics, masq connectionsnetstat -a # show both listening and non-listening sockets (e.g. netstat -a | less)netstat -l # show only listening socketsnetstat -nulp # show listening TCP and UDP ports and corresponding programs
ping host # ping host, may be symbolic name, domain name or IP addresswhois domain # display whois info for domaindig domain # display DNS info for domaindig -x addr # rever lookup on IP addrress addrhost domain # display DNS IP address for domainwget LINK # download from location LINKcurl LINK # display HTML source of LINKAdd a Bell at the End of a Script
Section titled “Add a Bell at the End of a Script”; tput beladded to end of scripts to make a beep after it has complete source
Adding ”; tput bel” at the end of a script in Unix-like systems will cause the terminal to emit an audible bell sound when the script finishes executing. This is achieved by using the tput command, which is used to query and set terminal capabilities, and in this case, bel is one of those capabilities, representing the audible bell.
The semicolon (;) is a command separator in shell scripting, allowing multiple commands to be executed sequentially on a single line. So, when you add ”; tput bel” at the end of a script, it ensures that the bell sound is triggered after the script completes its execution, providing an audible signal to the user that the script has finished running.