Sites


Introduction à la ligne de commande

CommandLineIntro: BasicCommands

Commandes basiques

Maintenant, vous avez quelques connaissances de base au sujet des répertoires et des fichiers et vous pouvez interagir avec l'interface en ligne de commande. Commençons par apprendre quelques commandes que vous utiliserez plusieurs fois par jour.

ls

La première chose que vous devez savoir avant de commencer à créer et modifier des fichier est Qu'y-a-t-il déjà ici ? Avec  uen interface graphique vous le sauriez en ouvrant un dossier et en regardant son contenu. En ligne de commande, vous utiliserez le programme ls pour effectuer la même opération de listaeg.

$ ls
Desktop  Documents  Music  Photos
Par défaut, ls utilisera un affichage très compact. De nombreux terminaux affichent les fichiers et sous-répertoires dans des couleurs différentes qui représentent leur type. Les fichiers standard n'ont pas de couleur particulière. Quelques formats de fichier comme JPEG ou PNG, ou encore tar et ZIP, sont souvent colorés différemment. Cela est aussi vrai pour les fichiers que vosu pouvez éxecuter et les répertoires. Essayez ls vous-même et comparez les icônes de votre gestionnaire de fichier graphique avec celles utilisées par ls.  Si la sortie n'est pas colorée, vous pouvez appeler ls avec l'option --color:
$ ls --color

man, info & apropos

Vous pourrez en savoir plus sur les options et les arguments d'une commande en utilisant un autre program appelé man (man étant l'abbréviation de manuel). Précédez de la façon suivante:

$ man ls

Ici, man se voit demandé d'afficher la page de manuel pour  ls. Vous pouvez utiliser les flèches du clavier pour faire défiler le texte et vous pouvez quitter la page en appuyant sur  la touche q  (pour quitter).

Une alternative pour obtenir une documentation compréhensible pour un programme est d'invoquer info à la place de man:

$ info ls

C'est particulièrement effectif pour apprendre à utiliser des programmes complexes. Vous pouvez naviguer dans la documentation  info à l'aide de l'éditeur Emacs, ce qui augmente grandement la lisibilité. Mais vous devriez maintenant être prêt pour faire votre premier pas dans le monde gigantesque d'Emacs.  Vous pouvez le faire en invoquant:

$ emacs -f info-standalone
that should display the Info main menu inside Emacs (if this does not work, try invoking emacs without arguments and then type Alt + x info, i.e. by depressing the Alt key, then pressing the x key, then releasing both keys and finally typing info followed by the Return or Enter key).  If you type then m ls, the interactive Info documentation for ls will be loaded inside Emacs.  In the standalone mode, the q key will quit the documentation, as usual with man and info.

Ok, now you know how to learn about using programs yourself.  If you don't know what something is or how to use it, the first place to look is its manual and information pages.  If you don't know the name of what you want to do, the apropos command can help.  Let's say you want to rename files but you don't know what command does that.  Try apropos with some word that is related to what you want, like this:

$ apropos rename
...
mv (1)               - move (rename) files
prename (1)          - renames multiple files
rename (2)           - change the name or location of a file
...

Here, apropos searches the manual pages that man knows about and prints commands it thinks are related to renaming.  On your computer this command might (and probably will) display more information but it's very likely to include the entries shown.

Note how the program names include a number besides them.  That number is called their section, and most programs that you can use from the command line will be in section 1.  You can pass apropos an option to display results from section 1 manuals only, like this:

$ apropos -s 1 rename
...
mv (1)               - move (rename) files
prename (1)          - renames multiple files
...

At this stage, the section number isn't terribly important.  Just know that section 1 manual pages are the ones that apply to programs you use on the command line.  To see a list of the other sections, look up the manual page for man using man man.

mv

Looking at the results from apropos, that mv program looks interesting.  You can use it like this:

$ mv oldname newname

