Intro to the Linux Command Line (with Examples)

I know a lot of developers tend to get overwhelmed when they see the terminal window. But I’m here to tell you my fellow devs, it’s not all that bad. Once you get a hang of the basic commands you’ll be asking yourself why you didn’t learn how to use the terminal earlier!

markus-spiske-666905-unsplash.jpg

Why should you learn how to use the terminal?

Firstly, you may encounter a distribution without a GUI and without the experience of the command line can make getting around difficult. Most importantly, the terminal is a very powerful tool, and gives you more control over your computer. For example, you’re able to make files, directories, and set permissions to those files and directories. It also has the ability to create backups of files before you start editing them. It also make tedious/repetitive tasks a lot simpler! For example, if you have a GitHub, how repetitive does it become to have to open your browser, type in the web address, login to your account, etc. That sentence almost put me to sleep. Nevertheless, with the terminal just type in a few commands and voilà, your code has been pushed to GitHub. But, before jumping ahead and learning how to use GitHub, it’s important to learn the basics of the command line.

Summary:

There are tons of commands that the CLI takes. Just about every command comes with options that are passed. These options are generally prefaced with a dash (-) for letter options, and a double dash (–) for whole word options. This tutorial is made for beginners and will go over a few of the basic commands. Although this is a beginner’s tutorial, I am going to assume you have a general idea as to what the command line is, and the Linux directory structure.

The ls Command

The ls command is one of the most commonly used commands. This command returns the files/directories that are in the directory you’re currently working in.

ls


How do I see all the file in my current directory?

The first command is the -a option. This option returns all of the files within the directory, even the hidden ones.

ls-a.png


How do I get a listing of my files and directories?

You’re able to do this with the ls -l command. Calling this option gives you the ability to see the files and directories permissions, the user, group user, and the date and time the files were modified.

ls-l


How do I distinguish the difference between files and directories?

In order to do this you need to use the -F option. Take notice that the file types are also colored differently.

ls-F

