Very Basic Linux Command Line Notes

Viktoria | Coding Viking
4 min readMar 9, 2019

It is a cheat sheet for a Linux terminal beginner. You will not find many explanations here. It’s rather a quick answer to your search. Also, you will get an idea of how mighty your small terminal actually is. Maybe the article will inspire you to dig deeper. The information is based on the book: “The Linux Command Line: A Complete Introduction” by William Shotts.

Today we will talk about most frequently used commands as well as I/O redirection.

pwd — display the current working directory, (print working directory) commandls -l — see a list of files and subdirectories contained in the current working directory. By adding -l to the command, we changed the output to the long format.

Most frequently used commands:

cp — Copy files and directories:

copy the single file or directory item1 to file or directory item2:

cp item1 item2

copy multiple items (either files or directories) into a directory:

cp item... directorymv — Move/rename files and directories.
mkdir — Create directories.rm — Remove files and directories.ln — Create hard and symbolic links.

Wildcards (too long to be listed here).

Info commands:

type; which; help

Redirection

I/O redirection allows us to redefine where standard output goes. To redirect standard output to another file instead of the screen, we use the > redirection operator followed by the name of the file. Why would we want to do this? It’s often useful to store the output of a command in a file. For example, we could tell the shell to send the output of the ls command to the file ls-output.txt instead of the screen:

Examples:

Truncate/create a new file:

> file.txt

Append to the file:

>> file.txt

Commands that make use of standard input:

cat — Concatenate files. The cat command reads one or more files and copies them to standard output like so:

cat [filename...]

join files file1.pdf and file2.pdf together:

cat file1.pdf > file2.pdf

sort — Sort lines of text.

uniq — Report or omit repeated lines. The uniq command is often used in conjunction with sort. uniq accepts a sorted list of data from either standard input or a single filename argument (see the uniq man page for details) and, by default, removes any duplicates from the list. So, to make sure our list has no duplicates (that is, any programs of the same name that appear in both the /bin and /usr/bin directories) we will add uniq to our pipeline:

ls /bin /usr/bin | sort | uniq | less

wc — Print newline, word, and byte counts for each file.

wc ls-output.txt  7902 64566 503634 ls-output.txt

grep — Print lines matching a pattern. grep is a powerful program used to find text patterns within files, like this:

grep pattern [file…]

When grep encounters a “pattern” in the file, it prints out the lines containing it. The patterns that grep can match can be very complex, but for now we will concentrate on simple text matches. Let’s say we want to find all the files in our list of programs that have the word zip in the name. Such a search might give us an idea of which programs on our system have something to do with file compression. We would do this:

ls /bin /usr/bin | sort | uniq | grep zip 

There are a couple of handy options for grep: -i, which causes grep to ignore case when performing the search (normally searches are case sensitive) and -v, which tells grep to print only lines that do not match the pattern.

head — Output the first part of a file. Sometimes you don’t want all the output from a command. You may want only the first few lines or the last few lines. The head command prints the first 10 lines of a file, and the tail command prints the last 10 lines. By default, both commands print 10 lines of text, but this can be adjusted with the -n option:

head -n 5 output.txt 

Using the -f option, tail continues to monitor the file and when new lines are appended, they immediately appear on the display.

tail — Output the last part of a file.

tee — Read from standard input and write to standard output and files. The tee program reads standard input and copies it to both standard output (allowing the data to continue down the pipeline) and to one or more files. This is useful for capturing a pipeline’s contents at an intermediate stage of processing.

ls /usr/bin | tee ls.txt | grep zip

--

--

Viktoria | Coding Viking

Engineering Manager by day, Software Developer by night | Occasional PM | Love all things software and people. https://www.instagram.com/coding_viking