Computers 'n Stuff: Linux toolbox: useful basic console commands to start using Linux

I here share the resolutions for my Linux problems so they might safe somebody some time.

Sunday, May 27, 2007 parmanent link to this post

Linux toolbox: useful basic console commands to start using Linux

LLinux features an extremely powerful command line interface with an amount of commands available that windows users can only dream of. Most of the time I think the command line is faster, more flexible and powerful than any grafical user interface (GUI), especially when it comes to system administration. However, for me as a beginner it was a struggle to find and understand the basic commands you need to maintain and configure a linux desktop. Thus, I decided to write down the commands I personally found to be most frequently used. Maybe this is a kick start for some users who start to use Linux.

General syntax remarks
First of all its important to know that linux commands usually accept options which come after the command itself. The short form of the option is usually preceded by a - (one hyphen) and the long form by -- (two hyphens). If you are not sure what a command does or what its syntax is type the command followed by -h ore --help. eg:

# man -h
# man --help

this will remind you of the syntax and options the command man accepts
man
man is a lifeline for linux. man brings up the manual or man page of any command available on your system instructing you how to use that command and what it is doing. So if you stumble upon a command and you don't now how its used or what its doing then type man in combination with the command you need the information about. For example if you want to know what - man - does or which options for - man - are available type:
# man man

cd
cd is use to change the current directory. If you want to change to the root directory type
# cd /

The prompt will show you the directory you're currently in. Typing cd followed by two dots (..) will bring you one level up in the directory tree.
# cd ..

Typing cd with out any options will bring you to your home directory.
the tab key
hitting the tab key in the console will expand your input depending on the context youre using it in. e.g. linux is trying to guess what you want to type. So if you type m and the tab key linux will list all available commands starting with m. The tab key is particularly useful if you're typing long directory path-names. just type the initial letters and then press the tab key to see if linux can already expand your input to full length.

tab tab
hitting the tab key twice will list you all posible input options in the context your just in.

CTRL + C
pressing ctrl and c at the same time breaks a programm you starte from the command line.

up-arrow and down-arrow key
the up-arrow will bring up your recent iput commands. the down-arrow does the oposite. this way you can browse your input history.

Shift page-up and shift page-down
use this to scroll up and down the console text.

CTRL Alt F1, CTRL Alt F2, CTRL Alt F3 ....CTRL Alt F7
pressing CTRL Alt and one of the function keys F1 - F7 lets you swich to other consoles. The GUI (X window) lives in consel F7


su
If you're working in a console you started from a Xwindow console and you need root access to you system you can use the su command: su will prompt you for the root password and than log you in to a root console.
# su -c "somecommand"

will ask you for your your root password to execute somecommand with root privileges and will exit right after the command has finished. This is useful if you need the root privileges for only a few commands.

ls

this will give you a short list of the directory your currently in. it will return only the directory names.

ll

this will give you a long list of the directory your currently in. it will retrurn the file and directory names including the respective owner and group.

l

will give a similar output like ll put will also list hidden files and directories which are usually preceded by a . (dot).

tar

this command comes in handy if you need to extract tar archives. A lot of software for linux is dirtributed as tar archives.
# tar xvzf somefile.tar.gz

This is the command you will usually have to use to extract an tar.gz archive of the name e.g. somefile. the option x means extract, v triggers the the output of all extracted filenames, z tells tar that is has to extract a zipped archive and the flag f inicates that the filname of the archive to be processed follows.
chown
this will set the owner of a file or directory e.g.
# chown me somefile

This will set the owner of somefile to be me. Test if successful with:
# l somefile


chgrp

this will set which group a file or directory belongs to e.g.
# chgrp users somefile
will set the group of somefile to be users.

chmod

chmod is used to set if a file is writeable (w) readable (r) or execuatable (x) by owner, member of the group and the world. look for example at the following line of the ll output:
-rw-r--r--   6 root root  4096 26. Mai 14:56 somefile 

