- Command line
- History
- Shell: any program that users employ to type commands (-Wikipedia)
- Interfaces directly with the OS kernal
- Kernal is typically the lowest level component of an OS
- Interfaces directly with the OS kernal
- First shell: Thompson shell
- Written at Bell Labs in 1971
- Lots of features for subsequent shells came from this one
- First modern shells: Bourne shell & C shell
- Bourne shell
- 1979 at Bell Labs
- Every current macOS & Linux distro comes with a version of this shell called
bash
- Bourne Again Shell
- C shell
- Bill Joy as grad student at UC-Berkeley in late 70s
- Bourne shell
- Unix Shell
- Serves as a command language and scripting language
bash
- Written by Brian Fox in 1989 for the GNU project as free replacement for Bourne shell
- Can enter commands or read them from a file (scripting)
- Takes some features from C shell
- Shell: any program that users employ to type commands (-Wikipedia)
- Subjects
- home directory
- Your default starting location
- Represented by
~
- When opening a new terminal window, this is the default location
- bash profile
- stores user-specific information
- found in home folder
~/.bash_profile
- sometimes you'll see
~/.bashrc
instead
- sometimes you'll see
- entering a command
[command] -[argument(s)] input
ls -a
grep npm
ls -al | grep npm
- print to terminal
echo
- present working directory
pwd
- list directory contents
ls
- list
- common arguments
a
: show all files & folders (even hidden)h
: show sizes in readable formatl
: show in long list formatt
: sort by modification time
- make directory
mkdir
mkdir [directory]
- change directory
cd
cd [directory]
- create a file
touch
touch [filename]
- can use with absolute path
- determine file type
file
file [filename]
- remove a file
rm
- remove
- common arguments
r
: remove a directory and all its contents (recursive)f
: never prompt (force)
- rename a file
mv
- move
mv [oldname] [newname]
- move a file
mv
- move
mv [oldlocation] [newlocation]
- copy a file
cp
- copy
cp [origfile] [newfile]
- If new file exists, it will overwrite new file
- common options:
i
: interactive; will prompt before overwriting-R
: copy a directory and all sub directories / files (recursive)
- search file contents
cat
- concatenate files and output in terminal
- common arguments
n
: Show line numbers in result
head
- head of file
head [arguments] [file(s)]
- common arguments
n
: Shows onlyn
lines in the file (head -100
)
tail
- tail (end) of file
tail [arguments] [file(s)]
- common arguments
n
: Shows onlyn
lines in the file (tail -100
)
grep
- global regular expression print
grep [arguments] [phrase] [filelocation]
- common arguments
i
: ignore casen
: show line number in resulto
: only show the matching words or phrases
- file permissions
drwxr-xr-x 6 Brenna staff 204 Jun 27 16:58 terminal-exercises
drwxr-xr-x
0
: file type-
: normal filed
: directory
1-9
: permissionsr
: readw
: writex
: execute1-3
: file owner permissions4-6
: group's permissions789
: everyone else
6
: number of hard linksBrenna
: owner namestaff
: owner group204
: file sizeJun...
: last modified dateterminal-exercises
: file / directory name
chmod
- change mode
- numbers method
chmod ###
read (r)
: 4write (w)
: 2execute (x)
: 1- owner > group > everyone
- man pages
man
- manual pages
man [command]
- command shortcuts
~
cd ~/Downloads
mv ~/Downloads/bacon.txt ~/school/terminal-exercises/
cd
.
- reference current directory in commands
..
- reference next directory up in commands
cd ../Downloads
- aliases
- it's possible to create custom shortcut commands via aliases in your bash_profile
alias [name]='terminal command'
alias cf='touch'
alias gohome='cd ~'
alias project='cd ~/school && nvm use 6.11'
- root / sudo
super user do
- The 'admin' of the console
- Required as a command when administrative action is required
- The terminal window will prompt for password
- Root is a synonym for super user
- variables
- last argument of last command
$_
- home dir
$HOME
- path
$PATH
- Search path
- When an unknown command is typed into the terminal, this comprises the list of directories it will search through to find the executable for the command
- last argument of last command
- run multiple commands
&&
- This will execute a command only if the previous command was successful
mkdir my-folder && cd $_
alias newfolder='mkdir new-folder && cd new-folder'
alias newfolder='mkdir new-folder && cd $_'
;
- This will execute commands regardless of whether or not the previous command succeeds
mkdir new-folder ; cd $_ && touch file-in-my-folder.txt
- stdin, stdout, stderr (standard streams)
- stdin (0)
standard input
- Data fed into the program
- stdout (1)
standard output
- Data printed by the program
- defaults to the terminal
- stderr (2)
standard error
- Messages & errors
- defaults to the terminal
- redirecting output
- to a file
>
- Will write stdout to a filename specified
- Creates a new file if file does not exist
- If file exists, will overwrite content
- Use
>>
to append
- Use
ls > files.txt
echo 'some text' > textfile1.txt
echo 'some new text' > textfile1.txt
echo 'line 1 \nline 2' > textfile2.txt
cat textfile1.txt textfile2.txt > all-files.txt
- to redirect stderr, specify the stream number (2) and the carrot
2>
- from a file
<
- redirects stdout from a file to the program
grep nomnomnom < bacon.txt
- to a file
- stdin (0)
- piping
- sends data from one program to another
- left output feeds into right output
ls . | grep npm
- top
- table of processes
top
- provides a real-time view of current running processes in the OS
- similar to task manager in windows
- kill
kill
- terminates a currently running process
kill [process id]
- compress / uncompress
tar
- tape archive
- also commonly called a 'tarball'
- common options
c
: create an archivev
: verbose; displays all files added to the tarballf
: specify the filenamex
: extract the archiveC
: changes to specified directoryt
: list contents of archivez
: create a gzip archive
tar -cvf example.tar my-project/
(compress)tar -xvf example.tar
(uncompress)- uncompresses in current directory
- there is also a
zip
command
- autocomplete
tab
- Terminal will try to autocomplete directories or files as you type them in
- ide commands
- atom
- subl
- vim
- scripting
- it's also possible to create a file that's a series of commands and execute it
- this is very common in dev ops work, and a very useful skill
sh
orbash
sh [filename]
- Can append any number of commands
- home directory
- History