CLI
Basics
Navigation
option + clickmove cursor inside linectrl + amove to beginning of linectrl + emove to end of line
Editing
ctrl + udeletes linectrl + kdeletes from cursor to end of linectrl + wdeletes word before cursorcmd + fsearch terminal
History
- up arrow key shows previous command
- down arrow key shows next command
ctrl + cstops current commandctrl + rsearch command historyhistoryshows history of commands
Clear
ctrl + lorclearclears screenctrl + dorexitexit terminal
Shell
man commandNameinfo on command man find q to quitwhatis commandNamesimplified info on commandwhereis executableNamereturns path to executable whereis python3whoamishows username
Navigation
pwdpresent working directorypwd -Pcurrent full directory pathlslist shows files in current directoryls -ashows all files, including secretls -lshows long format info on files in current directory file permissions, user, the group the user belongs too, the date of creationcdnavigates to the home directorycd /path/to/directorychange 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.txtopens “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.appopen file with applicationopen -a "Google Chrome" websiteAddressopen file in browseropenpwd“ opens ‘present working directory’open .open current foldercode .opens directory in VSCode
Create, move, and delete files
mkdir directoryNamecreates a directory/foldermkdir .directoryNamecreates a secret directory/foldermkdir -p directoryName/nestedDirectoryNamecreates nested directorytouch fileName.htmlcreates filetouch filename{1,2,3,4}.htmlcreates 4 filename.html filestouch filename{1..4}.htmlcreates 4 filename.html filestouch .secretFile.htmlcreates hidden filecp copiedFile.txt pastedFile.textcopies a filecp -Rv copiedDirectory pastedDirectorycopies a directoryrm fileName.textdeletes filerm *deletes all files in directoryrmdir directoryNamedeletes directoryrm -rf directoryNamedeletes directory and everything inside itmv path/oldName.txt path/newName.txtrenames a file or directory
Read files
head filenameoutputs the first 10 lines of a filetail filenameoutputs the last 10 lines of a filecat filenamedisplays 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 file2displays multiple files content in clicat file1 file2 > newcombinedfilecreates a new combined filecat < file1 > file2copies file1 to file2cat filename | fmt -w 20fmt—format. -w width. Display file content with width of 20 charscat -n filenameprints line numbers
Find, sort, and search files
find . -name src -type dfind directory with name srcfind . -path "*/folderName/*.py" -type ffind all of type files that have a in a folderdu fileName -cestimate file size in bytessort filenamesorts lines of a file alphabeticallysort filename | uniq -csort lines, output duplicate lines only once, -c will count linessort filename | uniq -ureturn only unique lineswc filenameoutputs how many lines, words, and characters are in a filegrep pattern filenamelooks for text inside files. eg. grep admin /desktop/foldergrep -B 0 -A 5 "searchWord" filenamereturn the line with the search word and x lines before (-B) and x lines aftergrep -C 2 foo README.txtreturn the line and the same amount of lines before and aftergrep -f file1 file2compares and returns similar linesgrep "YOUR SEARCH STRING" filename > output-filecopies (> means redirect) grep results into output-filegrep -i pattern filenameignores word casegrep -r pattern filenamesearches 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"; donerenames all files in directory with incrementing numbersdiff file1 file2compares differences between filessed -n 5p fileprint one line of filesed "word" fileremoves the word from filesed "7d;10d;11d" file1.txt > file2.txtremoves multiple lines from file, results to file2
Compress and download files
file filenamereturn file typegzip filename.txtcompress filegzcat filenameallows you to view a compressed file without unzipping itgunzip filename.gzdecompress file (must be a .gz)tar -xvf filenamedecompresses tar filebzip2 -d filename.bz2decompressed bzip2 file (must be a bz2)wget filedownloads file
Processes
Topreal time view of CPU usage and processespsprocess - any instance of a program, each process has a unique process idps -eflist all processeslsof -i tcp:3000checks what is running/listening on portkill -15 PIDTERM the process at process idkill -3 PIDQUIT the process at process idkillall processNameends all processes with name
DNS/IP
whois domainnameDomain name system lookupnslookup websitename.comlookup 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
-Hheaders-Xrequest-ddata
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
chownchange ownerchown -options userName:groupName filenamechmodchange mode, allows users to change the read, write, and execute permissions for files and foldersls -l filenameview 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 games to learn shell basics
- Awesome Shell list of command-line frameworks, tool kits, and guides