As you can see above, the -F shows that directories have a “/” after their name. Files have their extension name followed by the name of their file. (The command line in is case-sensitive. So the “F” does have to be capitalized.


How do I display the modification date of my files/directories?

Although it doesn’t return the modification dates, the -t command returns your files and directories by the last time they were modified.

ls-t


How do I show my files in reverse order?

This option really comes in handy when you’re working with a lot of files. The option is -r for reverse. Like the above option, you are able to organize your files in reverse order by modification date.

ls-r.png


Combining Multiple Options with ls

One of the many wonderful things the command line can do, is take more than one option at a time. The next few option combinations I’m going to go over are the most used and useful. What is also great is when using these combinations is that they don’t have to be in any particular order when they’re being passed.


Showing Hidden Files in Long List Format

ls-la


Show Long List and the Modification Time in Reverse Order

ls-ltr.png


Show All Item in Long List Form by Modified Date in Reverse Order

ls-latr.png


The cd Command

The cd command lets you change the directory you’re currently working in. All you have to do is type in the cd command followed by the directory name. A cool little keyboard shortcut is to hit the TAB key when beginning to type out the name of the directory, and the rest of the name will be filled out.


How do I change directories?

The way you change directories is by typing in the cd  command followed by the name of the directory

cd

As you can see above the character changes to the Downloads folder. Which indicates your current directory.


How do I move up in a directory?

In my opinion, one of the easiest options to remember is the dot-dot (..) option after the cd command.

cd..png


 How do I get back to the home directory?

No matter what directory you’re working in, if you use the cd command it takes you back to the home directory.

cd1


The pwd Command

The next command is probably the easiest command to remember. It’s pwd; it stands for “print working directory”, which does just that. Prints the current directory you’re currently working in.

pwd


The touch Command

Simply put, this command creates files. Txt files, HTML files, CSS files, etc. To create a file you have to call the command touch followed by the name of a file.

touch.png


How do I create multiple files at once?

You don’t have to limit yourself to making one file at a time. In order to create multiple files, all you have to do is separate the files by a space.

touchfilefile


How do I change the access time of a file that’s already been created?

Although you do not see a change in the command line, you have to remember that the access time is not included in the command line.


How do I change the modification time?

Now we get something that’s noticeable in the command line. The -m option changes the modification time of a file

touch-m


How do I change the timestamp of a file?

Using the -t option allows you to edit the timestamp of a new or existing file. The following way to change the timestamp is year, month, date, hour, minute and seconds hence. YYMMDDhhmm.ss

ls-t


How do I create a file with the same timestamp as another?

Using the -r allows you to update the timestamp of one file with the timestamp of another file. The former file will inherit the timestamp of the latter file entered. Therefore the two files have the same timestamp.

touch-r


Combining the Options with touch

Somewhat similar to the ls command and their combinations you can pass multiple options at once. Which can also come in handy in multiple situations.


Explicitly Set the Access and Modification Time

In this case the option combo does have to be in this exact order for it to be accepted.

touch-ct


The mkdir Command

The mkdir command allows you to create directories. You start the command with the mkdir command followed by the name of the directory.

mkdir


How do I make multiple directories at once?

Similar to the touch command, you can create multiple directories by separating each of them with a space.

mkdir1


How do I make a directory within a directory in one line?

Using the -p option allows you to create a parent, child, grandchild directories by separating them with a back slash “/

mkdir-p


The cat Command

The cat command allows you to read the contents of a file. This can come in handy when you want quick access to the contents of a file, or if you do not have access to a GUI or text editor.

cat


How do I display the text with numbered lines?

Using the -n option returns the text file with numbered lines.

cat-n


How do I search for a keyword in a file?

Another command I want to introduce with the cat command is grep. With grep you are able to search the contents of a file with a keyword. The way you can search is by using the cat filename | grep keyword.

cat-grep

It is important to keep in mind, along with the command line, grep is also case-sensitive.


The mv Command

The mv command allows you to move a file or directory to another existing directory.

mv

Above you can see I have moved fileOne into the parent directory.


How can I move a file or directory to another directory, but not overwrite it?

This option comes in handy when working with a file with a lot of files and directories. You do not want to accidentally overwrite something important.

mv-n

As you can see above fileOne in the /home/parent/ file contains 2 lines of text. While the /home file contains none. When trying to move the latter file into the parent/ directory it rejects the command.


How can I know what mv command is doing?

By using the -v command you can see exactly what the mv command is doing

mv-v


The cp Command

The cp command allows you to copy files or directories from one directory to another existing directory. The options are very similar to that of the mv command. The command goes as follows: cp

cp


How do I copy one directory or file to another directory without overwriting an existing one?

Like mv, using the -n option you are able to stop the command line from overwriting another file of the same name.

cp-n.png

In line 6, I called the cp command with the -n option to copy the fileOne file in the /home directory to the /home/parent. The two files still have different text even after the copy was called.


 How do I copy a directory and all of its contents to another directory?

The recursive option, or -r, allows you to copy directories and all of its contents to another directory.

cp-r

The above picture displays me copying the /home/parentTwo/ directory to the /home/parent/ directory.


How can I know what cp command is doing?

This is another verbose option, which explains what is going on behind the scenes.

cp-v


The rm Command

The final command we’re going to go over is the rm command, which removes files. It should be noted that you should take extra precaution in using this command. The reason being is that once you remove a file with this command it’s nearly impossible to get it back.

rm


How do I delete directories and their contents?

If you try to run the rm command against a directory you will be prompted with the following error:

rm-dir

In order to avoid this error you can run the -r option to delete the directory.

rm-r


How can I know what rm command is doing?

Like seen earlier the command line is verbose, –v, and shows what the command line is doing.

rm-v.png


Conclusion:

This intro to the command line didn’t cover all of the options that the command line has to offer. In order to get access to these simply type in a command followed by –help. Continue to play around with command line and explore the other amazing things it can do.

If you have any questions, please post them below. If you enjoyed this content please share with your friends or someone who would like this! Or if you have something that i can improve, please let me know. Happy coding!

Leave a comment

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started