GNU/Linux is powerful, very powerful, but truth be told it can also be a daunting experience when trying to learn to utilize the true power behind a GNU/Linux system; the terminal.
Using the CLI, or Command Line Interface, can speed up MANY processes and tasks, once you know how to use it and some of the basic commands for it. This tutorial is not meant to transform you from scared first timer into Linuxbeard poweruser, but rather to give you your first babysteps into the deeper world of your system.
So, let's just jump right into this. This tutorial is assuming you already have a GNU/Linux system installed, and you can access your terminal with su/sudo permissions. If you DO NOT have sudo permissions at the least, you need to contact your systems administrator and get sudo access otherwise certain parts of this tutorial will be impossible for you to follow.
sudo means "Superuser do". In other words, it's how you perform an action as an administrator, without actually logging into your root account by using the command 'su'
So, if you have sudo permissions (which unless it's a company machine or something...) you will, and can do sudo things.
So, you have a computer, you installed Ubuntu/Debian/Manjaro/Redhat/OpenSUSE/Gentoo or whatever your flavour of choice is, on it. Great! But don't you find it annoying when you have to open your file manager such as Dolphin or Caja, click fifty times to get to the directory you want, then click a bunch more times to copy some files, then navigate to where you want them to go, and click some more to paste them? Yeah, you can use keyboard shortcuts for copy/paste, but you're still clicking like a maniac...So, let's speed this process up!
Open your terminal of choice. Depending on your Desktop Environment this could be one of a few different options, and also located in a few different places...So, dig it out, and open it.
Now, typically you are starting in your home folder...So, let's change that. Let's navigate to a different folder!
Hint: If you ever used MSDOS back in the day, this first command will be either nostalgia, or an annoyance.
Ta-Da! You are now in your Documents folder!
The cd command, which stands for 'change directory' itself can also be quite powerful however. You do not need to be in the parent folder of a directory you wish to enter. For example. I can be inside my Documents folder which is located at /home/username/Documents, and then I can visit an entirely different folder without having to go back to my Home folder. This can be done by typing the exact path you wish to cd into.
This can be typed from ANYWHERE, and you will pop directly into your Downloads folder. This rule applies to all things, from changing directories (cd) to copying/pasting/moving/deleting/creating/executing files as well!
But what now? Well, let's make a file, and then move it somewhere else...
To do this, we are going to use a command called 'touch' that simply creates a blank file. You could use any file you wanted really, but let's just make a blank file for this tutorial, so we aren't moving important things all over the place.
But first, let's see what files are currently already IN our documents folder!
As you can see, the 'ls' command, lists all files and folders in your current directory. And then we will make our blank file...
And then list the files again...
And as you can see, you now have a file called 'tutorial'
Okay, so we now know how to navigate into directories...But what do we do with files? Let's try a few things. We are going to be utilizing a few different commands here, so I'll break them down before we start.
So, let's play with our tutorial file. Make sure you are in the Documents folder
Be sure you replace username with your exact CaSe SeNsItIvE username!
Now let's move that file somewhere else:
The syntax for this usually goes as follows for simple commands: COMMAND [LOCATION OF ITEM] [LOCATION DESIRED]
However, if your terminal session is currently inside the folder of the file/item you wish to interact with, you do not need to state the specific source of the root item, only the desired location you wish to copy/move it to.
mv tutorial /home/username/Downloads
You should now find the file inside your Downloads location. Next, we will copy that file back to our Documents folder.
cp tutorial /home/username/Documents
Then remove the one in our Downloads folder:
And finally cd back to the Documents
And voila, our file is back here again, with the other copy gone.
This is the basis of moving things around!
In order to rename a file, you must move it using the mv command, and giving it the new name, or copy it with the cp command and giving it the new name.
Working with folders is slightly different, we must change our command a little. For example:
rm -r /home/username/Documents will remove the entire folder Documents and everything within it.
mv /home/username/stuff will however move the folder 'stuff' and everything within it.
If you ever see the error, "-r not specified; omitting directory" then you must add -r after the initial command, to include other files within the directory. You must also add -r to remove directories.
The last thing we will cover in this part, is making a new folder. Simply, it is the command 'mkdir'
Using it could be done for example, like:
That's it for the basics of file management within the CLI. It may seem like a hassle now, but given time and practice, it becomes much faster and easier to navigate and do system tasks this way. For example.
cp -r ~/stuff2/* ~/Downloads/ && mv ~/Downloads/* ~/Stuff
The above example uses a couple of shortcuts not explained yet, so I'll give a super quick breakdown to show just how fast you can do things.
Let's assume that the folder "stuff2" has 400 files inside of it. I just moved all of the files out of stuff2, into the downloads folder, and then move everything inside the Downloads folder into the stuff folder. Obviously, I could have skipped a step and just gone directly from the stuff2 to stuff folders, but for example sake, that line took me about 7 seconds to type out, and did what would have taken a minute or two to click around and do!
Making a new user is extremely simple via Command Line. Yes, you could click through your Desktop Environment, find the settings, the user accounts area, and click through the procedure of setting up a new user...or....
sudo useradd -m bob
Done. Yes, really, that's it. You could now theoretically log out and change accounts over to 'bob'.
The -m (LOWER CASE!) gives bob a home directory.
useradd makes the user
And obviously bob is the username.
So, we made bob. But what if bob wants to add or change a password? Or what if you want to change your password, or change bobs password FOR him?
passwd is the command we use. It can be used a couple different ways for these examples.
If you are logged in as the user you wish to change the password for, simply enter
passwd and follow the instructions the CLI will give you.
However, if you want to change a different users password, you need to add two things.
sudo passwd bob
sudo, because only an administrator or the user themselves can change someones account (if it's not yours), our passwd command, and the username of the password we are changing.
So, you were surfing websites that maybe you should avoid in the future, got a popup that won't close, and won't stop playing an 8bit melody of 'It's a small world' repeatedly? No problem, we can just kill Firefox and that's that.
the commands we are going to be using are:
The ps aux command is going to list everything running on your machine for you, program and services wise, for ALL users.
putting in the command will spit out a bunch of lines of text for you. One such line, may look like this:
usernam+ 4022 6.3 4.6 2299028 373428 ? Sl 17:33 0:17 /usr/lib/firefox/firefox
The part we want to focus on is called the PID, its the process identfication number. The PID is the FIRST set of numbers you will see on the left.
Once you have figured out which process you want to kill, such as Firefox, and the PID of the process, now we need to kill it.
The command we want to use is the kill command. Most of the time, using it without any options will work fine. However, I've grown to absolutely love the -9 option, with forces the application to kill, forcibly if need be.
So, what we need to do to kill Firefox, is:
kill -9 ####
Replace ##### with the PID of the process and BAM, Firefox for example, is gone!
Okay, this one is super simple, and requires about...actually, you could have already been done before you even read this far.
BAM. Disc space is listed in detail for you. Well, since that was SO easy, you should have no issues, right?
Stay tuned for more CLI tutorials and 'things every GNU/Linux user should know how to do' type tutorials to come!Advertisement
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.