The first is a hyphen since this is a file. d would indicate a directory and l a link. the first triplet rw- sets the rights for the owner of this file which is root as indicted by the first entry in the list which is the owner. Hes allowed to read and write this file but not to execute it. The second triplet r-- indicates that members of the group root can read and execute this file but not write to it. The same access ist granted to this file to all others which is set by the last triplet of letters r--. now we assume you want to make this file executable. to do this you could use e.g.
chmod a+x somefile

In addition you want to allow members of the group root to write to this file. this could be done by
chmod g+w somefile

In depth information about Linux permissions

md
This command is used to create a directory with the name somedir
# md somedir

rd
This command is used to remove somedir. this works only if the directory is empty
# rd somedir


If you want to remove a directory with all its contained files and subfolders you could use
# rm -r somedir

rm
This command is used to remove a file with the name somefile
# rm somefile

It can also be used to remove folders with all its contents:
# rm -r somedir

find
Find is useful if you know the name of a file or directory but can't remeber where that file lives. to search your complete directory tree for somefile type
find / -name somefile

To find all files of a certen kind e.g. jpg files which end in .jpg use the asterisk * e.g.
find / -name *.jpg

lsmod
Sometimes to install hardware and stuff you need to know which modules are loaded. typing lsmod will return you a list of loaded modules. this list can be rather long so if you're looking for a module you know the name of grep in combination with a pipe comes in handy
| (the pipe)
The commands you used so far read from the standard input and print the output to the standard output which is usually the console so you can read it. But sometimes you prefer to redirect the output form the standardout to the standard input of a second command to process it further. this can be done by a pipe. To commands are linked together by a pipe in this way, e.g.:
# onecommand | nextcommand

This will send the output of onecommand to the nextcommand wich will do some further processing and sends the output to the standard out which usually happens to be your console.
grep
grep will help you to find lines containig some letters or words your interested in. So if you want to know if somemodule is loaded in your mashine you could type:
# lsmod | grep somemodule

grep is also useful if you want to search your logfiles for messages your interested in if you use it in combination with cat.

cat

Cat will print the contents of a file to the standard output which is usually your console.e.g.
# cat /var/log/messages

Will print the content of your messages logfile.this can be quite long. So if you you are only interested in lines of your log file that contain somephrase you could use :
# cat /var/log/messages | grep somephrase

to filter the output of cat
tail
tail somefile outputs the last ten lines of somefile. The -f option will print updates to the file directly to the console so you can trace whats going on. e.g.
# tail -f /var/log/messages

head
head does pretty much the same as tail but outputs the first ten lines
dmesg
This prints out all your kernel messages. Checking kernel messages is good for debugging, e.g. if some modules failed to load. this is how the linux kernel talks to you and tells you what he has been seeing.

write standard output to file >

sometimes you may need to post parts of your logs to a newsgroup for debugging purposes. To do this you can write the output of cat piped through grep to a file e.g.
# cat /var/log/messages | grep somephrase > news.txt

and the post the output which you can find in news.txt

CTRL + Z
pressing ctrl and z at the same time will interupt a programm and fork it to background you started form the command line and that is still running. Use the command fg to bring it back to the foreground or bg to continue the program in the background.

shutdown
shutdown without options does pretty much what you expect. interesting options are:
# shutdown -h now does preety much the same as:
# halt
this asks all applications to gracefully exit and stops all processes in an ordered fashion.
# shutdown -r now
this will reboot the system. Does pretty much the same as
# init 6

init

this is useful for switching runlevels
# init 5
Initiates the runvlevel with everything, network firewoall GUI login and the like.

# init 3 swichtes to a lowere runlevel and stops Xserver and GUI. this is good for debugging of Xserver.
kill
Use kill to terminate a process.
kill -term pid

where pid has to be replaced by the process id. you can find out about the process id by typing:
ps aux

use in combination with grep to reduce output

Some Links to Guides to start with:
Linux Quickstart More absolute Basic you'll need for Linux
Network commands: list of commmands and short explaination
Console commands list of console commands and brief explaination.
start with linux If you are interested to operate Linux than this would be a good point to start.
system administrator guide This is a good guide to learn how to administrate your linux
the file system learn where Linux stores stuff

Labels: ,