Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Forum Archive > Absolute Beginner Talk
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Hello, Unregistered You are browsing a READ only archive of the main support categories pre 4/21/2008. You will not be able to post or reply any threads in this section.

Absolute Beginner Talk
The perfect starting place to find out more about computers, Linux and Ubuntu.

 
Thread Tools Display Modes
Old October 10th, 2005   #1
Kyral
May the Ubuntu Be With You!
 
Kyral's Avatar
 
Join Date: May 2005
Location: Potsdam, NY
Beans: 1,646
Ubuntu 9.04 Jaunty Jackalope
Send a message via AIM to Kyral
Terminal for Beginners

I've seen time and time again ( and have done it myself ) people give beginners advice that basically is terminal based. Thing is, half the time we forget that to many of these people the Terminal is a scary, unknown thing. So since I have some free time ( yea classes being cancelled! ) I decided that I would write a quick intro to the terminal.

The terminal. Also called "command-line" or Bash (because Bash is the default shell on 90% of distros). Considered an ancient relic of the DOS days by the Windows community, the terminal in Linux is still strong and kicking in the present day. Why? Well because in most cases its just more powerful. You can do tons of things faster in a terminal than you can in a GUI. Its also very customizable. Ask any Guru and chances are they have a huge .bash_aliases file that allows them shorten a string of commands into a few keystrokes, I know I do. But that isn't the aim of this thing. The aim is to give a primer to the terminal.

So how do you fire this thing up? In GNOME its under either accessories or system tools. Clicking on it will bring up a window with a prompt, which looks something like this:

Code:
kyral@GNUGeneration:~$
Now let me take a second to decipher this, because it does look like code sometimes. The part before the "@" is your username, in my case kyral. The part after it but before the : is your machine name, in my case its my home machine called "GNUGeneration". Last but prolly most importantly is the part between the : and the $. That is your working directory, or in other words what directory you are currently "in". More on that in a sec, but in my case I'm in ~, which is an alias for your home directory. So I'm really in /home/kyral. Keep that in mind, its useful. The $ means you are a user. If you are in a root terminal it will be a #. I don't know why so don't ask me ;P So lets put this together. It means that I am logged in as kyral at the machine called GNUGeneration and I'm in my home directory. Got it? Cool!

Now how do we work this thing? Well my friend its easy. Frankly very easy. I have a directory in my /home called anime. How do I change to it? With the cd (Change Directory) command. Instead of giving an elaborate explination, I'll just show an example and then explain it.

Code:
kyral@GNUGeneration:~$ cd anime
kyral@GNUGeneration:~/anime$
Whoa, what just happened? Well the prompt is almost the same, except now it says ~/anime. Thats what I mean by working directory. I changed directory to anime in ~ and the prompt tells me this so I don't forget. Cool huh? Now what if I want to see what is in there? Well its easy as well. Check out the ls (list) command.

Code:
kyral@GNUGeneration:~/anime$ ls
After War Gundam X                     Mobile Suit Gundam MS IGLOO
Ah! My Goddess                         Mobile Suit Gundam SEED
Ai Yori Aoshi                          Mobile Suit Gundam SEED Destiny
Ai Yori Aoshi Enishi                   Mobile Suit Turn-A Gundam
Azumanga Daioh                         Mobile Suit Victory Gundam
Bleach                                 New Mobile Suit Report Gundam Wing
Cowboy Bebop                           Outlaw Star
DearS                                  Rurouni Kenshin
Full Metal Panic!                      Samurai Champloo
Full Metal Panic Fumoffu and Specials  School Rumble
Full Metal Panic! The Second Raid      S-CRY-ed
Furi Kuri(FLCL)                        SDF Macross
Love Hina                              Speed Grapher
Love Hina Again                        Trigun
Macross 7                              Viewtiful Joe
Macross Zero
Please withhold comments about me being a fanboy....
Anyway the ls command shows you what is in your working directory. Now do you get the concept? Speaking of working directory, what if you forget? Yah I know the prompt is there, but sometimes you overlook that. Well its easy, its called the pwd(Print Working Directory) command

