Introduction to the shell
I've recently joined Eva Dee from include JS on a study group of the Missing Semester lessons on youtube. The first lesson is all about the shell, this post contains my notes about the first lesson- it's a good, introduction to the shell.
Introduction
The command echo
prints out the given argument to the terminal.
example
echo hello
will print
hello
Note: Arguments are separated by white space
- By default running a command it will try to run on the current directory
Installed programs
- Shell uses
environment variables
to check for programs installed. This variables are set and don't change with each restart of the shell - The environment variable responsible to keep a list of all programs installed is called
$PATH
- Path is a list of program paths separated by a colon
:
- Path is a list of program paths separated by a colon
- We can check the programs path by running the command
which
- Running
which echo
will give us/usr/bin/echo
on a unix machine
- Running
- Absolute paths are paths that fully determine the location of the file
- Relative Path is the path that is relative to the place where we are.
- If you are inside your documents folder and have a file caled text.txt the relative path of that file is
./text.txt
- If you are inside your documents folder and have a file caled text.txt the relative path of that file is
Since arguments are separated with white space, you need to deal with them in two ways:
- escape the space with
\
- example:
echo Hello\ World
- example:
- put the name in quotes
- example:
echo "Hello World"
- example:
Useful Commands
mv
allows you to rename a file or move a file if you give a path to the second argument.mv text.txt test.txt
will rename the file text.txt to test.txtmv test.txt /documents/text.txt
will move the file into the documents folder and rename it back to text.txt
cp
allows you to copy a file it takes two arguments - the path and file to copy from and the path to copy tocp text.txt /documents/text.txt
will copy the file text.txt to the documents folder
rm
allows you to remove a filermdir
allows you to remove a directory if it's emptymkdir
allows you to create a directoryman
allows you to pass a program name as argument and read the manual about that programman ls
will give the manual pages of thels
command- press
q
to quit the manual pages
- press
clear
or shortcutctrl+L
clears all terminal text and moves prompt back to the topcat
prints the contents of a filetail
prints the last n lines of a file- We can run
tail -n1
to print the last line of a file
- We can run
grep
let's you search for an input string for a given keywordtee
takes a command and writes it to a file but also to stdout (standandard output - the terminal)- Command
pwd
- print working directory - shows full path of the directory we are in - Command
cd
- change current directory - change to a directory- example:
cd home
- example:
cd - Change Directory
Useful cd arguments
.
means current directory..
means parent directory- using
cd ..
moves working directory to the parent folder - one folder up
- using
~
means home directory-
move to the directory that you were previously in
Example: Moving directories
Let's assume you have the following directory:
home
documents
pictures
If our pwd
is documents, we can move to the home by running the command:
cd ..
Now running pwd
will give us
/home
We can then use the relative path to go back to documents
cd ./documents
We can think of these two as ..
move a directory up and .
as move a directory down.
Let's assume you are inside the pictures
folder, you can go straight to the home folder by running this command
cd ../../
You can see that it moved the current directory first to documents then to home.
Since we moved to the home directory with the previous command, we can run cd -
to move back to the pictures directory. If we run cd -
again we will be moved back to the home directory.
ls- Listing Files in Current Directory
ls
list files in the current directory
We can pass flags or options to commands by using a -
followed by that flag/option argument.
flag
anything that doesn't take a valueoption
anything that takes a value
Example
Running ls -l
will run the ls
command with the -l
flag - this command will run the command with a long listing format (it shows permission, owner, group, size, last changed)
Let's assume you have the following after running ls -l
drwxr-xr-x 1 FabioRosado FabioRosado 24 Jun 13 12:42 _data
- The initial
d
means that a file is a directory - The rest of the letters -
rwxr-xr-x
- are the permissions- The first group of letters -
rwxr-
- are the permissions for the group FabioRosado - The second group of letters -
xr-
are permissions for anyone else that is not in the group. - The last one is everyone else
- The first group of letters -
- The number after -
1
- means the permissions of the group- In this case
1
means that all files are owned by the FabioRosado group
- In this case
FabioRosado FabioRosado
is the user and group- The date is the last time the file was modified
- The final bit, contains the file/folder name
Permissions
Permissions are shown on the shell like the previous example: rwxr-xr-x
Note: they will always follow the pattern
rwx
.
r
means reading permissionw
means writing permissionx
means executing permission-
means you don't have that permission
For directories things are a bit diferent:
r
means if you are allowed to see which files are inside the directoryw
means if you are allowed to rename, create or remove within the directoryx
means if you are allowed to enter the directory- to
cd
into the directory you must have thex
permission set on all parent directories.
- to
We can change the permissions of a file with the command chmod
.
- chmod u+x file Give the [u]ser who owns a file the right to e[x]ecute it
Streams: Combining Programs
In shell you can combine programs by chaining commands, interacting with files, etc.
- Every program has at least two separated streams
- Input stream
- output stream
>
rewire the output of this program to the file on the right<
rewire the output of the file on the right into the left program>>
append output of the program to the right file|
takes the output of the program to the left and make it the input of the program on the right
Example: Using the rewire operators
Running this command will create a file with the contents of the command echo hello
echo hello > hello.txt
Note: Nothing will be printed because the output of the command
echo hello
is being added to the hello.txt file
We can check that the contents of hello.txt do contain the word hello by running the command
$ cat hello.txt
hello
We could combine the two operators (>
and <
) to copy the contents of a file
cat < hello.txt > hey.txt
This means that we are
- Opening hello.txt and pass it to the
cat
command, which will output the text hello - Use that hello text to input it back to a file called
hey.txt
Note: The
cat
command doesn't know about the redirection to thehey.txt
file - the shell handles everything
Now running the command cat hey.txt
we can see that we get back hello
$ cat hey.txt
hello
If we want to append another hello to our file we can use the append operator
$ echo hello >> hey.txt
$ cat hey.txt
hello
hello
Example: Using the pipe operator
We could use the pipe operator to get the last line of the command ls -l
by running the command:
ls -l | tail -n1
We can also write the output of that last line into a file by combining both operators
ls -l | tail -n1 > ls.txt