[How to] Beginner intro for linux terminal (command prompt)

Had your computer crash on you, or a website shows wrong, or your printer went dead on you? Come on in...

Moderator: Crew

User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS

[How to] Beginner intro for linux terminal (command prompt)

Post by Maz »

So you're considering installing Linux on your computer?
Welcome to the world, which allows for example really powerful ways to edit textfiles.

First things first. Terminal is a really powerfull tool. No matter that nowadays Linux distributions include easy to use, windows like desktop systems (KDE and Gnome), terminal (like command prompt in Wirudows) is still really powerfull tool. Even though I have desktops installed, I still do almost everything using command terminal.

Now. Sympol of terminal program is usually a picture of a screen. When you klick it, you'll have a screen saying something like

username@localhost username>
or
username@localhost ~]$

following part is just general info about your filesystem, read only the summary from the bottom, if you're not interested

the something@something indicates computer youre on, and the user you are. Last username is the name of the folder you're in (~ mark represents your 'home' folder. Now you're ready to start your journey ^_^

First, something about folders in linux.

'lowest' folder is just / folder (root). In windows it corresponds to C:\
in Linux you'r hard drives have not been separated with any symbols, they're 'mounted' as some folders (I for example have one HDD as /storage/ ).

As you probably noticed, in linux / is used to separate folders instead of \. Your root folder should not contain any files, just other folders.

Following information considers also subfolders. for example when I say you should store all your data in /home/ folder, I mean /home/username/.../.../......... what ever subfolders you might want to create to organize your data :)

Next I list some folders you should not write anything before you know what you do
/boot/ folder contains your kernel (the linux system) image, and other booting related stuff.
/proc/ folder contains 'virtual files' related to current state of system
/dev/ folder contains your hardware, but do not attempt write straight in here anything, when devices are used, they'll be mounted on some other folder. (USB sticks, cds etc. are often mounted in folder /mnt/
/sys/ more system data

Following folders are for real use, but generally all non programs/system related should be written in /home/username/ folder (and subfolders you might want to create).
/bin/ contains binaries (executable files IE applications). Basically that's the case with all bin folders you might find in other folders.
/sbin/ contains system binaries, IE system executables
/home/ contains homefolders for users. That's usually the area where you should store all your data.
/media/ often the cdroms / floppydisks are mounted in folder called media. (If this folder does not exist, it might be the mnt folder instead.)
/opt/ errmmm.. This is a bit mysterious to me.. Some applications seem to like to dwell in here :)
/root/ your root users home.
/selinux/ Fedora includes selinux software, which tries to prevent users to risk the security of the system.
/srv/ errmmm... I do not actually know...
/usr/ users' applications etc related data. Also often contains kernel sources and other odd stuff...
/etc/ As name suggests, this contains quite miscelleaneous data. Often different configurations are stored in here. If you'll start really play with linux, you'll get used to changing configurations in this folder.
/lib/ programming libraries.
/lost+found/ data found & saved from bad HDD sectors.
/tmp/ temporary files
/var/ kinda storage area. One important thing usually located in here are your www pages!

So. Summary. Use /home/ and subdirectories for your downloaded stuff, documents, music, all personal.
use /var/www/html for your www pages.
Beware usage of folders between red and green text.
Last edited by Maz on Sat May 20, 2006 0:49, edited 1 time in total.
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS

Post by Maz »

commands
Basic files & folders things.

changing folders, command is
cd <foldername>
and gettin into one folder below
cd ..
getting straight back to your home:
cd ~

Note, you can either use the name of subfolder straight away, or the full pathexamples:

[Matti@localhost ~]$ cd Desktop
[Matti@localhost Desktop]$ cd ..
[Matti@localhost ~]$ cd /home/Matti/Desktop/
[Matti@localhost Desktop]$ cd /
[Matti@localhost /]$ cd ~
[Matti@localhost ~]$ cd /usr/src/kernels/
[Matti@localhost kernels]$ cd ~
[Matti@localhost ~]$


