Command Line / Shell Flashcards
Terminal (or Terminal Emulator)
A program that accepts text based commands and can render text on the screen
Shells
Interpret the commands you type and execute them. Often referred to as “REPL”s:
Read - Read the commands you type
Evaluate - Evaluate those commands, usually by running other programs on your computer
Print - Print the output of those commands
Loop - Give you a new prompt to type another command and repeat
Command to print something
echo
Prefix when you want to use a variable in a terminal command
$
Command to see the commands you’ve typed in the past
history
Keys to use to quickly cycle through your command history
Up and Down arrows
Command to clear terminal screen
clear
OR
ctrl + l
Command to see the filepath of your current working directory
pwd
Command to see the contents of your current working directory or a downstream directory
ls
ls filepath
Command to see the contents of your current working directory (including hidden files)
ls -a
Command to move into (or down) a directory/filepath
cd filepath
Command to move back (or up) directories
cd .. (one directory)
cd ../.. (two directories and so on)
Command to move to home directory
cd ~
Absolute vs Relative Path
Absolute - Filepath that starts at the root of the filesystem
Relative - Filepath that takes into account your current directory
Command to view the contents of a file or files combined
cat filename (single file)
cat filename1 filename2 (multiple files)
Command prints the first or last n lines of a file (5 lines in this example)
head/tail -n 5 filename
Command to view the contents of a file one page (or line) at a time
less filename
less -N filename (w/ line numbers)
Use spacebar to scroll down a page and “b” to scroll back up
Key to press to exit out of the currently running process
“q”
Command to create a new directory inside a current directory
mkdir directory_name
Command to change filename
mv filename new_filename
Command to move file
mv source_filepath destination_filepath
Command to delete a file or empty directory
rm filename
rm -r directory_name
Command to copy a file or directory from one location to another
cp filename destination_filepath/
cp -R current_directory new_directory
Command to search for text in files
grep “example” filename (single file)
grep “example” filename1 filename2 (multiple files and so on)
Command to search for text in current directory and all subdirectories
grep -r “example” .
Command to find files and directories by name
find directory_path -name filename
” * “ character is a wildcard that matches anything, if you’re trying to find filenames that contain a specific word
Command to see which user is logged in
whoami
Command to print out the permissions of each file and directory in long format
ls -l
How are permissions of an individual file or directory visually represented?
10 character string
-: Regular file -rwxrwxrwx
d: Directory drwxrwxrwx
First 3 characters are owner permissions
Middle 3 characters are group
Last 3 characters are public
Command to change the permissions of a file or directory
chmod
chmod -R u=rwx,g=rwx,o=rwx DIRECTORY/FILENAME
Shell Scripts
Text files that contain shell commands
End with .sh
Command to change the owner of a file or directory and it requires root privileges
sudo chown -R root filename/directory