Code:
kyral@GNUGeneration:~/anime$ pwd
/home/kyral/anime
Neat huh? But wait, the prompt and the pwd say different things! Or do they? Remember I said that ~ is an alias to your /home. So they are basically the same things. No matter where you are in the file system, cding to ~ will always bring you back to your /home, so remember that if you get lost. Watch.

Code:
kyral@GNUGeneration:~/anime$ cd /usr/bin/
kyral@GNUGeneration:/usr/bin$
Whoa wait a minute, where am I? Well the prompt tells me I'm in /usr/bin. Now I know where that is, but you may not. So how do you get back? You may have guessed it, but I'm gonna show you anyway.

Code:
kyral@GNUGeneration:/usr/bin$ cd ~
kyral@GNUGeneration:~$ pwd
/home/kyral
See? Back at /home. That reminds me, there are two ways to specify where you want to go. You can specify a relative location, or an absolute one. Yah, confusing. But wait! I shall explain. Remember when I cd'd to anime? That was relative to my current working directory. anime was a directory in my working directory, even though its really /home/kyral/anime, but the shell knew where I was and just sent me there. Now when I changed to /usr/bin, it was outside my home, so I had to use an absolute location (path is frequently used in place of location BTW) notice the leading /. That tells the terminal to start looking at the "root" of the filesystem (not to be confused with the superuser Root). Maybe this will clear it up..

Code:
kyral@GNUGeneration:~$ cd /
kyral@GNUGeneration:/$ ls
anime  cdrom        etc     initrd.img  media  proc  srv  usr
bin    debootstrap  home    lib         mnt    root  sys  var
boot   dev          initrd  lost+found  opt    sbin  tmp  vmlinuz
Welcome to the base of your filesystem. The location of every other file on your Linux computer is defined in relation to this. Yes there is an anime there, but thats because I have another HD mounted there and symlinked into my ~/anime (Symlinks and mounting are another topic, this is terminal time!). Anyway back to home! Do I really need an example here?

Now some final words before we jump to copying and moving files and whatnot. There are another pair of alias I forgot to mention. ".." refers to the directory directly "above" the working directory.

Code:
kyral@GNUGeneration:~/anime$ cd ..
kyral@GNUGeneration:~$
See what happened? I was in ~/anime and I jumped "up" one level. Very nice and useful if you happen to be in one directory and want to change to another thats in the same directory above you. Like..

Code:
kyral@GNUGeneration:~/anime$ cd ../workspace
kyral@GNUGeneration:~/workspace$
See that? I changed from /home/kyral/anime to /home/kyral/workspace. Also notice that its an example of a relative location. Relative to ~/anime, ~/workspace is up one. Get it? Cool.

Last alias. "." refers to your working directory. Now you may be thinking why do I need that? Well, if you are asked to run an executable, you are going to have to specify the absolute path to it, otherwise its going to go looking in /usr/bin, /usr/sbin, and a bunch of other predefined places. By sticking a ./ infront of the filename, you just did give it the absolute path. Oh, this shouldn't be confused with files and dirs that are named with a . in front, like .bash_aliases. A . in front of the file name means its hidden and won't show up with a normal ls command. You have to use the ls -a command for that.

Well this part is done, ON TO PART TWO!

Quick Links to the Guide:
Part 2: Playing with Files, Tab Completion, Bash Aliases
Part 3: Advanced File Ops
Part 4: Harnessing the Power of Apt
Part 5: Permissions
Part 6: Input/Output Redirection and a bit of Sed
__________________
ArchLinux 0.8.0
Associate Member of the Free Software Foundation

Last edited by Kyral; December 28th, 2005 at 05:26 PM.. Reason: Added in quick links to the rest of the Guide
Kyral is offline   Reply With Quote
Old October 10th, 2005   #2
frodon
Ubuntu French Roast
 
frodon's Avatar
 
Join Date: Jun 2005
Location: France
Beans: 6,387
Ubuntu 9.10 Karmic Koala
Re: Terminal for Beginners

I like your anime directory
frodon is offline   Reply With Quote
Old October 10th, 2005   #3
Strangerdave
A Carafe of Ubuntu
 
Strangerdave's Avatar
 
