njosefbeck / devops-and-backend-dev-roadmap

To keep track of my personal progress through the backend and devops roadmaps found at roadmap.sh
0 stars 0 forks source link

Linux Command Line #3

Open njosefbeck opened 5 years ago

njosefbeck commented 5 years ago

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.

cp

Copy files from one directory to another, or from one file into another.

mv

Move files.

rm

Delete files and directories.

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.

sed

Stands for stream editor. Accepts stdin and modifies it based on an expression, before displaying it as stdout. Similar to "find and replace".

njosefbeck commented 5 years ago

Input & Output (I/O) Redirection

"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)

stdin, stdout, and stderr

> 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).

Redirection

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.