For most computer users, the Terminal is a fairly alien application.  It has no real user interface – no buttons to push or sliders to control – it’s just a simple text-based interface.  Likely because of this, it can be a scary place.  “I have to type things to get my computer to do stuff?” is a common thought when first faced with the command line.  It seems odd to have to type things like this in order to move documents from one folder to another:

00Lengthy_Terminal_Command

It seems simple enough to go into our Documents folder and drag out all of the HTML documents.  But the command line can be a much more efficient way of doing things.  Just imagine if you had a folder full of hundreds of documents.  Some are HTML, but others are TXT, some RTF and others in the DOC format.  You would have to select each one individually, whereas the command shown above simply searches for the “.html” extension and moves them for you.

Below are several different commands every beginning Linux user should be aware of.  They may not be commands you’ll use every day (and sometimes you’ll use a different tool to do the same job), but they are very handy to be aware of, should the need arise.  In this, the first of two articles, we’ll discuss the ‘cd’ command, and how it is used in the Terminal to navigate through your computer, as well as how to create and delete empty files and folders

For the rest of the article, why not open your Terminal and follow along.

01Open_Terminal

Using ‘cd’ To Change Directories

When you open your Terminal, you’ll be located in your Home folder.  This is your working directory.  To move to a different location (commands that act on files/folders always look in your working directory), we’ll use the ‘cd’ command.  The following commands will come in useful, especially in later commands where we create files or folders, as well as copy and/or move them from place to place (in part two of this 2-part series).

To change your location to a new directory:

cd /path/to/new/directory

An example of this would be to ‘cd’ from the Home folder to the Java folder located in our /opt/ folder.

02CD_to_Java_Folder

We only need to type ‘cd‘ to immediately switch back to your Home folder:

03Return_to_Home_Folder

To return to the previous working directory, ‘cd -‘ does the trick:

04Return_To_Previous_Directory

As you can see from the above screenshot, we were first on the Desktop, before using ‘cd’ to change to the Grand Funk Railroad folder.  Then we used ‘cd’ to navigate to the Documents folder.  We then typed ‘cd -’ and were returned to the Grand Funk Railroad folder on the Desktop.

Finally, type ‘cd ..’ to move up from your current working directory to its parent:

05CD_Up_One_Level

You’ll notice above, we were in the Commands folder on the Desktop, but after this command, our current working directory is the Desktop, one level “up” from the Commands directory.

The ‘cd’ command is as simple as a command can be.  It’s only two characters long, and only has a few options, yet it is incredibly easy to use, makes it simpler to navigate between two folders that aren’t close to each other hierarchically, and is very fast.  What’s not to like?

Create And Delete Files and Folders

Another task that is simple to perform from the Terminal is creating and deleting folders.  Please not that this method of deleting files does not place them into a Trash Can.  So, if you’re in the habit of dumping unused items in the Trash, then looking at them later “just to make sure,” then these commands may not be for you.

Let’s say you want a new text document, and you’ve already used the ‘cd’ command to switch to the correct directory.  The command is as simple as ‘touch filename” like this (we’ll use ‘touch test.txt‘ (without the quotes) to create a document called test.txt”:

06Create_Test.txt_Document

If the document we want to create will be in a different directory than our working directory, we’ll need to use the full path.  In the following example, our working directory is the Desktop, but we want to create that same “test.txt” document in our Documents folder.  Typing touch /home/ericcflem/Documents/test.txt will do just that, as shown below:

07Create_Document_Using_Full_Path

Note: the ‘touch’ command will create a new file as named, but if the file already exists, ‘touch’ will change the date/time stamp to the current date/time.

What about deleting files?  Also quite simple. For this the ‘rm’ command will serve us quite well.  Again, the syntax is simple:

rm file

As an example, we’ll use ‘rm test.txt‘ to delete the ‘test.txt’ document we just created in our Documents folder.

08Delete_Document

This command assumes our working directory is the same as the file to delete.  To delete a file in a different working directory, the full path to the file is necessary.

rm /path/to/file

Here’s what that looks like in deleting the same file, but from a different working directory.

09Delete_Document_in_Different_Directory

Now that we can create and delete files, how about folders to put our documents in?  Just as easy, but with a slightly different command.

To create a new folder/directory:

mkdir directory

In the following screenshot, we’ve created a folder named “Stuff” on the Desktop.

10Create_Folder_With_Full_Path

Note: in the above example, our working directory was our Documents folder, making it necessary to use ‘mkdir /home/ericcflem/Desktop/Stuff/‘ as our command.  If we had already used ‘cd’ to navigate to the Desktop, we could have simply typed ‘mkdir Stuff‘ without the full path.

What if we find a folder we no longer use?  For that we use the ‘rmdir’ command.  The ‘rm’ stands for remove (and we’ll use the simple ‘rm’ command in a bit to delete files), while ‘dir’ stands for directory, thus making ‘rmdir Stuff‘ the command to remove the Stuff folder we just deleted.

11Remove_Empty_Folder

Since the folder was empty, the simple ‘rmdir Stuff’ command worked, but if we had tried ‘rmdir Stuff’ and the Stuff folder wasn’t empty, we would see this error message:

12Cannot_Remove_Directory_Warning

So, how do we delete a folder that isn’t empty?  We use the ‘rm’ command as if we were removing a file, but add one option to it.  We’ll add “-r” to the command (without the quotes, as shown below), to indicate that we want the ‘rm’ command performed recursively.  This means that any file or folder within the named folder will be deleted.  WARNING: this can be dangerous!

So, to remove the Stuff folder, had it had any items placed in it already we would type rm -r Stuff, as illustrated below:

13Remove_Non-Empty_Folder

Just like when creating a folder not in the current working directory, to delete an empty folder/directory when your Terminal isn’t in the same working directory where the folder you’ll be deleting is as shown:

rmdir /path/to/empty/folder

Using the Stuff folder as an example, here’s how to delete it when our Terminal’s working directory is not the Desktop (where the Stuff folder was created):

14Remove_Folder_in_Non-Working_Directory

And that’s it for this part.  We can now ‘cd’ from place to place on our hard drive, creating and deleting files and folders.  In the second part, we’ll learn how to copy and move folders, as well as a command that will show us what’s in a folder.