CLI Basics (Part 2 of 2)

banner image

In this week’s newsletter, I’ll be presenting the second post in a short mini series on CLI fundamentals. If you’ve missed the first post, you can read it here: seenickcode.com/blog/cli-basics-part-1

We’ll be covering the final list of basic commands all engineers should know.

Let’s go!

Creating Files with touch, echo, >, >>

touch: Creates an empty file or if the file already exists, update its timestamp to the current date and time. We can simply run touch <filename> to create any file will empty content. If the file already exists, the updated timestamp of the file will be “touched” or updated to the current date and time. We can touch multiple files, i.e. touch <file1> <file2> <file3> if we want.

echo: Prints text to the terminal. For example: echo hello will simply will print “hello” on the screen. This command is mainly use in conjunction with other commands that we will discover below, such as inserting content into a new or existing file.

>: is called the “output redirection operator”. We can use > as well, i.e. echo "Hello, World!" > newfile.txt. This command will create a new file called newfile.txt in the current directory and write the text "Hello, World!" into it1. Note: be careful! If the file already exists, its contents will be replaced with the new text.

>>: is a double “output redirection operator”. It appends to existing files or creates new ones. Use when you want to add to file contents without erasing.

Managing Files with rm, cp, mv

Note: be careful to not specify a critical directory, such as / or you could wipe your entire harddrive in one command!

cp: Copies files or directories. Same options, just for copying. We can also copy several files to a directory: cp file1.txt file2.txt /path/to/destination/.

We can copy an entire directory using the -r option as well. What’s even more useful is that we can use a wildcard (“*”) to copy a set of files. Say, we want to copy all “.txt” files, we can simply run cp *.txt /path/to/destination

mv: Moves or renames files or directories, i.e. mv <existing file> <new file or destination>.

Beyond moving a file, this is most commonly used to rename a file or directory, i.e. mv <directory> <new directory name>. We can use the -i option if we want to be prompted if the destination already exists. Lastly, the -u option moves only when the source is newer.

Searching Files with grep

grep: Searches for patterns in files.

The simplest way to use grep is to search for a string in a file: grep <pattern> <filename>. The “pattern” here is most commonly some simple text.

Say I create a file with some content: echo “apple banana cherry” > fruits.txt. I can now search its contents with grep. Say I want to find “cherry” in the file, this command will output the matching lines and in most cases, color highlight the match:

grep -i cherry fruits.txt
cherry

We can search an entire directory with -r: grep -r <pattern> <directory>. Additionally, the -i option performs a case-insensitive search and the -I (capital “i”) to ignore all binary files, for example, I can try to find all occurrences of my username in my coding projects, located in my home directory, “~”:

grep -riIn seenickcode ~

You see how we combined all of these options in one flag? “-riIn”. We could have just specified “-r -i -I -n” separately as well - it’s just a matter of taste.

In more advanced cases, regular expressions can be used. We’ll be covering regular expressions in a future lesson.

Quickly Looking at File Contents with cat, less, more, head, tail

The cat command (it stands for “concatenate”) is most commonly used to display file contents, i.e. cat <file>. We can also combine multiple files: cat <file1> <file2>. We can use the > operator to insert the output into a new file: cat <file1> <file2> > output.txt

The less command, i.e. less <file> is a fun and useful tool for viewing and navigating text files. This will open a temporary file viewer. Use the arrow keys or Page Up/Down to scroll. Press 'q' to quit and return to the command line. You can search for text while in the file with ‘/’. Use 'n' for next occurrence, 'N' for previous.

The more command is very similar to less, except that more is much simpler with fewer features (as they say, “less is more and more is less”!). Covering this command is out of the scope of this lesson. I recommend simply using less and am only mentioning more as an FYI.

The tail command is used to display the last part of files: tail [options] [file(s)]. For example, to display last 10 lines of a file, we do: tail -n 10 <file>. This is mainly used to view real-time updates on say, a log file. We use the -f option to follow changes: tail -f <file>

The head command works the same way, except shows the beginning of a file and you can’t use the -f option.

Combining Multiple Commands with | (pipe)

The pipe operator | (i.e. Shift + ‘\’ on ANSI keyboard layouts, most commonly used in the US) is a powerful tool for combining multiple commands, i.e. <command 1> | <command 2>.

Let’s take a look at a few potentially useful examples.

  1. Sorting directory contents alphabetically with: ls | sort, which will the take the output of the ls command (list files) and “pipe” (i.e. “transfer”) them to the sort command.

  2. Count files in a directory with: ls | wc -l. wc -l here counts the number of lines in a file, so with this command, we can list out all files with a number next to them showing the number of lines in each file. Pretty cool, huh?

  3. Display only text files in the current directory: ls | grep ".txt".

  4. For the adventurous, we can combine a pipe (“|”) with output redirection (“>”).

Say we have a file, “fruits.txt” like so:

apple
banana
banana
cherry

We can output a sorted, unique list of the file’s contents easily with: cat fruits.txt | sort | uniq > result.txt.

This will take the contents of our “fruits.txt”, using cat, pipe its contents to the sort command then pipe that sorted output to the uniq command, which will remove the redundant “banana”, then redirect that output to a new file, “result.txt”.

Amazing how powerful combining these commands are, right?

Conclusion

Remember, we don’t have to remember these commands per se - just soak them in and try them out a bit yourself. I hope you found this guide useful. If you’d like to request more lessons on this topics, such as more advanced commands that we haven’t yet covered, like awk, sed, etc, feel free to email me at hello@seenickcode.com.

Happy coding!

Nick