Depending on your system configuration, you may not be warned when renaming a file will overwrite an existing file whose name happens to be newname.  So, as a safe-guard, always use `-i' option when issuing mv like this:

$ mv -i oldname newname

Just as the description provided by apropos suggests, this program moves files.  If the last argument happens to be an existing directory, mv will move the file to that directory instead of renaming it. Because of this, you can provide mv more than two arguments:

$ mv one_file another_file a_third_file ~/stuff

If ~/stuff exists, then mv will move the files there.  If it doesn't exist, it will produce an error message, like this:

$ mv one_file another_file a_third_file stuff
mv: target 'stuff' is not a directory

mkdir

How do you create a directory, anyway?  Use the mkdir command:

$ mkdir ~/stuff

And how do you remove it?  With the rmdir command:

$ rmdir ~/stuff

If you wish to create a subdirectory (say the directory bar) inside another directory (say the directory foo) but you are not sure whether this one exists or not, you can ensure to create the subdirectory and (if needed) its parent directory without raising errors by typing:

$ mkdir -p ~/foo/bar
This will work even for nested sub-sub-...-directories.

If the directory you wish to remove is not empty, rmdir will produce an error message and will not remove it.  If you want to remove a directory that contains files, you have to empty it first.  To see how this is done, we will need to create a directory and put some files in it first.  These files we can remove safely later.  Let's start by creating a directory called practice in your home and change the current working directory there:

$ mkdir ~/practice
$ cd ~/practice

cp, rm & rmdir

Now let's copy some files there using the program cp.  We are going to use some files that are very likely to exist on your computer, so the following commands should work for you:

$ cp /etc/fstab /etc/hosts /etc/issue /etc/motd .
$ ls
fstab  hosts  issue  motd

Don't forget the dot at the end of the line!  Remember it means "this directory" and being the last argument passed to cp after a list of files, it represents the directory in which to copy them.  If that list is very long, you'd better learn using globbing (expanding file name patterns containing wildcard characters into sets of existing file names) or some other tricky ways to avoid wasting your time in typing file names.  One trick can help when dealing with the copy of an entire directory content.  Passing to cp the option -R you can recursively copy all the files and subdirectories from a given directory to the destination:

$ cp -R . ~/foo
$ ls ~/foo
bar  fstab  hosts  issue  motd
$ cp -R . ~/foo/bar
$ ls -R ~/
~/foo:
bar  fstab  hosts  issue  motd

~/foo/bar:
fstab  hosts  issue  motd

In this case the current directory has no subdirectories so only files were copied.  As you can see, the option -R can be passed even to ls to list recursively the content of a given directory and of its subdirectories.

Now, if you go back to your home and try to remove the directory called practice, rmdir will produce an error message:

$ cd ..
$ rmdir practice
rmdir: failed to remove 'practice': Directory not empty

You can use the program rm to remove the files first, like this:

$ rm practice/fstab practice/hosts practice/issue practice/motd

And now you can try removing the directory again:

$ rmdir practice 

And now it works, without showing any output.

But what happens if your directories have directories inside that also have files, you could be there for weeks making sure each folder is empty!  The rm command solves this problem through the amazing option -R, which as usual stands for "recursive".  In the following example, the command fails because foo is not a plain file:

$ rm ~/foo/
rm: cannot remove `~/foo/`: Is a directory

So maybe you try rmdir, but that fails because foo has something else under it:

$ rmdir ~/foo
rmdir: ~/foo: Directory not empty

So you use rm -R, which succeeds and does not produce a message.

$ rm -R ~/foo/

So when you have a big directory, you don't have to go and empty every subdirectory.

But be warned that -R is a very powerful argument and you may lose data you wanted to keep!

cat & less

You don't need an editor to view the contents of a file.  What you need is just to display it.  The cat program fits the bill here:

$ cat myspeech.txt
Friends, Romans, Countrymen! Lend me your ears!

Here, cat just opens the file myspeech.txt and prints the entire file to your screen, as fast as it can.   However if the file is really long, the contents will go by very quickly, and when cat is done, all you will see are the last few lines of the file.  To just view the contents of a long file (or any text file) you can use the less program:

$ less myspeech.txt

Just as with using man, use the arrow keys to navigate, and press q to quit.

Il y a une erreur de communication avec le serveur Booktype. Nous ne savons pas actuellement où est le problème.

Vous devriez rafraîchir la page.