Linux

Basic Linux Commands

Command Line Interface

Introduction

Everything in Linux revolves around one thing, FILES. If you want an introduction to linux, you will need an introduction to how to navigate the file system. This tutorial is a basic introduction to navigating the file system with the command line interface (CLI). If your coming from a backend engineering role, you may be familiar with CRUD operations. Well, I have organized this tutorial to show you how to perform CRUD operations on your files from the command line. By the end of this tutorial you will know how to navigate between directories, create files and directories, read those files, update those files by redirecting output, and remove files perminately from your file system. In doing so, you will learn most of the common commands used to introduce you to linux.

Before we dive into creating files and directories, we first need to learn how to navigate the file system. First let's open our terminal and type:

cd ~/

Let's break down this command:

cd is the command used to change directories. The general syntax is cd /path/to/file

~/ is the path to the home directory for the current user. We can always navigate back to the home directory from any given directory in the file system.

Now what if we are working from the command line and we want to know what folder we are currently working in. We can run the following command:

pwd
>>> /Users/robertcampbell/

As you can see we are currently in the /Users/robertcampbell/ directory which is the absolute path of our home directory. We can also navigate backwards a directory into the parent directory leveraging the cd command like so:

cd ..
pwd
>>> /Users/

After navigating to the parent directory, we ran the pwd command to see that we are in fact in the parent directory. We can move back into our home directory by running cd /robertcampbell. Now let's move on to creating our own files and directories.

Creating Files and Directories

First let's create a directory for this tutorial project called linux-basics. In order to create a directory, we use the mkdir command which stands for "make directory".

mkdir linux-basics

Now, let's move into that directory with:

cd linux-basics

We can also create nested directories using the -p flag. If we want to create two additional folders, for example, tutorial/blog we can use the mkdir command below:

mkdir -p tutorial/blog

Now let's create a file with the touch command:

touch example.txt

Now that we have created files and directories let's dive into how to read these files and inspect their contents

Reading Files and directories

We previously created a file called example.txt and a directory called tutorial. Let's list out all of the files and directories in our current directory:

ls
--- output ---
example.txt
tutorial

Additionally, we can specify the -l flag to get more information about each file and directory:

ls -l

[IMAGE]

We can even open files directly from the terminal and read them ourselves with the open command. Run open example.txt to open the file and add the following text to the file:

""" This is the example file This is the best tutorial ever Linux is awesome we will become Linux pros """

We can now read the contents of the file with:

cat example.txt
>>>This is the example file
>>>This is the best tutorial ever
>>>Linux is awesome
>>>we will become Linux pros

Another userful command for reading the contents of a file is the wc command which stands for word count. This command outputs statistics about the file including the number of words, lines, bytes and characters of the file. Let's see an example

wc example.txt
>>> 3   19    98    example.txt
  • The first column in the output represents the number of lines in the file (count starts at 0, so 3 really means 4 lines).
  • The second column represents the number of words
  • The third column represents the number of characters or bytes are present in the file

We can limit the output leveraging these common flags:

  • -l will only output the number of lines in the file
  • -w will only output the number of words in the file
  • -c or -m will output the number of bytes or characters respectively. The linux documentation states that if your locale doesn't support multibyte characters than these options will be equal

You can test out these flags for yourself in your terminal!

Updating Files and Directories

Previously we opened the file and added contents to the example.txt file manually. But that is not using the full power of the command line. We can redirect the output of one command onto a file without having to open it and manually add the contents ourselves. One way to accomplish this is by redirecting output with the > character. The syntax for redirecting output is:

[command] > path/to/file

For example, we could use the cat command to take the contents of the example.txt and redirect the output to new_file.txt with the following command:

cat example.txt > new_file.txt

Another way to accomplish the same thing is with the cp command, which stands for copy. We could instead run the command below to accomplish the same thing:

cp example.txt > new_file2.txt

As we just covered, the > character allows you to redirect standard output onto a file of your choice. Similarly, two right angles together, >>, allow you to append a single line to a file. Let's look at an example:

echo "new line appended" >> new_file.txt
cat new_file.txt
>>>This is the example file
>>>This is the best tutorial ever
>>>Linux is awesome
>>>we will become Linux pros
>>>new line appended

As you can see we have appended a new line to the file.

Removing Files

Finally, you can remove files when files are no longer needed with the rm command. Previously we created a file called new_file2, which is a copy of example.txt, so that file is redundant and no longer needed. we can remove it like so:

rm new_file2.txt

We can remove directories with the same syntax: rm /path/to/directory. Additionally, if we specify -r flag it will recursively delete any nested directories. Let's run the rm command on the folder tutorial:

rm -r tutorial

Conclusion

In this tutorial we covered most of the basic linux commands needed to start feeling confident at the command line. We learned how to create files and directories with touch and mkdir. We learned how to inspect those files with cat, wc, and ls. We redirected output to both append lines and rewrite files entirely. Finally we removed files that are no longer needed. You are now well equipped to start manipulating your file system at the command line!

Previous
SqlAlchemy