Linux introduction/fr: Difference between revisions
(Created page with "=== Lister le contenu d'un répertoire === Pour lister les fichiers d'un répertoire dans un terminal, on utilise la commande <tt>ls</tt> (''list'') : {{Commande|ls}} Pour inc...") |
(Created page with "Les options peuvent être combinées, par exemple : {{Commande|ls -alth}}") |
||
Line 32: | Line 32: | ||
L'option <tt>-h</tt> donne la taille des fichiers dans un format aisément lisible par les humains. | L'option <tt>-h</tt> donne la taille des fichiers dans un format aisément lisible par les humains. | ||
Les options peuvent être combinées, par exemple : | |||
{{ | {{Commande|ls -alth}} | ||
=== Navigating the file system === | === Navigating the file system === |
Revision as of 18:22, 24 May 2016
Cet article s'adresse aux utilisateurs de systèmes Windows ou Mac ayant peu ou pas d'expérience sous environnement UNIX. Il devrait vous donner les bases nécessaires pour accéder aux serveurs de calcul et être rapidement capable de les utiliser.
La connexion aux serveurs utilise le protocole SSH en mode texte. Vous ne disposez pas d'une interface graphique, mais d'une console. Notez que les exécutables Windows ne fonctionnent pas sur nos serveurs, à moins d'utiliser un émulateur.
Obtenir de l'aide
En général, les commandes sont documentées dans les manuels de référence disponibles sur les serveurs. Vous pouvez y accéder à partir du terminal :
[nom@serveur ~]$ man command
man utilise less (voir section Visualiser et éditer des fichiers); il faut appuyer sur q pour quitter le programme.
Pour trouver des pages de manuel se rapportant à un sujet ou mot-clé, vous pouvez taper :
[nom@serveur ~]$ apropos sujet
Par convention, les exécutables contiennent eux-mêmes une aide sur la façon dont ils doivent être utilisés
En général, on invoque cette aide en passant -h ou --help en argument à la commande et, dans certains cas, -help.
S'orienter sur le système
Au moment de la connexion, vous serez dirigé dans le répertoire $HOME (terme UNIX pour dossier ou répertoire) de votre compte utilisateur. Lors de la création d'un compte, le $HOME ne contient rien d'autre que des fichiers de configuration qui sont cachés (fichiers préfixés par un « . »).
Dans un système Linux, il est fortement déconseillé de créer des fichiers ou répertoires dont les noms contiennent des espaces ou des caractères spéciaux; ces caractères spéciaux incluent les accents.
Lister le contenu d'un répertoire
Pour lister les fichiers d'un répertoire dans un terminal, on utilise la commande ls (list) :
[nom@serveur ~]$ ls
Pour inclure les fichiers cachés :
[nom@serveur ~]$ ls -a
Pour trier les résultats par date (du plus récent au plus ancien) plutôt qu'en ordre alphabétique :
[nom@serveur ~]$ ls -t
Pour obtenir des informations détaillées sur les fichiers (permissions d'accès, propriétaire, groupe, taille et date de dernière modification) :
[nom@serveur ~]$ ls -l
L'option -h donne la taille des fichiers dans un format aisément lisible par les humains.
Les options peuvent être combinées, par exemple :
[nom@serveur ~]$ ls -alth
To move about in the file system, use the cd command (change directory).
So, to change to my_directory you type:
[name@server ~]$ cd my_directory
To change to the parent folder you type:
[name@server ~]$ cd ..
And, to move back to your home directory ($HOME):
[name@server ~]$ cd
Creating and removing directories
To create (make) a directory, use the mkdir command:
[name@server ~]$ mkdir my_directory
To remove a directory, use the rmdir command:
[name@server ~]$ rmdir my_directory
Deleting a directory like this only works if it is empty.
Deleting files
You can remove files using the rm command:
[name@server ~]$ rm my_file
You can also recursively remove a directory:
[name@server ~]$ rm -r my_directory
The (potentially dangerous!) -f option can be useful to bypass confirmation prompts and to continue the operation after an error.
Copying and renaming files or directories
To copy a file use the cp command:
[name@server ~]$ cp source_file destination_file
To recursively copy a directory:
[name@server ~]$ cp -R source_directory destination_directory
To rename a file or a folder (directory), use the mv command (move):
[name@server ~]$ mv source_file destination_file
This command also applies to directories. You should then replace source_file with source_directory and destination_file with destionation_directory.
File permissions
UNIX systems support 3 types of permissions : read (r), write (w) and execute (x). For files, a file should be readable to be read, writable to be modified, and executable to be run (if it's a binary executable or a script). For a directory, read permissions are necessary to list its contents, write permissions enable modification (adding or removing a file) and execute permissions enable changing to it.
Permissions apply to 3 different classes of users, the owner (u), the group (g), and all others or "the world" (o). To know which permissions are associated to files and subdirectories of the current directory, use the following command:
[name@server ~]$ ls -la
The 10 characters at the beginning of each line show the permissions. The first character indicates the file type :
- -: a normal file
- d: a directory
- l: a symbolic link
Then, from left to right, this command shows read, write and execute permissions of the owner, the group and other users. Here are some examples :
- drwxrwxrwx: a world-readable and world-writable directory
- drwxr-xr-x: a directory that can be listed by everybody, but only the owner can add or remove files
- -rwxr-xr-x: a world-readable and world-executable file that can only be changed by its owner
- -rw-r--r--: a world-readable file that can only be changed by its owner.
- -rw-rw----: a file that can be read and changed by its owner and by its group
- -rw-------: a file that can only be read and changed by its owner
- drwx--x--x: a directory that can only be listed or modified by its owner, but all others can still pass it on their way to a deeper subdirectory
- drwx-wx-wx: a directory that everybody can enter and modify but where only the owner can list its contents
Important note: to be able to read or write in a directory, you need to have execute permissions (x) set in all parent directories, all the way up to the file system's root (/). So if your home directory has drwx------ permissions and contains a subdirectory with drwxr-xr-x permissions, other users cannot read the contents of this subdirectory because they do not have access (by the executable bit) to its parent directory.
After listing the permissions, ls -la command gives a number, followed by the file owner's name, the file group's name, its size, last modification date, and name.
The chmod command allows you to change file permissions. The simple way to use it is to specify which permissions you wish to add or remove to which type of user. To do this, you specify the list of users (u for the owner, g for the group, o for other users, a for all), followed by a + to add permissions or - to remove permissions, which is then followed by a list of permissions to modify (r for read, w for write, x for execute). Non-specified permissions are not affected. Here are a few examples:
- Prevent group members and all others to read or modify the file secret.txt:
[name@server ~]$ chmod go-rwx secret.txt
- Allow everybody to read the file public.txt:
[name@server ~]$ chmod a+r public.txt
- Make the file script.sh executable:
[name@server ~]$ chmod a+x script.sh
- Allow group members to read and write in the directory shared:
[name@server ~]$ chmod g+rwx shared
- Prevent other users to read or modify your home directory:
[name@server ~]$ chmod go-rw ~
Viewing and editing files
Viewing a file
To view a file read-only, use the less command:
[name@server ~]$ less file_to_view
You can then use the arrow keys or the mouse wheel to navigate the document. You can search for something in the document by typing /what_to_search_for. You can quit less by pressing the q key.
Comparing two files
The diff command allows you to compare two files:
[name@server ~]$ diff file1 file2
The -y option shows both files side by side.
Searching within a file
The grep command allows you to look for a given expression in one
[name@server ~]$ grep 'tata' file1
or multiple files.
[name@server ~]$ grep 'tata' fil*
.
Note that, in Linux, the "*" wildcard matches zero or more characters. The "?" wilcard matches exactly one character.
The text to be searched for can also be variable. For example, to look for the text "number 10 » or "number 11", etc. with any number between 10 and 29, the following command can be used:
[name@server ~]$ grep 'number [1-2][0-9]' file
A regular expression must be used for the search text. To learn more about regular expressions,
look at the following sites:
A very complete list of regular expressions