Shell Snippets
- date
- echo 'Hello World
- echo $PATH
- which echo
- --help
- sudo
- convert image.png image.jpg
- man mv (Show manuel for the move command, close with cmd -q)
- pwd (print working directory)
- cd (change directory)
- cd .. (one directory back)
- cd ./home (change directory in the current directory
- ls (Lists files in the directory)
- ls -l (List files with extra information)
- ls -R (List only the directories with paths)
- ls *.sh (Only files with .sh extension)
- cd ~ (Goes to home directory)
- cd - (Goes to the previous directory)
- mv current-name-folder new-name-folder (Change name or file of folder)
- cp (copy)
- rm (remove)
- clear (control l - also works)
- find . -name test-folder -type d (find in current directory with name 'test-folder' and type 'directory')
- find . -name "*.txt" (find all files with the .txt extension)
- find . -name "*.txt -exec rm {} ; (remove those files)
- grep again hello.txt (Check the string in the file of hello.txt)
- history (prints out all the executed commands)
- history 1 | grep mkdir (print out all the executed commands of 'mkdir')
- tree (show tree of the current directory)
- echo "Hello" > hello.txt (creates file with hello) in current directory
- echo "Hello Again." > hello.txt (appents the file with new content)
- cat hello.txt (prints content of a file)
- ls | >> hello.txt (pipe)
- open hello.txt (open a file)
- open ./ (open current folder)
- source mcd.sh (load a script into your shell
- touch exmaple
- touch example-1 example-2
- touch example-{3,4,5}
Working with values
foo=bar
echo $foo
# Prints out: 'Bar'
echo "Value is $foo"
# Print out: "Value is bar"
echo 'Value is $foo'
# Print out: "Value is $foo"Creating a shell script
mcd () {
mkdir -p "$1"
cd "$1"
}$1 is the first variable of a shell script.
$0 is the name of the file script itself.
Example.sh script that check files for foobar
#/bin/bash
echo "Starting program at $(date)"
echo "Running program $0 with $# arguments with pid $$"
for file in "$@"; do
grep foobar "$file" > /dev/null 2> /dev/null
if [[ "$?" -ne 0]]; then
echo "File $file does not have foobar, adding one"
echo "# foobar" >> "$file"
fi
doneAbsolute Paths vs Relative Paths
Spaces are reserved with Scripting is Bash
Single space don't take in account variables
PID means process id $$
Data Wrangling, formatting data