Other languages Deutsch ; English ; français ; Nederlands ; português brasileiro ; |
Synopsis: This page is written with absolute beginners in mind (mostly those coming from Windows) and deals with the Command line. The goal of this page isn't to teach the computer management with command line, there is plenty of pages about it in the Internet and in this wiki.(see Links) This page, voluntarily light, aims at introduce the reader to something he often doesn't even know the existence, and to prompt him, if he likes it, to go on with other pages. |
Contents
What is the command line for ?
It is a way to give orders to the computer using commands that are characters strings typed in an area called "Console". A question that may have crossed your mind is "Why should I bother learning the command line? The Graphical User Interface (GUI) is much easier and I can already do most of what I need there." Well, you are generally right, but there is situations where the command line is very helpful:
- When the graphical interface doesn't work, unlike Windows, Linux can run without any GUI. The command line, for example, may be the only way to reboot properly.
- Command line are very often more accurate (more options) than a GUI.
- During its execution, the command line gives you more information about the done job (verbose mode). If a graphical application doesn't start for example, try a start in a console, you will know much about the problem.
- Command line needs less hardware resources.
- The commands are always available on any computer running Linux not the graphical tool.
- The commands are roughly the same in any Linux distribution.
- Give and/or receiving help is more easy with command line. It is often confusing and long to explain where to click in the multiple pages of a GUI, while a copied/pasted command in a console is always easy to do. With regard to forward the system answer to the helper, once more a text copy/paste from the console is easy to do while the same answer through a GUI is not always.
How to find a console
Each desktop environment allow to open a windows for command line. It is called Konsole for KDE, Terminal for Gnome, Xfce, LXDE, Mate, ... Its icon looks like an old PC screen.
You can find it in the menu, for example Menu Mageia -> Tools -> Konsole
for KDE or Applications -> System tools -> MATE Terminal
for Mate or Menu Mageia -> Tools -> LX Terminal
for LXDE,...
Another way is to hit simultaneously the keys Alt+Ctrl+Fn, with n being a number between 2 and 6. You are then leaving the graphical interface (but is still running), come back hitting Alt+Ctrl+F1.
What is root ?
As a user, your rights are limited in order to protect the system. You can read files everywhere, but you can modify them in /home/user
subdirectories only.
Root is the "Administrator", he has all the rights, including breaking everything. So be root only when really necessary and be very careful when you are root.
To become root, open a console and enter: su -
(don't forget the -). A password is asked, but not printed of course. To be user again, enter: exit
.
Good to know
Autocompletion
Instead of writing the whole command or directory name, just write the first letters and type the TAB (tabulation) key, the system will complete the name. the advantages are:
- save time
- avoid typo errors
- acts as memory help, if you don't remember a command or file-name spelling, enter the beginning and the system lists all the possibilities matching the first letters. (see next screen-shot).
Case
Linux and the console are case sensitive, that means image.jpg, Image.jpg and Image.JPG are different file-names.
Copy/paste
To copy from an application to a console, select the text as usual and then hit the keys Ctrl+C or choose Edit -> Copy
from the menu.
After that, paste in the console with a central (or wheel) click; or by selecting Edit -> Paste
; or by hitting Alt+Ctrl+V on your keyboard.
To copy from a console to an application, select the text, then use the menu Edit -> Copy
, or hit Alt+Ctrl+C on your keyboard.
After that, paste in the application with a central (or wheel) click; or Edit -> Paste
, or with Ctrl+V on your keyboard.
Commands historical
You can use the arrows keys UP and DOWN to navigate in the commands historical, very useful when you want to re-use a recently used command
It is of course possible to use the console as a file manager (like Dolphin, Nautilus or Thunar). A console opens in the folder /home/<user>
.
- pwd (Print working directory) -> to know where you are
- cd (Change directory) -> to go elsewhere. Example:
cd /home/<user>/Documents/linux
. This example is called an absolute path, it starts at/
. If you are in/home/<user>
, you can more simply writecd Documents/linux
. It is called a relative path, it starts where you are. (take care to the case !) - cd .. -> to go in the parent directory
- ls -lh -> to list what is in the directory. The rights are given in the first column, if they start with a "d", that means it is a directory. Columns 3 and 4 are the owner and the group owner names, then the size, the last saving date and the file name.
Example:
Simple commands to know my computer
Here are some commands to investigate your computer, they are harmless, use them with user rights.
- What is my Mageia version ? ->
cat /etc/release
. cat means print and /etc/release is the file-name housing this information. - And the Linux one ->
uname -a
- How is used my computer memory ->
free
- What is consuming the computer resources ->
top
. You can see here what are the command (command column) consuming the largest CPU (%CPU column) and memory (%MEM column) resources.
This command is always running and updating, use Ctrl+C to stop it.
- How many bad blocks in my partition ->
e2fsck -cyv /dev/sdxy
You must be root to execute this command. Replace x by the HDD letter and y by the partition number, for example /dev/sda5. This partition must be unmounted. If the system says it is mounted, check that you are not using it and enter the command umount /dev/sdxy
.
You will get something like that:
- Last installed packages ->
rpm -qa --last
. Next page with the Space key, exit with Ctrl+Z
If you want more commands to investigate your computer, see the fourth link in this section: Links
Simple commands to modify my computer
Warning! These commands need root rights, thus they may break things if not duly executed as said. |
Here are some commands you may be very happy to find if some day your computer doesn't work properly.
Software management
- Delete the sources ->
urpmi.removemedia -a
- Add sources ->
urpmi.addmedia --distrib --mirrorlist 'http://mirrors.mageia.org/api/mageia.4.x86_64.list'
you must replace mageia4 by the wanted release and x86_64 by i586 if you use a 32 bits version. Note that Nonfree and Tainted sources are installed and activated.
- Update ->
urpmi --auto-update --auto
(execute twice if used after new sources installation)
Mounting and unmounting
These commands are very useful for example if you try to fix a computer using a Live CD/DVD. You have to mount the folders by hand before being able to have access into it.
- Mounting sdb1 into /media ->
mount -t ext4 /dev/sdb1 /media
. ext4 is the sdb1 format. You may have ext3, vfat (DOS) or ntfs-3g (Windows XP and after). - Mounting a floppy ->
mount -t vfat /dev/fd0
- Mounting a CDROM ->
mount -t iso9660 -r /dev/cdrom
- Unmounting ->
umount /dev/sdb1
. Note there is no n before the m in the command name: umount
- Suppose you want to see your USB keys automatically mounted in
/media
instead of/run/media/<user>/<key name>
, open kwrite and copy paste:
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1" |
Save the file in /etc/udev/rules.d/99-udisks2.rules
Others
- To change a driver -> See the fifth link in this section: Links
- To halt properly the system ->
shutdown -h now
without reboot,shutdown -r now
with reboot.