CLI
Basics
Navigation
option + click
move cursor inside linectrl + a
move to beginning of linectrl + e
move to end of line
Editing
ctrl + u
deletes linectrl + k
deletes from cursor to end of linectrl + w
deletes word before cursorcmd + f
search terminal
History
- up arrow key shows previous command
- down arrow key shows next command
ctrl + c
stops current commandctrl + r
search command historyhistory
shows history of commands
Clear
ctrl + l
orclear
clears screenctrl + d
orexit
exit terminal
Shell
man commandName
info on command man find q to quitwhatis commandName
simplified info on commandwhereis executableName
returns path to executable whereis python3whoami
shows username
Navigation
pwd
present working directorypwd -P
current full directory pathls
list shows files in current directoryls -a
shows all files, including secretls -l
shows long format info on files in current directory file permissions, user, the group the user belongs too, the date of creationcd
navigates to the home directorycd /path/to/directory
change directorycd ..
navigates up one level in the directory tree
File Commands
>
redirect output to file(copy)>>
append output onto file<
read input from a file|
piping, use the output from left side program as input for right side program
Open files
open file\ name.txt
opens “file name.txt” \ allows for a spaceopen file*
opens all files beginning with “file” eg. file.txt or file243.js * is a wildcardopen file?
opens all files beginning with “file” and ending in one wild card. eg. file2 of fileAopen -a /Applications/AppName.app
open file with applicationopen -a "Google Chrome" websiteAddress
open file in browseropen
pwd“ opens ‘present working directory’open .
open current foldercode .
opens directory in VSCode
Create, move, and delete files
mkdir directoryName
creates a directory/foldermkdir .directoryName
creates a secret directory/foldermkdir -p directoryName/nestedDirectoryName
creates nested directorytouch fileName.html
creates filetouch filename{1,2,3,4}.html
creates 4 filename.html filestouch filename{1..4}.html
creates 4 filename.html filestouch .secretFile.html
creates hidden filecp copiedFile.txt pastedFile.text
copies a filecp -Rv copiedDirectory pastedDirectory
copies a directoryrm fileName.text
deletes filerm *
deletes all files in directoryrmdir directoryName
deletes directoryrm -rf directoryName
deletes directory and everything inside itmv path/oldName.txt path/newName.txt
renames a file or directory
Read files
head filename
outputs the first 10 lines of a filetail filename
outputs the last 10 lines of a filecat filename
displays file content in clicat file*
displays files with names starting with filecat < -
adding < before filename allows you to access files named with special charscat “filename w spaces”
adding quotation marks allows you to access files with spaces in the namecat ./*
all files in folder (allows you to find files that begin with special charscat file1 file2
displays multiple files content in clicat file1 file2 > newcombinedfile
creates a new combined filecat < file1 > file2
copies file1 to file2cat filename | fmt -w 20
fmt—format. -w width. Display file content with width of 20 charscat -n filename
prints line numbers
Find, sort, and search files
find . -name src -type d
find directory with name srcfind . -path "*/folderName/*.py" -type f
find all of type files that have a in a folderdu fileName -c
estimate file size in bytessort filename
sorts lines of a file alphabeticallysort filename | uniq -c
sort lines, output duplicate lines only once, -c will count linessort filename | uniq -u
return only unique lineswc filename
outputs how many lines, words, and characters are in a filegrep pattern filename
looks for text inside files. eg. grep admin /desktop/foldergrep -B 0 -A 5 "searchWord" filename
return the line with the search word and x lines before (-B) and x lines aftergrep -C 2 foo README.txt
return the line and the same amount of lines before and aftergrep -f file1 file2
compares and returns similar linesgrep "YOUR SEARCH STRING" filename > output-file
copies (> means redirect) grep results into output-filegrep -i pattern filename
ignores word casegrep -r pattern filename
searches all files under the specified directoryegrep "condition" filename
(grep -E) output lines containing condition. eg. egrep “practice|PRACTICE” filename returns all lines containing the word practicefgrep "condition" filename
(grep -F) find the exact string “condition” in file
Compare, rename, and manipulate files
ls -v | cat -n | while read n f; do mv -n "$f" "$n.ext"; done
renames all files in directory with incrementing numbersdiff file1 file2
compares differences between filessed -n 5p file
print one line of filesed "word" file
removes the word from filesed "7d;10d;11d" file1.txt > file2.txt
removes multiple lines from file, results to file2
Compress and download files
file filename
return file typegzip filename.txt
compress filegzcat filename
allows you to view a compressed file without unzipping itgunzip filename.gz
decompress file (must be a .gz)tar -xvf filename
decompresses tar filebzip2 -d filename.bz2
decompressed bzip2 file (must be a bz2)wget file
downloads file
Processes
Top
real time view of CPU usage and processesps
process - any instance of a program, each process has a unique process idps -ef
list all processeslsof -i tcp:3000
checks what is running/listening on portkill -15 PID
TERM the process at process idkill -3 PID
QUIT the process at process idkillall processName
ends all processes with name
DNS/IP
whois domainname
Domain name system lookupnslookup websitename.com
lookup Internet Protocol address http://142.251.215.238:80 Google.comping [websitename.com](http://websitename.com)
get IP address http://142.251.211.228:80 Google.com
HTTP request
⭐curl stands for client URL
-H
headers-X
request-d
data
curl -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" -X POST http://localhost:3000/ -d '{"payload"{"url":"https://demo.appwrite.io/","method":"GET","headers":{"x-client-version":"1.0.0"},"body":""}}'
Unix file permissions
chown
change ownerchown -options userName:groupName filename
chmod
change mode, allows users to change the read, write, and execute permissions for files and foldersls -l filename
view info returns permissions
symbol | filetype | mode | |||
---|---|---|---|---|---|
- | file | r | read | 4 | |
d | directory | w | write | 2 | |
i | link | x | execute | 1 |
Example: -rw-r--r--@
- regular filetype, rw- user can read and write, r— groups and others can only read
Types of users:
- User Person who has created the file.
- Group Group of other users who share similar privileges as that of the owner.
- Others Other members who have access to the path where you have kept the files.
Permission level options:
- 4 Read Permission
- 2 Write Permission
- 1 Execute Permission
- 0 No permission
Calculated (user read + user write + user execute)(group read + group write…)(others…)
# full permission for all
chmod 777 filename.txt
# USER: 4+2+1 = 7 GROUP: 4+2 = 6 OTHERS: 0
# USER: read, write, execute GROUP: read, write OTHERS: none.
chmod 760 filename.txt
Resources
- Command Line Murder Mystery CLI whodunit
- Over the Wire wargames to learn shell basics
- Awesome Shell curated list of awesome command-line frameworks, toolkits, and guides