pwd
outputs the name of the current working directory-
ls
lists all files and directories in the working directory -
cd
switches you into the directory you specify -
mkdir
creates a new directory in the working directory -
touch
creates a new file inside the working directory -
cp
copies files or directories -
mv
moves files. It’s similar tocp
in its usage. To rename a file, usemv
with the old file as the first argument and the new file as the second argument. -
rm
command deletes files and directories.rm -r
removes directories -
wc
command outputs the number of lines, words, and characters in file. -
>
redirects standard output of a command to a file, overwriting previous content. -
>>
redirects standard output of a command to a file, appending new content to old content. -
<
redirects standard input to a command. -
|
redirects standard output of a command to another command. -
sort
: sorts lines of text alphabetically. -
uniq
: filters duplicate, adjacent lines of text. -
grep
: searches for a text pattern and outputs it. -
sed
: searches for a text pattern, modifies it, and outputs it. -
clear
to clear the terminal window
ls
The ls
command lists all files and directories in the working directory.
ls -a
– lists all contents, including hidden files and directoriesls -l
– lists all contents of a directory in long formatls -t
– order files and directories by the time they were last modified
Multiple options can be used together, like ls -alt
rm
The -r
is an option that modifies the behavior of the rm
command. The -r
stands for “recursive,” and it’s used to delete a directory and all of its child directories.
Wildcards
In addition to using filenames as arguments, we can use special characters like *
to select groups of files. These special characters are called wildcards. The *
selects all files in the working directory, so here we use cp
to copy all files into the satire/ directory.
cp m*.txt scifi/
stdin, stdout, and stderr
$ echo "Hello"
Hello
The echo
command accepts the string “Hello” as standard input, and echoes the string “Hello” back to the terminal as standard output.
- standard input, abbreviated as
stdin
, is information inputted into the terminal through the keyboard or input device. - standard output, abbreviated as
stdout
, is the information outputted after a process is run. - standard error, abbreviated as
stderr
, is an error message outputted by a failed process.
>
$ echo "Hello" > hello.txt
The >
command redirects the standard output to a file. Here, "Hello"
is entered as the standard input. The standard output "Hello"
is redirected by >
to the file hello.txt. >
overwrites all original content in file.
>>
$ cat glaciers.txt >> rivers.txt
>>
takes the standard output of the command on the left and appends (adds) it to the file on the right.
<
$ cat < lakes.txt
<
takes the standard input from the file on the right and inputs it into the program on the left. Here, lakes.txt is the standard input for the cat
command. The standard output appears in the terminal.
cat
$ cat hello.txt
The cat
command outputs the contents of a file to the terminal. When you type cat hello.txt
, the contents of hello.txt are displayed.
| is a pipe
$ cat volcanoes.txt | wc
|
is a “pipe”. The |
takes the standard output of the command on the left, and pipes it as standard input to the command on the right. You can think of this as “command to command” redirection.
$ cat volcanoes.txt | wc | cat > islands.txt
Multiple |
s can be chained together. Here the standard output of cat volcanoes.txt
is “piped” to the wc
command. The standard output of wc
is then “piped” to cat
. Finally, the standard output of cat
is redirected to islands.txt
.
wc
wc
command outputs the number of lines, words, and characters in file.
sort
sort
takes the standard input and orders it alphabetically for the standard output. Here, the lakes in sort lakes.txt
are listed in alphabetical order.
$ cat lakes.txt | sort > sorted-lakes.txt
uniq
uniq
stands for “unique” and filters out adjacent, duplicate lines in a file.
$ sort deserts.txt | uniq
A more effective way to call uniq
is to call sort
to alphabetize a file, and “pipe” the standard output to uniq
. Here by piping sort deserts.txt
to uniq
, all duplicate lines are alphabetized (and thereby made adjacent) and filtered out.
sort deserts.txt | uniq > uniq-deserts.txt
grep
grep
stands for “global regular expression print”. It searches files for lines that match a pattern and returns the results. It is also case sensitive. Here, grep
searches for “Mount” in mountains.txt.
$ grep Mount mountains.txt
grep -i
enables the command to be case insensitive. Here, grep
searches for capital or lowercase strings that match Mount
in mountains.txt.
$ grep -i Mount mountains.txt
grep -R
searches all files in a directory and outputs filenames and lines containing matched results. -R
stands for “recursive”. Here grep -R
searches the /home/ccuser/workspace/geography directory for the string “Arctic” and outputs filenames and lines with matched results.
grep -R Arctic /home/ccuser/workspace/geography
grep -Rl
searches all files in a directory and outputs only filenames with matched results. -R
stands for “recursive” and l
stands for “files with matches”. Here grep -Rl
searches the /home/ccuser/workspace/geography directory for the string “Arctic” and outputs filenames with matched results.
$ grep -Rl Arctic /home/ccuser/workspace/geography
sed
sed
stands for “stream editor”. It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to “find and replace”.
$ sed 's/snow/rain/' forests.txt
s
: stands for “substitution”. it is always used when usingsed
for substitution.-
snow
: the search string, the text to find. -
rain
: the replacement string, the text to add in place.
In this case, sed
searches forests.txt for the word “snow” and replaces it with “rain.” Importantly, the above command will only replace the first instance of “snow” on a line.
$ sed 's/snow/rain/g' forests.txt
The above command uses the g
expression, meaning “global”. Here sed
searches forests.txt for the word “snow” and replaces it with “rain”, globally. All instances of “snow” on a line will be turned to “rain”.
Environment
Each time we launch the terminal application, it creates a new session. The session immediately loads settings and preferences that make up the command line environment.
We can configure the environment to support the commands and programs we create. This enables us to customize greetings and command aliases, and create variables to share across commands and programs.
~/.bash_profile is file used to store environment settings. It is commonly called the “bash profile”. When a session starts, it will load the contents of the bash profile before executing commands.
Environment variables are variables that can be used across commands and programs and hold information about the environment.
export USER="Jane Doe"
- The line
USER="Jane Doe"
sets the environment variable USER to a name “Jane Doe”. Usually the USER variable is set to the name of the computer’s owner. - The line
export
makes the variable to be available to all child sessions initiated from the session you are in. This is a way to make the variable persist across programs. - At the command line, the command
echo $USER
returns the value of the variable. Note that$
is always used when returning a variable’s value. Here, the commandecho $USER
returns the name set for the variable.
export PS1=">> "
PS1
is a variable that defines the makeup and style of the command prompt. Here we change the default command prompt from $
to >>
.
$HOME
variable displays the path of the home directory.$PATH
variable lists which directories contain scripts. Separated by a colon.
env
The env
command stands for “environment”, and returns a list of the environment variables for the current user.
env | grep PATH
Here the standard output of env
is “piped” to the grep
command. grep
searches for the value of the variable PATH
and outputs it to the terminal.