Introduction to the Linux command line

Introducing command line tools

Linux has some powerful command line tools with a variety of helpful functions.

Command line tools can, among other things, be used to copy, move, edit and manipulate files and directories as well as navigate around the directories in your computer.

You will find that tasks such as locating, moving and copying files become much faster when you use these tools through the terminal.

Navigating your computer

Your computer has a filesystem where each file and directory has it’s own location or path.

Below is a diagram of a typical Linux filesystem, note that the directories are organised in a hierarchy starting from the root (/) directory.

We can navigate to any directory in this filesystem using command line tools.

When navigating via the command line it helps to imagine you are physically moving through the filesystem – you can think of the paths as bus routes 🚌 and the directories as the bus stops. I’m going to stick with this analogy so buckle up.

Taken from https://www.linuxyogi.com/linux-directory-structure-file-system-hierarchy/

What is a path?

A path describes where a file or directory is located in the hierarchy of the filesystem.

For example the path /home/user1/Documents/ tells us that the Documents directory is located inside the user1 directory, which is itself located inside the home directory.

Current and working directory

You will often hear the terms current and working directory, sometimes interchangeably, but there is a difference.

The current directory is the directory that you are located in. For example, if you navigate to the /home/user1/ directory, that is your current directory.

The working directory is referring specifically to the directory that you are running your script or commands from.

Here lies the difference, the working directory may or may not be the same as your current directory (you do not always need to be in the same directory as your script to run it).

Time to introduce a command that may now sound somewhat confusing:

  1. Print working directory

When you use the pwd command it shows you the absolute path of the directory that is both your working directory and your current directory at that moment.

Imagine you are at a bus stop and you open google maps on your phone to see where you are.

Try out this command in the JSLinux terminal by typing:

pwd

Here you can see the current and working directory is the /root/ directory:

2. Listing files and directories

You can see what is in your current directory using the ls or list command – Think of it as stepping off the bus to take a look around.

In the terminal type:

ls 

Here you’ll find a few files listed including a readme.txt file and a python script bench.py.

Next, add the -l flag and run the command again:

ls -l 

This time we see some more information, such as the date and time the files were last modified, links (for files) or number of subdirectories (for directories) and permissions (we will cover this in more detail in a later post).

3. Making directories

In the terminal you may have noticed we have files but there aren’t any directories, let’s make one!

To create a new directory we can use the mkdir or make directory command, type this into the terminal:

mkdir my_new_directory

Now we have a new directory created inside the /root/ directory, we can use our new friend the ls command to check the directory was created. Your terminal should look like this:

So, we have created a new bus stop on our route! But how do we travel there!?

4. Changing directories

To travel to a directory we use the cd or change directory command – Think of this as getting on the bus and buying a ticket.

Using cd and providing the path will take us where we want to go, for example:

cd /path/to/directory

Let’s try moving to our new directory:

cd my_new_directory

We can check we are in the right directory by using pwd or looking at the terminal prompt:

If we want to move up a single directory up we can do:

cd ../

We can also add to this command to move up two directories, for example cd../../ and so on.

Remember that typing cd on its own will usually take you back to your /home/directory.

Exiting the terminal

Finally, to exit the terminal you type exit and press enter.

There are many other command line tools available to explore! Next we will look at some tools to help us manipulate text files and introduce piping.

I hope you enjoyed this introduction to the world of the linux command line and command line tools…besides….you never know when this information may come in handy…