Adviece! you can use asterix *, and ? in file/foldernames. * as usual, and ? to indicate that character in that location can be any character.
HINT! When using Linux terminal, you can save your fingers by pressing tab key to complete foldr/filenames!
TIP! You can save your fingers by using 3 button mouse & easy copypaste: Activate the text you wish to paste, select the window (and cursor position) where you wish to paste, and press the middle mousebutton. (with mouse with 2 buttons, you can do the same by pressing both buttons simultaneously instead of middlebutton)
The Tip&Hint! You can browse through the previous commands, by pressing up arrow!


NOTE: If file/foldername contains certain special marks like space or ' you need to write \ in front of them as FM\'s\ folder


listing folder contents:
ls
use ls -l to show more specs about the file
You can also use ls path_to_a_folder to display contents of some other folder than the one youre currently in.
create folder
mkdir foldername
remove file(s)
rm filename
remove folders including subfolders & files:
rm -r foldername.

Tip! usually rm -r confirms that you really want to remove a file. If you think it's annoying (like it is if your folder contains lots of files) you can use rm -rf foldername to force removing files & folders without prompt.

moving files / folders
mv which where. (mv includes some clever features, refer to man pages to find out more!)

Editing file.
Usually you wish to use some editor to edit your files. The most usual ones are pico, nano, emacs and vi. vi and emacs are advanced & powerfull editors, but they're not too easy to use... nano, pico (and joe) are simple yet effective basic text editors for terminal window. You can launch them with
pico filename (or nano or joe or vi or emacs).
file will be created when you save your work.

A bit more...

displaying xontents of a textfile
cat filename
(this does not allow any modifications)
displaying first N rows of file:
head -N filename
last N rows:
tail -N filename
searching a line containing sometext from a file:
cat filename | grep "sometext"

Now, the last example showed us one REALLY usefull thing. It's pipelining of command (or piping or dunno, I'm no english speaker)
Basically what pip*ing means, is that you can combine commands so that second command uses output of first command as it's input. So when you give command
cat filename | grep "matchtext"
you actually first output the whole file, and give that output to grep command, which simply filters the line containing matchtext and displays only it.

Now we start to get there! I told linux is effective what comes to textfiles manipulating!

cutting out N first characters from lines:
cat filename | cut -c N-
displaying characters from Nth to Mth
cat filename | cut -c N-M
replacing occurrances with sometext
cat filename | tr "occurrances" "sometext"

Now... Before I'll show you why all of this printing changed texts into console window is so usefull, I'll show you one more examples how to use | effectively with listing of files / displaying of filecontents... ...use command more combined with previous commands:

Now, if you type
ls | more
it displays the contents of the folder, but if screen is filled, more waits for keypress before showing more!

Now, one thing more to add in this post. (and then one reminder).
We surely like the way we can manipulate txt in files, but it's not too handy if we must copy the text dumped in console window, and paste it in texteditor... Especially if the buffer gets flooded so full, that some of the text at start vanishes totally.. Thats why we can use > operator.

command:
cat data05.2006 | grep "20.5.2006" > data20.5.2006
would now write all lines from file data05.2006 containing text 20.5.2006 into file data20.5.2006! So > operator writes text that is normally displayed in consolewindow into a file specified!
NOTE > operator overwrites previous file, if it exists
operator >> appends new text into bottom of existing file, or creates new if theres no existing file

Finally the reminder I promised.
To display contents of a folder, you need to have read rights for the folder.
To remove a file, you need to have write rights for folder it's in!
to display filecontents, you need to have read rights for the file
to write into file, you need to have write rights for the file.
etc etc.
Did you spot the one special thing in this?
To remove a file, you need not have write rights in that file! It's enough if you have write rights for the folder! Why's that?
Because folder is actually a file, containing information about files inside the folder. If you remove one filename in that file, the file is removed, since theres no way for computer to know there once was a file!

ALL OF THESE COMMANDS HAVE LOT MORE OPTIONS THAN PRESENTED IN HERE! SEE THE MAN PAGES TO GET THE FULL GAIN! (I did not want to give you too much at once ;) ) (Potkingish super ego: Bah. Truth is that you do not know all yourself... )