My favorite thread I have ever read on Ubuntu Forums is "Terminal for Beginners" by a guy named Kyral. I have decided to post a rewrite of his tutorial because his was written in 2005, and no one can comment on that thread anymore! I want some of the people who are new to Ubuntu to have a good place to learn about the terminal and why it is so useful. Also, I want this thread to be a place to request tutorials about other things that can be done through the terminal. If it seems like I'm just rewording his thread… I am… I do not intend to plagiarize his work; most of this is using his examples through my terminal. I have also added some information that I think is useful. So here it is… Terminal For Beginners (Updated).
The terminal is an extremely useful tool for getting things done quickly on your Linux system. It makes installing programs, deleting files, writing to files, etc. much faster in many cases. Most former Windows users are scared off when they are given advice on the forums that involves using the dreaded terminal, but it's not really as complicated as it sounds.
Starting Your Terminal
Let's start by opening a terminal:
Ubuntu users: (Applications > Accessories > Terminal)
Your default terminal is the Gnome Terminal.
Kubuntu users: (KMenu > System > Konsole)
Your default terminal is the Konsole.
Xubuntu users: (Applications > Accessories > Terminal)
Your default terminal is the Xfce Terminal
When I open my terminal I get this:
Let's break down what this means. The part before "@" is your username, in my case, nick. The part after "@" and before the colon ":" is they name of your machine, in my case it's my laptop called "ubuntu". The part between the colon and the "$" is very important, this is your working directory. Your working directory is the directory (folder) that you are currently "in".
Yours is probably like mine and says "~"… This is an alias for your current home directory. So really I'm in /home/nick. The "$" means you are a user, "a #" means that you are in a root terminal. I wouldn't suggest working in a root terminal.
So let's put it all together… this means that I am logged in as nick, at the machine named ubuntu, and I'm in the home directory. Simple enough, right?
Navigating
Now, how do we navigate around in this thing? I have a folder called "Music" in my home directory, so let's go there. We will use the cd (Change Directory) command to get there. Here is an example:
Code:
nick@ubuntu:~$ cd Music
nick@ubuntu:~/Music$
Did you notice that my command prompt changed? The last part now says "~/Music" which means that I am now working in my Music folder, and the prompt tells me this so I don't forget. Cool huh? By the way, the terminal is case sensitive, so CAPS do matter. "Music" and "music" are two different things.
If I were using the GUI (Graphic User Interface – All those fancy windows, buttons, and such.) I could see what files were in that folder. How can I do that in the terminal? It's simple, use the ls (List) command! Let's try it…
Code:
nick@ubuntu:~/Music$ ls
12 Stones Five Iron Frenzy Of The Son
Audio Adrenaline Forgiven Party Music
Autumn Ashley Great Days End Point Blan_k
Because We're Loved Jacob's Well Radio Deadspace
Ben Harper James Taylor Relient K
Blank Blue Sky Jars of Clay Sevenglory
Bondservant Jennifer Knapp Shane and Shane
Bondservant Rough Mix Jeremy Camp SiDE Walk Slam
Casting Crowns Jonah 33 The Migraines
Creed Kutless The White Scarf Scandal
Dane Cook Matchbox 20 Third Day
DC Talk Michael Jackson Vroom
Disciple Mike Lee Vroom Compilation
Everybodyduck Mitch Hedburg Warren Barfield
Family Force 5 Nirvana
nick@ubuntu:~/Music$
The ls command shows you what is in your working directory. Do you see how that works? Speaking of the working directory, what if you forget? Yeah the prompt tells you, but there is another way to know. It's the pwd (Print Working Directory) command. This may sound useless right now, but if you ever decide to write a script, you may find this to be a useful tool.
Code:
nick@ubuntu:~/Music$ pwd
/home/nick/Music
nick@ubuntu:~/Music$
Neat huh? You'll notice that right now the prompt and the output of pwd seem to say different things, but remember when I said that "~" is an alias to your home directory? So they basically say the same thing. No matter where you are in the file system, using cding to "~" will bring you back to your home directory, so remember that if you get lost. Watch…
Code:
nick@ubuntu:~$ cd /usr/bin
nick@ubuntu:/usr/bin$
Hey wait! I'm stuck in /usr/bin, but how do I get back to the home directory? You probably already know, but I'll give you an example anyway.
Code:
nick@ubuntu:~/Music$ cd ~
nick@ubuntu:~$ pwd
/home/nick
See? We're back at our home directory again.
It's probably a good time to mention that there are two ways to specify where you want to go. You can specify an absolute location, or you can specify a location relative to your working directory. If that is confusing, don't worry, examples are coming…
Remember when I cd'd to Music? That was relative to my current working directory. Music was a directory in my working directory, even though it's really /home/nick/Muisic, but the shell knew where I was and just sent me there. Now when I changed to /usr/bin, it was outside my home directory, so I had to use an absolute location (some people call it an absolute path in case you ever see someone tell you to navigate to /path/to/file) notice the leading /. That tells the terminal to start looking at the "root" directory (not to be confused with the superuser Root). Maybe this will clear it up…
Code:
nick@ubuntu:~$ cd /
nick@ubuntu:/$ ls
bin dev initrd lib mnt root sys var
boot etc initrd.img lost+found opt sbin tmp vmlinuz
cdrom home initrd.img.old media proc srv usr vmlinuz.old
Welcome to the base of your filesystem, the "root" directory. The location of every other file on your Linux computer is defined in relation to this. Now, let's get back to home! I bet you have that mastered by now!
Now, before we start doing things with files, there are a few aliases that I haven't mentioned yet.
".." refers to the directory directly "above" the working directory.
Code:
nick@ubuntu:~/Music/Vroom$ cd ..
nick@ubuntu:~/Music$
See what happened? I was in ~/Music/Vroom and I jumped "up" one level. This is very useful when you are in one directory, but you want to be in another directory that is in the same directory above you. Like this:
Code:
nick@ubuntu:~/Music$ cd ../Documents
nick@ubuntu:~/Documents$
See that? I changed from /home/nick/Music to /home/nick/Documents. Also, notice that it's an example of a relative location. Relative to ~/Music, ~/Documents is up one. Get it? Got it? Good!
Another alias, "." refers to your current working directory. Now you may be thinking, "Why do I need that?" Well, if you are asked to run an executable file, you are going to have to specify the absolute path to it, otherwise its going to go look for it in /usr/bin, /usr/sbin, and a bunch of other predefined places. By sticking a "./" in front of the filename, you just did give it the absolute path, using an alias, which is usually much easier and shorter. However, don't confuse this with files and directories that begin with a period. A period in front of a file means that it is hidden and won't show up in a normal ls command, or in the GUI. You have to use the ls –a( command for that in the terminal. And hitting (Ctrl + H) will show them in the GUI.
Well, now you should feel comfortable navigating around with the terminal; let's put that knowledge to use!
There are three basic commands for file operations. cp (Copy), mv (Move), and rm (Remove, which means to Delete). cp and mv work in similar ways, so we'll cover those first.
Copying Files
Let's take a look at what's in my home folder...
Code:
nick@ubuntu:~$ ls
Customizations Documents Music Screenshot.png Videos
Desktop Downloads Pictures Tux Guitar
nick@ubuntu:~$
I'm pretty organized, so I had to take a screenshot to give you a simple example . So, let's make a copy of that screenshot.
The syntax for cp is:
Code:
cp [options] <location of file> <where you want the copy to be made>
So, you're probably wondering what "options" are. Well, they are options that effect how cp does what it does. If you ever wonder about the syntax and options of a command you can view the manual page for that command right from your terminal!
Just type:
Oh, and you hit "Q" to exit that manual page. It took me a while to realize that... I used to just exit the terminal and open a new one .
Common Options are:
-i (Interactive, Asks you to confirm every action)
-r (Recursive, Includes all the files, folders, and sub-folders, Required for directories.)
-v (Verbose, Tells you more about what it is doing.
Now, we can use either relative or absolute paths (Remember those?). So, example time. Let's copy Screenshot.png to Screenshot2.png...
Code:
nick@ubuntu:~$ cp -v Screenshot.png Screenshot2.png
`Screenshot.png' -> `Screenshot2.png'
nick@ubuntu:~$
I used the verbose option to show you how it works. If I hadn't used it the second line wouldn't be there. Let's take a look at our Home folder now...
Code:
nick@ubuntu:~$ ls
Customizations Documents Music Screenshot2.png Tux Guitar
Desktop Downloads Pictures Screenshot.png Videos
nick@ubuntu:~$
... and there it is! Simple enough, right?
Now let's remove it!
Removing Files
rm uses this syntax:
Code:
rm [options] <path to file>
Some common options for rm are:
-f (Force; Force it do delete; This is needed for directories)
-i (Interactive; Same as cp except it's more important in this case)
-r (Recursive; Same as cp)
-v (Same as cp)
In order to delete important system files on Linux you have to run the command as root (Using sudo... we'll get to that later). Now, look at those options and guess what this command would do (Never run this command!!!).
You guessed it! It would delete EVERYTHING! You would need to hit Ctrl+C really fast, or you'd lose it all! (Ctrl+C kills any command in progress and returns you back to the prompt) I intentionally left sudo out of that command just in case someone wanted to try it.
Anyways, let's remove that screenshot copy. This time we'll use an absolute path instead of using an alias or removing it relative to our working directory. Oh, and we'll use the interactive option too.
Code:
nick@ubuntu:~$ rm -i /home/nick/Screenshot2.png
rm: remove regular file `/home/nick/Screenshot2.png'? y
nick@ubuntu:~$
Now it is deleted, and it asked my permission first. That's a good safe-guard to use. Some people even make an alias that always applies the interactive option to the rm command, but we'll talk about aliases later.
Moving Files
Let's look at mv. The syntax for mv is pretty much the same as cp...
Code:
mv [options] <location of file> <place where you want to put it>
... it has all the same options as cp, and some more. Check out the manual page... remember how do do that?
You can use mv to rename also. You can either move the file and keep the same name, or you can change the name. Let me show you... This time I'll use the verbose option and a relative path.
Code:
nick@ubuntu:~$ mv -v ./Screenshot.png ./DesktopPicture.png
`./Screenshot.png' -> `./DesktopPicture.png'
See... the verbose option told me what the command did, and the "./" made it relative to my working directory. The file was moved from /home/nick/Screenshot.png to /home/nick/DesktopPicture.png ... so basically I just renamed the picture. Get it?
You can move directories too. Here is an example... I have a folder "/home/nick/Documents/Excel Sheets", but I don't actually have any Excel files, so let's rename that folder to "Spreadsheets".
Now is a good time to talk about "escaping". Since that folder has a space between Excel and Sheets, we need to escape the space. If we did this...
Code:
mv -r /home/nick/Documents/Excel Sheets
... it would confuse what you are trying to say. It would try to move a folder called "Excel" to your working directory and rename it to "Sheets". Try it out sometime if that is confusing. So, how do we get it do recognize that space? We escape it with a "\". Let me show you...
Code:
nick@ubuntu:~$ mv /home/nick/Documents/Excel\ Sheets /home/nick/Documents/Spreadsheets
nick@ubuntu:~$ cd Documents
nick@ubuntu:~/Documents$ ls
Julie's Documents Nick's Music Spreadsheets
Nick's Lessons Product Keys Word Documents
nick@ubuntu:~/Documents$
See, it changed the name of the folder. You could also use quotes to make it recognize that space. It would look like this...
Code:
mv /home/nick/Documents/"Excel Sheets" /home/nick/Documents/Spreadsheets
See? However, if want to remove or copy directories that have files in them... you will have to do it to the files in that folder too. So, you'll have to use the -r option.
Uses For The Recursive Option
So, if I wanted to delete that Spreadsheets folder I would have to do this:
Code:
rm -r /home/nick/Documents/Spreadsheets
That would delete the folder and all the files and all the sub-folders in it.
Tab Completion
Now for a time saver, tab completion. If you type out the first few characters of a path or command and hit tab, the terminal will try to complete the name for you. If its obvious what you want it will instantly complete it for you, saving you a lot of typing. If it's not so obvious, either type a few more characters and try again, OR hit tab once more and it will call up a list of possible completions.
For instance, if I want to type /home/nick/Documents... I could just type ~/Doc and hit tab. It would finish it for me. I needed Doc because I have other files or folders that start with "D". If I tried to type ~/D and hit tab a few times it would show me that I have 3 folders that start with D... let me show you:
Code:
nick@ubuntu:~$ cd /home/nick/D
Desktop/ Documents/ Downloads/
nick@ubuntu:~$ cd /home/nick/D
See? Tab completion, aliases, and relative locations are great time savers!
WOW! This has been a longer project than I thought... I now have even more respect for Kyral! lol! I think I'm going to do like him and do a part 2 and 3 since there is much more information to cover.
However, I want to know if this is helping people, and if it is worth the effort. His post was awesome to me, so I hope this is a good learning experience for the "Absolute Beginners". Let me know what you think!
Bookmarks