Open njosefbeck opened 5 years ago
"Through redirection you can direct the input and output of a command to and from other files and programs, and chain together commands in a pipeline." (Codecademy)
> echo "Hello"
> Hello
The echo
command accepts a string as standard input (stdin). "Hello" in this case. Then, it echoes the string back to the terminal as standard output (stdout).
echo "Hello" > hello.txt
Echo command takes the standard input of a string: "Hello", runs, and redirects its output (also "Hello") to hello.txt, in effect copying the string "Hello" into the hello.txt file.
cat oceans.txt > continents.txt
>
takes the standard output of the command on the left, and redirects it to the file on the right. Here the standard output of cat oceans.txt
is redirected to continents.txt
. This overwrites the contents of continents.txt
with the contents of oceans.txt
.
cat glaciers.txt >> rivers.txt
This does the same thing as the first example, except that it ADDS the output of glaciers.txt
to rivers.txt
instead of overwriting it. Thus, >>
takes the standard output of the command on the left and appends 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, the standard input is the string lakes.txt
, which gets used as the standard input for the cat
command. The standard output of running that process appears in the terminal.
cat volcanoes.txt | wc
The |
(pipe) character takes the standard output of the command on the left and pipes it as standard input to the command on the right ("command to command" redirection). Here the output of cat volcanoes.txt
is piped to the wc
command. In turn, wc
outputs the number of lines, words, and characters found in the file.
cat volcanoes.txt | wc | cat > islands.txt
It's possible to chain multiple pipes together. Like the previous example, the stdout of cat volcanoes.txt
is piped as the stdin of wc
. This command's stdout is a string containing the number of lines, words, and characters found in volcanoes.txt
. This stdout is used as the stdin for the cat
command. This string is then the stdout after running cat
, which gets redirected to islands.txt
.
cat lakes.txt | sort > sorted-lakes.txt
Take the standard output of running cat lakes.txt
and pipe it to the sort
command. Then, redirect the stdout from running sort
and redirect it to sorted-lakes.txt
. This text file gets created if it already doesn't exist, or overwrites the previous content if it does.
sort deserts.txt | uniq > uniq-deserts.txt
Take the stdout of running sort deserts.txt
, which is an alphabetically-sorted list of desert names and pipe it to the uniq
command, which removes duplicate desert names, and then redirects the final list to uniq-deserts.txt
.
uniq
stands for “unique” and filters out adjacent, duplicate lines in a file. Here uniq deserts.txt filters out duplicates of “Sahara Desert”, because the duplicate of ‘Sahara Desert’ directly follows the previous instance. The “Kalahari Desert” duplicates are not adjacent, and thus remain. 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.
Commands
pwd
Print the current working directory you're in.
cd
Change working directory.
ls
List the files and directories in the current working directory. Note that options can be combined, like
ls -alt
.ls -a
: List all files, including . files.ls -l
: List all files as a long list, with more details about each file. From left to right the information shown is: permissions, number of hard links, owner name, owner group, file size, time of last modification, and file/directory name.ls -t
: List all files by last modified date.cp
Copy files from one directory to another, or from one file into another.
cp frida.txt lincoln.txt
: Copy frida.txt into lincoln.txt.cp biopic/cleopatra.txt historical/
: Copy cleopatra.txt into historical directory.cp biopic/ray.txt biopic/notorious.txt historical/
: Copy multiple files into historical directory.cp * satire/
: Copy everything in the current working directory into satire directory.cp m*.txt scifi/
: Copy all txt files that start with the lowercase letter m into scifi directory.mv
Move files.
mv superman.txt superhero/
: Move superman.txt into superhero directory.mv blah.txt yes.txt hello/
: Move multiple files into one directory.mv batman.txt spiderman.txt
: Rename batman.txt to spiderman.txt.rm
Delete files and directories.
rm blah.txt
: Delete file from current working directory.rm -r slapstick
: Delete slapstick directory and all its subdirectories from current working directory. (-r option = "recursive)cat file.txt
Outputs the contents of a file to the terminal.
wc file.txt
Outputs the number of lines, words, and characters found in the file.
sort file.txt
Sorts the contents of the file alphabetically.
uniq file.txt
Removes adjacent duplicate items. Duplicate items that are not adjacent remain.
grep
Stands for
global regular expression print
. Searches files for lines that match a pattern and returns the results. It's also case sensitive.grep Mount mountains.txt
: Searches for 'Mount' in mountains.txtgrep -i Mount mountains.txt
: Searches for capital OR lowercase strings that match 'Mount' in mountains.txtgrep -R Arctic /home/ccuser/workspace/geography
: Searches all files in the given directory for the string "Arctic" and outputs filenames and lines with matched results (-R = "recursive")grep -Rl Arctic /home/ccuser/workspace/geography
: Same as above, but only outputs the filenames where matches were found (-R = "recursive", -l = "files with matches")sed
Stands for
stream editor
. Accepts stdin and modifies it based on an expression, before displaying it as stdout. Similar to "find and replace".sed 's/snow/rain/' forests.txt
: Expressions
stands for "substitution".snow
= the text to find, andrain
= the replacement string. Thus,sed
searches forests.txt for the string "snow" and replaces it with "rain". Will only replace the FIRST instance of "snow" on a line.sed 's/snow/rain/g' forests.txt
: Same as above except now, because of theg
(global) flag, sed will replace ALL instances of "snow" with "rain".