Linux Commands
cd: Change Directorycditself will default to the home DIRcd -takes you back to the previous directory you were at
pwd: print the directory you are in
whoami: prints current user
wc: get file size infowc -c unix.md: Get number of bytes of filewc -l unix.md: Get number of lines of file
diff: Compares two filesdiff -y <file1> <file2>: Outputs diff of file1 and file2 side-by-side
find: Find a filefind . -name "*.py": Finds all .py files from current directory and down
tree: Shows files in tree display
printenv: Print environment variables
curl: cURL lets us query URLs from the command linecurl www.google.com: Returns HTML/Scripts of google.comoption
-igives information headeroption
-d or --datacan be used to post data to a urlcurl -d "first=Bob&last=Ross" http://<url>
pass in username and password:
curl -u <username>:<password> <url>Download response e.g. picture:
curl -o test.jpg <url>-> outputs response to test.jpg
grep: Search for word or expression in a filegrep "expression" <file>Use
-ito search for expression without being case sensitiveCan use it with pipe:
cat my_file.txt | grep "hello world"Use with extended regualar expressions:
grep -E 'pattern1|pattern2' fileNameOr regular grep:
grep 'pattern1|pattern2' fileNamegrep -e 'pattern1' -e 'pattern2' fileName
Use
-cto get a count of number of grep matchesCan also search multiple files:
grep -c 'warning|error' /var/log/*log-> Searches all log files…Use the -R flag to recursivly search subdirectories too
grep -r "pattern" *: This will search recursivly all files in the current dir and belowgrep -P "(?<=hello)(.*)(?=world)": Use a pearl regex. This returns all expressions found between “hello” and “world”.
uname: prints system info
ps: “Process Status”, prints info about running processesps -Aorps -eprints all running processesps auxshows all running processes in BSD formatps -u <user>allows you to filter by userps -C process_namesearches the PID of a process by name
pidof <process_name>: This returns the PID only of the processsudo kill $(pidof <process_name>)
chown: Change the owner of a filesudo chown root <my_file>: Example changing owner to root
chmod: Changes permissions to a filePermissions are grouped in 3 sections, Owner, Group and Users. - Owner is the owner of the file which can be changed with chown - Users is all the users of the system
Setting permissions with numbers: - 4: read permission - 2: write permission - 1: execute permission
chmod 764 <file_name>: Sets RWX permission for owner, RW for Group and R only for users.Adding with letters: - chmod +x <file_name>: This will add execute permissions for Owner, Group and Users
echo: Prints a string to console/fileUse with -e to for formatting characters:
echo -e "nThis is a newline"
ctrl+R: Reverse i search.Allows you to search through previous commands.
Press
ctrl+Rand start typing the search string. Hitctrl+Rto move to the next matching command.Use the arrow keys if you want to modify the command before running it.