Join Date: Jul 2005
Beans: 96
Ubuntu 7.10 Gutsy Gibbon
Re: Terminal for Beginners

Hey man, thanks for the info. Being new to all this, the terminal is a bit of a scary thing. I know for me, one of the biggest problems in my understanding is what can be done with the terminal and keeping all the commands either in memory or written down.

-SD-
Strangerdave is offline   Reply With Quote
Old October 10th, 2005   #4
Kyral
May the Ubuntu Be With You!
 
Kyral's Avatar
 
Join Date: May 2005
Location: Potsdam, NY
Beans: 1,646
Ubuntu 9.04 Jaunty Jackalope
Send a message via AIM to Kyral
Re: Terminal for Beginners

Terminal for Beginners: PART TWO: File fun!

Well, you know how to move about in the terminal, and hopefully it isn't as scary anymore. But all you know how to do is move around. Thats no fun! I also told you that the terminal can be very powerful, but again I haven't shown it. Well file operations are very fun in the terminal. And powerful as well. Here we go again!

I assume you remember how to naviagate, so I won't go over that again. There are three basic commands for file operations. cp (Copy), mv (Move), and rm (Remove, basically Delete). cp and mv function pretty much the same way, so I'll cover those first.

Okay I'm at home. Time to ls.

Code:
kyral@GNUGeneration:~$ ls
14560-firefox-thunderbird.tar.gz
19506-pinux's-tux-cursors-theme-0.3-cur.tar.bz2
2005-08-03--10.05.26
2005-08-03--10.07.23
2005-09-25--20.15.58
20178-Ambidexter.Silver.tar.gz
ABC Exams
anime
Crystalcursors.tar.bz2
cs142
DarkFirePic.png
debpacks
Desktop
document.png
ffmpeg-0.4.8-2.rh80.dag.i386.rpm
ffmpeg_0.4.8-3_i386.deb
Final Fantasy 3.zip
finddeps.sh
firefox-thunderbird
flash
freenx-0.4.2.tar.gz
GDM-InThisWorld.tar.bz2
gftp_2.0.18-1_all.deb
gftp-common_2.0.18-1_i386.deb
gftp-gtk_2.0.18-1_i386.deb
gftp-text_2.0.18-1_i386.deb
gnome-clipboard-daemon-1.0.bin.tar.bz2
GNOME-GNOMEInSilk_1280x1024.png
hallelujah.mp3
imacgirl_v3_1280x1024.jpg
is400
Jeko_xchat.png
keitaroanimeICON.jpg
keitaroanime.jpg
Manga
MCity-Aero-ng-default-1.1.tar.gz
mozilla-firefox.png
mozilla-thunderbird.xpm
mygraph.php
MyMusic
NVIDIA-Linux-x86-1.0-7667-pkg1.run
ONR_Meeting_10052005.doc
Oral Presentation Proposal.abw
Parallel_Dimensions.jpg
pi
pics_Full_Metal_Panic_Full_Metal_Panic_fmetalp00.jpg
PI.DAT
Presentation Proposal.abw
programs_sourcecode
public.key
Purposed ITL Build Software List.pdf
q2pres2day2.doc
Readme.txt
Resume.odt
Screenshot-1.png
Screenshot.png
sdsf.txt
Sealab 2021 - 101 - 20021008 - Radio Free Sealab.avi
Splash-GNOMEInSilkEvolving2Blurred.png
Splash-Linux_splash.png
StepMania-3.9-rc3
StepMania-3.9-rc3-linux.tar
super_pi
super_pi.tar
System_Stats_3.0
System_Stats_3.0.tar
The_Calling_-_Anime_Wallpaper.jpg
thunderbird_icon2.png
torrents
TransGaming_Drive
UbuntuCodeofConduct-1.0.txt
UbuntuCodeofConduct-1.0.txt.asc
ubuntu-logo.png
Ubuntu-tan_01-1280x1024.jpg
ut2004
web_sketch.xls
wget-log
wget-log.1
workspace
Yah I have a lot of stuff here, but then again its mounted on a 285 GB partition, so its not like I'm strapped. But I can clean it out. So lets play with those two screenshot files I have laying around from when I uploaded them to the UbuntuForums Gallery.

