Slides (contains setup instructions)
-
Commands: Commands are unique statements that indicate specific tasks
-
Arguments: Variables/inputs needed to complete commands (some commands need more than one argument)
-
Flags: Come at the end of a command, found in 'man' pages. Pretty much do extra behavior.
-
Directory: The same thing as a folder in your computer
- tab : tries to auto-complete what you are typing
- Ctrl-C : ends the current program; keep a note of this one.
- !! : Runs/brings up previous command entered, hope it's not rm :(
/cats1/ directory has /cats2/cats3/ and cats4/ directories inside it. What you type is after the "$"
Lists all the files in the current directory
By itself it will just print all files in current directory
~/Desktop/cats1 $ ls
cats2/ cats4/
With the option -a you can see hidden files (they start with " . " ) Note: ./ and . ./ refers to the outside directories (aka the desktop in this case, where cats1 folder is located
~/Desktop/cats1 $ ls -a
./ ../ .secretCats.txt cats2/ cats4/
You can even see files in directories you aren't even at with:
$ ls <file-path>
~/Desktop/cats1 $ ls cats2/
cats3/
Print Working Directory, prints out the path of the current directory
~/Desktop/cats1 $ pwd
/c/Users/me/Desktop/cats1
clears commands from screen
This is moments after using the clear command
~/Desktop/cats1 $
Change directory, basically your way of travelling the maze that are your files.
~/Desktop/cats1 $ cd <file-path>
Pay attention to what's before the "$" (current location)
~/Desktop/cats1 $ cd ..
~/Desktop $ cd cats1/cats2/
~/Desktop/cats1/cats2 $ cd ..
~/Desktop/cats1 $
Concatenate, display contents of a file Note: Will print out meta-data for images
$ cat <fileName>
~/Desktop/cats1 $ cat .secretCats.txt
Secret, shhh!
Creates file in current directory Note: You can create files without extension
$ touch <fileNane>.<fileExtension>
~/Desktop/cats1 $ touch catDestroyer.md
~/Desktop/cats1 $ ls
catDestroyer.md cats2/ cats4/
Remove, deletes files does NOT put them in the trash, SCaaAARy!!
$ rm <file-you-want-to-delete>
~/Desktop/cats1 $ ls
catDestroyer.md cats2/ cats4/
~/Desktop/cats1 $ rm catDestroyer.md
~/Desktop/cats1 $ ls
cats2/ cats4/
Using the "-rf" flag allows us to delete directories, as well as everything inside of them. Ultimate purge, try to avoid.
~/Desktop/cats1 $ ls
cats2/ cats4/
~/Desktop/cats1 $ rm -rf cats2/
~/Desktop/cats1 $ ls
cats4/
Make empty directory
$ mkdir <directoryName>
~/Desktop/cats1 $ ls
cats4/
~/Desktop/cats1 $ mkdir cats2/
~/Desktop/cats1 $ mkdir cats2/cats3
~/Desktop/cats1 $ ls
cats/2 cats4/
~/Desktop/cats1 $ ls cats/2
cats3/
Remove Directory Note: will only delete an empty directory. Refer to rm for deleting directories and it's contents
$ rmdir <directory_to_remove>
Move. Serves two purposes: When renaming the original file will get deleted once it gets moved to the new file. Note: Renaming to an already used name will overwrite the file with that name (aka, it get's deleted!! Make sure your name is unique!
$ mv <directory_to_rename> <new_file_name>
When moving a file to a new location specify the files (can be more than one) and the location (directory)
$ mv <file1> <file2> <file...> <directory_to_move>
Copy a file into a new location, does not delete original.
$ mv <file1> <directory_to_copy_to>
Manual. When google isn't an option. Opens up the manual for a specific command (ex: ls, mv, man man??!!).
$ man <some_command>
Piping redirects output from command line, to a file you specify. With both characters typed after the command, ">" creates a new file and ">>" appends to an already existing file (creates a new one if none exist)
$ ls >> <file_name>
/cats1/ directory has /cats2/cats3/12coolfile.txt /cats2/cats3/dogscool.txt and cats4/ directories inside it. What you type is after the "$"
Gives user root privileges (admin rights), typed before command. Makes command be run as what's called a "super user"
After typing it, you will be prompted to type password (don't be alarmed if you don't see your password being typed out, it's normal, just type it and press enter once you're done.
$ sudo <some_command>
Allows you to print into bash. This is useful when chaining longer commands to debug what went wrong.
This command will print out whatever "some_string" is.
$ echo <some_string>
A tool for locating files (is Amazing). This command will print out everything in the path specified, that is, files and everything inside directories in that path.
By just typing a " . ", bash will print out everything in the current directory.
$ find <path_to_directory>
$ find .
Using the flag "-name" will lead the terminal to only print out paths for files with that specific name.
$ find <path_to_directory> -name <file_name>
~/Desktop/cats1 $ find /cats2 -name 12coolfile.txt
cats2/cats3/12coolfile.txt
Additionally you can use the " * " wildcard to find files with a specific beginning, or files that are all the same type:
~/Desktop/cats1 $ find /cats2 -name 12*file*.txt
cats2/cats3/12coolfile.txt
Using the -iname flag instead of -name will cause the search to be case-insensitive (aka, doesn't care about caps):
~/Desktop/cats1 $ find /cats2 -iname *FiLe*.txt
cats2/cats3/12coolfile.txt
Using the -delete flag will lead to all files found to be deleted. In this example I combine two flags.
~/Desktop/cats1 $ find /cats2 -name 12*file*.txt -delete
~/Desktop/cats1 $ find .
.
./.secretCats.txt
./cats2
./cats2/cats3
./cats4
Global Regular Expression Print, used to find all text instances in a specified file (will return lines where text was found)
$ grep <text_to_look_for> <file_path>
$ grep david cats2/cats3/12coolfile.txt
david was here
david was also down here
cats are evil! david
Using the "-w" flag will return only those lines with exact matches, that is something like "davidAndFriends" won't return if you're looking for "david" Additionally, the "-i" flag will cause the search to be case-insensitive.
Note: Flags can stack on top of each other, that is, "wi" will do the case, and the exact match search.
$ grep <search_text> <file_to_search> -w
$ grep <search_text> <file_to_search> -wi
Will compare two files line by line, and then identify the lines where they differ
$ diff <file1> <file2>
Extra features, can chain them!
Let's you do several commands sequentially all in one line as long as all preceding commands complete successfully
$ <command1> && <command2> && <command...>
will try the first command and if the first command fails, will do the command after the operator
$ <command1> || <command2>
We use these to edit files in bash, some examples are: - Vim - Emacs - Nano (not as popular)
If file exists, vim will open the file, if not, if you save when closing, vim will create the file.
$ vim <file_name>
vim opens in command mode This can be identified by a lack of description at the bottom of the window.
Essential:
- " i " = Enters insert mode
- Esc key (in insert mode) = Press esc to go back to command mode
- " :q " = To quit
- " :q! " = To quit without saving
- " :wq " = Write and quit
Travel:
- " h ", " j ", " k ", " l ", = left, down, up, right (arrow keys also work)
- " $ " = Moves cursor to the end of the line
- " _ " = Moves cursor to the beginning of line
- " w " and " b " = Moves cursor forward and backward, one word at a time
- " /<some_word> " = will move the cursor to the next occurrence of <some_word>
- " <line_number>gg " = Will move cursor to line
- " dd " = deletes the line the cursor is on
- " u " = Undo
Creates file if doesn't exist if you save (same as Vim) Does not use 'modes' like vim, has GAMES
Note:_Commands here are prefixed by the control key or meta (alt/option) key
When you start emacs, you can start typing right away!
$ emacs <file_name>
Ctrl + :
-
" h " = Bring up help page
-
" x + s " = Save a file
-
" x + c " = Exit emacs
-
" _ " = Undo changes
-
" e " = Moves cursor to the end of the line
-
" a " = Moves cursor to the beginning of line
-
" :q " = To quit
-
" :q! " = To quit without saving
-
" :wq " = Write and quit
meta_key (option/alt) + :
- " f ", " b " = forwards, backwards by one word
- " a ", " e " = Move to the start, end of the sentence
esc + :
- " > ", " < " = Move to end, start of file
$ nano <file_name>
Commands for Nano are at the bottom, those are all!, just press crtl and the key
Aliasing is a powerful tool to create shortcuts for bash
This creates an alias so that when we type "shortcut_name" in bash, <full_command> will run
$ alias <shortcut_name>=<full_command>
To remove an alias we use:
$ unalias <shortcut_name>
You can write scripts to run commands. These can take input! (note that bash files have extension " .sh "
To run the scripts you type:
$ bash <script_name.sh>
Note: From this point on, I will explain the programming language basics for bash scripting, that is, everything coming up is what you would write in the .sh file
You don't need to declare type for variables, to use a variable use " $ " before you type your variable name
This example should declare secret_var and then run the echo command with the value of that variable. All of these accomplish the same thing.
secret_var="example1"
echo $secret_var
echo "$secret_var"
echo "${secret_var}"
if, else, elif statements.
Note: You need spaces between the condition and the bracket.
if [[ <condition> ]]; then
<do something>
elif [[ <condition> ]]; then
<do something>
else
<do something>
fi
Because if statements are so closely related, we will talk about Operators:
-
" -eq " : is an equals operator for numerical comparisons
-
" -ne " : is a not equals operator for numerical comparisons
-
" == " : is an equals operator for string comparisons
-
" != " : is a not equals operator for string comparisons
for loop will iterate from i = 1 to i = 10
for i in {1..10}; do
echo $i
done
This one has steps, sort of like range in Python
for i in {5..50..5}; do
echo "Welcome $i"
done
Here are C-like for loops
for ((i = 0 ; i < 100 ; i++)); do
echo $i
done
<function-name>() {
<stuff it does>
}
<function-name> <value-to-pass> <value-to-pass...>
This is an alternative syntax, is an example with a function declaration, and function call
function myfunc() {
echo "hello"
}
myfunc
Now, when you pass values into a function, they get saved as " $1 ", " $2 " and so on, so:
myfunc() {
echo "Hi $1, it's $2 outside eh?"
}
myfunc "Jenny" "cold"
Will print out "Hi Jenny, it's cold outside eh?"
Note how there was no need to specify how many variables our function asked for when we declared it Make sure you call the function after declaring it, otherwise it won't run!