Bash cheat sheet

pwd
- Print working directory, outputs the path to the current directory
tree <path>
- See how the child files are organized from the current directory
cd <path>
- Change directory, changes current directory to the file path given
ls <path>
ls -a <path>
- Lists the files contained in current directory - Use -a to show hidden files
mkdir <flags> <path(s)>
- Makes directory, a new directory at the file path given
mv <flags> <sourcePath(s)> <destinationPath>
- Moves files from source to destination - Can rename / overwrite files
cp <flags> <sourcePath(s)> <destinationPath>
- Copy files / directories - Can overwrite existing files
rm <flags> <filePath(s)>
- Remove each specified file permanently - Does not remove directories by default
alias <aliasName>='<commandToRun>'
- Substitutes the alias name with the command to be run - Create shortcuts for tedious commands - Extend the default behavior of commands
alias rm='rm -i'          # confirm to delete file
alias rm='mv -t -/.tash'  # move file to a trash directory
alias cp='cp -b'          # make backup of destinationFile
alias mv='mv -u'          # move only if sourceFile is newer
alias hello="echo 'cow power'"

cat <flags> <file>
less <flags> <file>
- Print contents of file to screen; concatenate - less outputs a page at a time, to scroll through - Can search through file using the /<word>
head <flags> <file>
- Shows the first 10 lines of a file - Use -n <number of lines> to print certain number of lines from file
tail <flags> <file>
- Shows the last 10 lines of a file
nano <flags> <file>
- Opens simple CLI text editor
vim <flags> <file>
- Another text editor
man <command>
man mkdir
man stdio
- System reference manuals - Contains details of all command behavior and flags - Documentation of c libraries
tldr <flags> <command>
- Lists the typical uses of a command

grep 'word-to-search' <file_name>
grep 'word-to-search' <file_name1> <file_name2> <file_name3> ...
grep 'pattern' <file_name>      # searches by regex pattern
- Searches the given files for lines containing a match to a given pattern list 1

grep 'Gandalf' list_of_dwarves.txt 

if [[ $? != 0 ]] 
then 
    echo "Gandalf is not a dwarf" 
    exit 1 
else 
    echo "Gandalf is a dwarf" 
    exit 0 
fi
- $? stores the exit code for the last command

p10k configure
- Configure powerlevel10k looks


  1. https://www.cyberciti.biz/faq/grep-in-bash/