Copy time!

The syntax for cp is

Code:
cp [options] <location of file> <where you want the copy to be made>
Now the locations can either be relative or absolute paths (remember those?) . Now wait a second? What are "options". Well those are optional! (*groan I can't believe I pulled that*). No those are options that affect how cp goes about its business and are prefaced with a "-". Common options are -i (interactive, it asks you to confirm every action), -r (Recurses into directories, means it basically grabs everything in there. Needed for copying directories), -v (Verbose, tells you everything its doing). You stick'em where I put [options] in the syntax. Well, enough talking. You want an example? Good 'cause I was getting bored. Lets copy Screenshot.png to Screenshot2.png

Code:
kyral@GNUGeneration:~$ cp -v Screenshot.png Screenshot2.png
`Screenshot.png' -> `Screenshot2.png'
Noticed I used the -v option. If I hadn't the second line would not have showed up. It just told me that it copied Screenshot.png to Screenshot2.png. A ls will confirm this
Code:
kyral@GNUGeneration:~$ ls
14560-firefox-thunderbird.tar.gz
19506-pinux's-tux-cursors-theme-0.3-cur.tar.bz2
2005-08-03--10.05.26
2005-08-03--10.07.23
2005-09-25--20.15.58
20178-Ambidexter.Silver.tar.gz
ABC Exams
anime
Crystalcursors.tar.bz2
cs142
DarkFirePic.png
debpacks
Desktop
document.png
ffmpeg-0.4.8-2.rh80.dag.i386.rpm
ffmpeg_0.4.8-3_i386.deb
Final Fantasy 3.zip
finddeps.sh
firefox-thunderbird
flash
freenx-0.4.2.tar.gz
GDM-InThisWorld.tar.bz2
gftp_2.0.18-1_all.deb
gftp-common_2.0.18-1_i386.deb
gftp-gtk_2.0.18-1_i386.deb
gftp-text_2.0.18-1_i386.deb
gnome-clipboard-daemon-1.0.bin.tar.bz2
GNOME-GNOMEInSilk_1280x1024.png
hallelujah.mp3
imacgirl_v3_1280x1024.jpg
is400
Jeko_xchat.png
keitaroanimeICON.jpg
keitaroanime.jpg
Manga
MCity-Aero-ng-default-1.1.tar.gz
mozilla-firefox.png
mozilla-thunderbird.xpm
mygraph.php
MyMusic
NVIDIA-Linux-x86-1.0-7667-pkg1.run
ONR_Meeting_10052005.doc
Oral Presentation Proposal.abw
Parallel_Dimensions.jpg
pi
pics_Full_Metal_Panic_Full_Metal_Panic_fmetalp00.jpg
PI.DAT
Presentation Proposal.abw
programs_sourcecode
public.key
Purposed ITL Build Software List.pdf
q2pres2day2.doc
Readme.txt
Resume.odt
Screenshot-1.png
Screenshot2.png
Screenshot.png
sdsf.txt
Sealab 2021 - 101 - 20021008 - Radio Free Sealab.avi
Splash-GNOMEInSilkEvolving2Blurred.png
Splash-Linux_splash.png
StepMania-3.9-rc3
StepMania-3.9-rc3-linux.tar
super_pi
super_pi.tar
System_Stats_3.0
System_Stats_3.0.tar
The_Calling_-_Anime_Wallpaper.jpg
thunderbird_icon2.png
torrents
TransGaming_Drive
UbuntuCodeofConduct-1.0.txt
UbuntuCodeofConduct-1.0.txt.asc
ubuntu-logo.png
Ubuntu-tan_01-1280x1024.jpg
ut2004
web_sketch.xls
wget-log
wget-log.1
workspace
Its there! Now lets remove it. rm uses syntax like this

Code:
rm [options] <path to file>
Again options are to be found. Practically EVERY command has options. Anyway, common options for rm are, -i (Interactive, same as cp, more important in this case), -v (same as cp), -f (force, make it delete. Needed for directories), -r (Recurse, again same as cp). Now look at those options and think about something people tell you about not running as root. Guess what THIS command would do (DON'T DO IT!) if you ran it as root.

Code:
rm -rf /
Yah, kiss your system goodbye, if you didn't hit CTRL+C fast enough. CTRL+C kills any command in progress and brings you back to the prompt. Anyway lets get rid of that screenshot copy.

Code:
kyral@GNUGeneration:~$ rm -v Screenshot2.png
rm: remove regular file `Screenshot2.png'? y
removed `Screenshot2.png'
Wait, it went interactive. Well thats because I overrode rm's default behavior in my alias' file so it always goes interactive unless I use -f. Nifty safeguard ;P

Now for mv. Again mv behaves just like cp, but it moves the files instead of copy. It also renames them. Yah what? It RENAMES? Think about it. When you rename you basically move the file to one of a different name. Oh I forgot the syntax.

Code:
mv [options] <location of file> <place where you want to put it>
It looks like cp! Thats because its almost the same thing. It has most of cp's options (-i, -v) and it also has rm's -f option. And they work the same way! Example time again! This time rename!

Code:
kyral@GNUGeneration:~$ mv -v Screenshot-1.png Screenshot2.png
`Screenshot-1.png' -> `Screenshot2.png'
It was renamed. Coooool. This means that the file known as Screenshot-1.png is now called Screenshot2.png.

Keep in mind you can also do this with directories, but chances are you'll need to use the -r option if you are using rm and cp.

Now on to a couple other things. I have been keeping a really awesome terminal secret from you, one that makes the terminal really powerful. Its called tab completetion. Now what is that?! Well, if you just type out the first few characters of a file or command or directory 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 load of typing. If its not, either type a few more characters and try again, OR hit tab once more and it will call up a list of possible completions. Keep in mind for files it will only work for whats in the path you are typing. Confusing. Yah I didn't word it good. Example time.

Okay I'm trying to get to /etc/apt/ from ~. So I start typing cd /e and hit tab. Well, from / there is only one thing that starts with e, /etc, so it would fill in. Now I have cd /etc/. I add a to get cd /etc/a and hit tab. Ooops, That isn't unique enough. There must be more than one thing in /etc that starts with a. So I hit tab again. Behold

Code:
kyral@GNUGeneration:~$ cd /etc/a
acpi/         adjtime       alsa/         anacrontab    apt/
adduser.conf  aliases       alternatives/ apm/          at.deny
kyral@GNUGeneration:~$ cd /etc/a
Notice that on the tab complete list directories are shown by having a trailing "/" Also keep in mind that I haven't hit return yet to enter the command. So it looks like I have to finish and type "pt" to get to /etc/apt. So see what I mean? When I tried to tab complete /etc/a it only looked in /etc for possible completions. Keep it in mind. And tab completion works with 99% of commands in the terminal for filenames. It also works with commands. Watch.

Code:
kyral@GNUGeneration:~$ ca
caesar         calibrate_ppa  canfield       cardinfo       cat
cal            caller         captoinfo      cardmgr        catchsegv
calendar       cancel         cardctl        case           catman
I typed in ca and hit tab. It gave me all the commands that start with ca. Nice!

Okay there is one more trick. You wanna see what a file contains? There is a command for that! Assuming you have read permissions, you can do it! (Permissions are another thing entirely). Remember I mentioned something called aliases? Well, I have a file that defines them. You wanna see it? The cat command is your friend.

Code:
kyral@GNUGeneration:~$ cat .bash_aliases
alias cp='cp -i'
alias ln='ln -s'
alias mv='mv -i'
alias rm='rm -i'
alias ..='cd ..'
alias ...='cd ...'
alias cd..='cd ..'
alias cd-='cd -'
#alias ls='ls -Cha --color=auto'
alias df="df -h"
alias h=history
alias untgz="tar -xvfz"
alias untbz2="tar -xvfj"
alias aptsource='sudo gedit /etc/apt/sources.list'
alias aptUI='sudo apt-get update && sudo apt-get dist-upgrade'
alias aptI='sudo apt-get install'
alias aptR='sudo apt-get remove'
alias aptS='apt-cache search'
alias aptSh='apt-cache show'
alias debI='sudo dpkg -i'
alias sourcebuild='sudo apt-get source -b'
alias depbuild='sudo apt-get build-dep'
Cool huh? It will display ANYTHING! Even stuff that isn't text. Case in point...

Well there isn't a case in point because when I did it on a png a bunch of nonsense characters flew past and messed up my terminal.

Last thing. You see those sudo commands? Sudo is how we do things in Ubuntu. It grants you Root power for the command that follows it, and then it puts you back as your normal login. Use it, love it.
__________________
ArchLinux 0.8.0
Associate Member of the Free Software Foundation
Kyral is offline   Reply With Quote
Old October 10th, 2005   #5
Kapre
Dipped in Ubuntu
 
Kapre's Avatar
 
Join Date: Jun 2005
Location: Canada
My beans are hidden!
Ubuntu 7.04 Feisty Fawn
Send a message via Yahoo to Kapre
Re: Terminal for Beginners

Kyral - thanks for this very informative info. Can we make this a sticky and also place this in the index of HOW TOs (paging mods)?

K
__________________
"When I'm right no one remembers....When I'm wrong no one forgets.."
Registered Linux User#396300 Get Counted
Use The Guide
After The Guide read the Ubuntu Docs
Basic Linux Commands - Check
Kapre is offline   Reply With Quote
Old October 10th, 2005   #6
KeithO
Just Give Me the Beans!
 
Join Date: Oct 2005
Location: Altamonte Sprin
Beans: 54
Ubuntu 6.10 Edgy
Re: Terminal for Beginners

thanks for the great write up.
KeithO is offline   Reply With Quote
Old October 10th, 2005   #7
BLTicklemonster
Tall Cafè Ubuntu
 
BLTicklemonster's Avatar
 
Join Date: Oct 2005
Location: Rome, Ga
Beans: 2,614
Ubuntu 8.10 Intrepid Ibex
Send a message via MSN to BLTicklemonster Send a message via Yahoo to BLTicklemonster
Re: Terminal for Beginners

This needs to be stickied. Great Job, Kyral! For all us newbies, I say THANK YOU.
BLTicklemonster is offline   Reply With Quote
Old October 10th, 2005   #8
Kyral
May the Ubuntu Be With You!
 
Kyral's Avatar
 
Join Date: May 2005
Location: Potsdam, NY
Beans: 1,646
Ubuntu 9.04 Jaunty Jackalope
Send a message via AIM to Kyral
Re: Terminal for Beginners

Quote:
Originally Posted by BLTicklemonster
This needs to be stickied. Great Job, Kyral! For all us newbies, I say THANK YOU.
Actually my experiance with your thread led me to make this...
__________________
ArchLinux 0.8.0
Associate Member of the Free Software Foundation
Kyral is offline   Reply With Quote
Old October 10th, 2005   #9
BLTicklemonster
Tall Cafè Ubuntu
 
BLTicklemonster's Avatar
 
Join Date: Oct 2005
Location: Rome, Ga
Beans: 2,614
Ubuntu 8.10 Intrepid Ibex
Send a message via MSN to BLTicklemonster Send a message via Yahoo to BLTicklemonster
Re: Terminal for Beginners

I know, lol. Thanks for doing this!!!
BLTicklemonster is offline   Reply With Quote
Old October 10th, 2005   #10
Kyral
May the Ubuntu Be With You!
 
Kyral's Avatar
 
Join Date: May 2005
Location: Potsdam, NY
Beans: 1,646
Ubuntu 9.04 Jaunty Jackalope
Send a message via AIM to Kyral
Re: Terminal for Beginners

I'll do a Part 3 if there is anything else general like symlinks, which are REALLY cool, or stuff specific to Bash. Actually....I have an idea. I'll put it out after I am done with homework and whatnot.

Preview of Part 3:

- Advanced File Ops (Mkdir and Symlinks!)
- Bourne Again Fun (I can hear the groans from the experianced people with this one)
- Aliases
__________________
ArchLinux 0.8.0
Associate Member of the Free Software Foundation

Last edited by Kyral; October 10th, 2005 at 03:20 PM..
Kyral is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:24 AM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry