Table of contents
- Navigate the Filesystem
- View Files and Folders
- Create and Delete Files and Folders
- Move and Copy Files and Folders
- Find Files and Folders
- Search for Text
- Make HTTP Requests
- Create and Run Scripts
- Store and Use Values
- Understand and Use Functions
- Understand Exit Statuses
- Use Conditional Statements
- Create Aliases in .bash_profile
- Create and Copy multiple files with Brace Expansions
- History expansion
- Default arguments
- Bash Keyboard Shortcuts
- Complicated Conditional Statements
Navigate the Filesystem
pwd
~ view current directorycd
~ move into a directory..
~ move up a directory
ls
~ list what is in a directory-a
~ hidden files and folders-l
~ long listing, see more information
View Files and Folders
cat
~ see content of a file-n
~ see content with line numbers
less
~ see content of a file in editoropen
~ open files in the directory
Create and Delete Files and Folders
touch
~ create filesmkdir
~ create folders-p
~ create nested foldersa/b/c
echo
~ log string or write to file>
~ write to file>>
~ append at the end of file
rm
~ remove file or folder-r
~ remove folder recursively
Move and Copy Files and Folders
mv
~ used to move and rename files and foldersmv source/* target/
~ move everything inside source to target folder
cp
~ copy files and folderscp -R source/* target/
~ copy everything recursively from source to target folder
Find Files and Folders
find
~ find files and foldersfind images/ -name "*.png"
~ find files that end withpng
.-iname
~ find files, but case insensitive
find . -type d -name "images"
~ find all folders in current directory with name images-delete
~ flag to delete what was found
Search for Text
grep
~ search for textgrep "text" folder/file.js
~ search for text in whatever file--color
~ search but colorize what we're looking for-n
~ include the line numbers-e
~ search but use regexman grep
~ see all flags available
Make HTTP Requests
curl
~ make requests, by default GET-i
~ inspect headers-iL
~ inspect headers and follow redirect to see responsecurl -iL "..."
-H
~ pass headerscurl -H "Authorization: ..."
-X
~ change the request methodcurl -X POST -H "Content-Type: ..." -d '{ title: "Curling" }'
\
~ to do things on new lines nicely formattedcurl -i -X PUT \
Create and Run Scripts
.sh
~ file has to end with thischmod x+u ./file.sh
~ make file executable- The command stands for change mode, giving the user the executable permissions
$
~ Accept arguments in your script. They will be passed in chronological order when running the script:./file.sh argument1 argument2
$(<command>)
~ run commands inside the bash script#
~ write comments in bash
Store and Use Values
var=value
~ set a variable's value for a sessionecho $var
~ print the variableunset var
~ unset the variableexport var
~ make the variable available to all child processes
Understand and Use Functions
Create and call a function in bash:
hi() {
echo "Hello world"
}
hi
Function with an argument passed:
hi() {
echo "$1 world"
}
hi "Hello"
Assign function's return statement to a variable:
hi() {
echo "$1 world"
}
hiResult=$(hi "Hello")
echo "the result from hi is $hiResult"
local
~ create local variables only available within the function
Understand Exit Statuses
echo $?
~ see the exit status0
for ok1
for error
Use Conditional Statements
# Some conditional primaries that can be used in the if expression:
# =, != string (in)equality
# -eq, -ne numeric (in)equality
# -lt, -gt less/greater than
# -z check variable is not set
# -e check file/folder exists
# elif else if statement
if [[ $USER = 'naruto' ]]; then
echo "true"
else
echo "false"
fi
# Inline format, similar to ternaries
[[ $USER = 'naruto' ]] && echo "yes" || echo "no"
Create Aliases in .bash_profile
On Ubuntu, if you don't create the
.bash_profile
file, it will use the existing configuration file~/.profile
.gitm="git add . && git commit -m 'commit'"
~ example of an alias
Create and Copy multiple files with Brace Expansions
Items in the list of brackets.
touch index.js{,.backup}
~ create two files,index.js
andindex.js.backup
, first item in the list is emptymkdir -p packages/{pkg1,pkg2,pkg3}/src
~ create multiple packages insidepackages
folder, each containingsrc
folderecho {1..10}
,echo {a..z}
~ print out rangesYou can use it for other things like creating multiple files
History expansion
!!
~ run the previous command
Default arguments
dir=${1:-$PWD}
~ users would pass the directory as the first argument, but it defaults to the current directory
Bash Keyboard Shortcuts
Ctrl+A - go to the beginning of a line
Ctrl+E- go to the end of a line
Ctrl+K - clears line up to the cursor
Ctrl+W - delete last word
Ctrl+L - clear the screen (equivalent of the
clear
command)
Complicated Conditional Statements
Case statements, they can also be on multiple lines:
case "$1" in
firstValue) echo "firstValue matched";;
secondValue) echo "secondValue matched";;
thirdValue)
echo "thirdValue matched"
;;
esac