GNU/Linux >> Tutoriels Linux >  >> Linux

Exemples de commande "ls" de base sous Linux

Présentation

Dans cet article, nous allons apprendre à utiliser la commande Linux ls sous Linux. Fondamentalement, la commande ls est une commande de base sous Linux utilisée pour répertorier les fichiers et les répertoires. La commande ls est livrée avec de nombreux arguments et fonctionnalités, comme vous pouvez trier les fichiers et les répertoires par date, par taille, capable de vérifier les fichiers et répertoires cachés, les autorisations, les informations d'inode, etc.

Jetons donc un coup d'œil à la commande Linux ls utile et très importante avec des exemples.

Quelques arguments utiles de la commande Linux ls :

  • l – Lister les fichiers et les répertoires au format Long Listing.
  • un – Pour vérifier les fichiers et répertoires cachés.
  • je – Pour vérifier les informations d'inode.
  • n – Répertorier les numéros UID et GID du propriétaire et des groupes auxquels appartiennent les fichiers et les répertoires.
  • g – Liste des groupes de fichiers et de répertoires auxquels ils appartiennent.
  • G – Lister les utilisateurs et non les groupes de fichiers et répertoires auxquels ils appartiennent.
  • h – Répertorier la taille des fichiers et des répertoires au format lisible par l'homme.

Commande Linux ls avec exemples

[root@localhost ~]# ls       ### List Files and Directories
anaconda-ks.cfg  Documents    install.log.syslog  Public
data             Downloads    Music               Templates
Desktop          install.log  Pictures            Videos

Vous pouvez utiliser la commande ls avec l'argument -l pour obtenir plus d'informations avancées sur les fichiers et répertoires tels que les autorisations, le propriétaire, le groupe, la taille, le mois de création et de modification, la date et l'heure, le nom du fichier… etc…

[root@localhost ~]# ls -l   # Long Listing of Files and Directories
total 100
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos

Pour répertorier les détails d'un fichier particulier, reportez-vous à la commande ci-dessous.

[root@localhost ~]# ls -l file.txt   # List a Particular File
-rw-r--r--. 1 root root 0 Apr 26 10:52 file.txt

Pour répertorier le contenu d'un répertoire particulier, reportez-vous à la commande ci-dessous.

[root@localhost ~]# ls -l /mydata/   # List a Particular Directory
total 0
-rw-r--r--. 1 root root 0 Apr 26 10:53 file1.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file2.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file3.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file4.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file5.txt

Vous pouvez vérifier les fichiers et répertoires cachés en utilisant la commande Linux ls avec l'argument -a .

[root@localhost ~]# ls -a   # Checking hidden files and directories
.                .config    .gconf           .gvfs               Pictures
..               .cshrc     .gconfd          .ICEauthority       Public
anaconda-ks.cfg  data       .gnome2          install.log         .pulse
.bash_history    .dbus      .gnome2_private  install.log.syslog  .pulse-cookie
.bash_logout     Desktop    .gnote           .local              .ssh
.bash_profile    Documents  .gnupg           .mozilla            .tcshrc
.bashrc          Downloads  .gstreamer-0.10  Music               Templates
.cache           .esd_auth  .gtk-bookmarks   .nautilus           Videos

Pour vérifier le nombre d'inodes de fichiers et de répertoires, utilisez la commande ls avec l'option -i .

[root@localhost ~]# ls -i  # Checking Inode numbers of files and directories
401494 anaconda-ks.cfg  785089 Downloads           785094 Pictures
917077 data             392451 install.log         785091 Public
785088 Desktop          392452 install.log.syslog  785090 Templates
785092 Documents        785093 Music               785095 Videos

OU vous pouvez utiliser le -i argument avec -l comme indiqué sur la commande ci-dessous.

[root@localhost ~]# ls -li
total 100
401494 -rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
917077 drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
785088 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
785092 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
785089 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
392451 -rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
392452 -rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
785093 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
785094 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
785091 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
785090 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
785095 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos

Commande ls avec l'argument -o affichera le propriétaire des fichiers et des répertoires et n'affichera pas les groupes.

[root@localhost ~]# ls -o   # Listing only owner of files and directories
total 100
-rw-------. 1 root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Videos

Commande Linux ls avec l'option -m listera les fichiers et répertoires séparés par des virgules.

[root@localhost /]# ls -m   # List seperated by comma
bin, boot, dev, etc, home, lib, lib64, lost+found, media, mnt, mydata, opt,
proc, root, sbin, selinux, srv, sys, tmp, users, usr, var

Commande ls avec l'option -Q listera les fichiers et les répertoires dans lesquels les noms de fichiers et de répertoires sont entre guillemets. Reportez-vous à l'exemple de sortie ci-dessous.

[root@localhost /]# ls -lQ   # Labels of Files and directories enclosed by Quotes
total 106
dr-xr-xr-x.   2 root root  4096 Apr 16 09:59 "bin"
dr-xr-xr-x.   5 root root  1024 Apr  9 02:45 "boot"
drwxr-xr-x.  19 root root  4120 Apr 26 08:51 "dev"
drwxr-xr-x. 102 root root 12288 Apr 26 10:34 "etc"
drwxr-xr-x.   9 root root  4096 Apr 20 09:39 "home"
dr-xr-xr-x.  10 root root  4096 Apr  9 02:39 "lib"
dr-xr-xr-x.   9 root root 12288 Apr 16 09:59 "lib64"
drwx------.   2 root root 16384 Apr  9 02:35 "lost+found"
drwxr-xr-x.   2 root root  4096 Sep 23  2011 "media"
drwxr-xr-x.   3 root root  4096 Apr  9 02:46 "mnt"
drwxr-xr-x.   2 root root  4096 Apr 26 10:53 "mydata"
drwxr-xr-x.   3 root root  4096 Apr  8 21:17 "opt"
dr-xr-xr-x. 162 root root     0 Apr 26 08:49 "proc"
dr-xr-x---.  27 root root  4096 Apr 26 10:52 "root"
dr-xr-xr-x.   2 root root 12288 Apr 16 09:59 "sbin"
drwxr-xr-x.   7 root root     0 Apr 26 08:49 "selinux"
drwxr-xr-x.   2 root root  4096 Sep 23  2011 "srv"
drwxr-xr-x.  13 root root     0 Apr 26 08:49 "sys"
drwxrwxrwt.  15 root root  4096 Apr 26 10:56 "tmp"
drwxrwxrwx.   3 root root  4096 Apr 20 09:22 "users"
drwxr-xr-x.  13 root root  4096 Apr  9 02:35 "usr"
drwxr-xr-x.  22 root root  4096 Apr 16 09:24 "var"

vous pouvez répertorier les répertoires de manière récursive à l'aide de la commande Linux ls avec l'argument -R . Récursivement signifie qu'il listera tous les répertoires avec tous les sous-répertoires dans un format arborescent.

[root@localhost ~]# ls -R   # Listing Directories Recursively
.:
anaconda-ks.cfg  Documents  install.log         Pictures   Videos
data             Downloads  install.log.syslog  Public
Desktop          file.txt   Music               Templates

./data:
a.txt  b.txt  c.txt

./Desktop:
data1  data2  data3

./Desktop/data1:

./Desktop/data2:

./Desktop/data3:

./Documents:

./Downloads:

./Music:

./Pictures:

./Public:

./Templates:

./Videos:

commande ls avec ~ vous basculera vers le répertoire personnel.

[root@localhost Downloads]# pwd 
/root/Downloads
[root@localhost Downloads]# ls ~   # Switch to Home Directory
anaconda-ks.cfg  Documents  install.log         Pictures   Videos
data             Downloads  install.log.syslog  Public
Desktop          file.txt   Music               Templates

Commande Linux ls avec -t listera les fichiers et les répertoires par date de modification dans l'ordre croissant, ce qui signifie que le plus élevé viendra en premier.

[root@localhost ~]# ls -lt   # Sort by Modified by date
total 100
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates

Vous pouvez trier les fichiers et les répertoires en fonction de leur taille à l'aide de la commande ls avec l'argument -S.

[root@localhost ~]# ls -lS   # Sort by Size in ascending order
total 100
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt

La commande ls avec l'option -h listera la taille des fichiers et des répertoires au format lisible par l'homme pour l'exemple 20K (20 Ko), 15M (15 Mo).

[root@localhost ~]# ls -lh   # List the size in Human Readable format
total 100K
-rw-------. 1 root root 2.7K Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Apr 24 09:10 data
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Documents
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Downloads
-rw-r--r--. 1 root root    0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root root  41K Apr  9 02:44 install.log
-rw-r--r--. 1 root root 9.0K Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Music
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Public
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Templates
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Videos

La commande ls avec l'option -n affichera les numéros UID (ID utilisateur) et GID (ID de groupe) du fichier et du répertoire.

[root@localhost ~]# ls -ln   # List the UID and GID Numbers
total 108
-rw-------. 1 0   0  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 0   0  4096 Apr 24 09:10 data
drwxr-xr-x. 5 0   0  4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 0   0     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 0   0 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 0   0  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 0 501  4096 Apr 28 09:48 test
drwxr-xr-x. 2 0 506  4096 Apr 28 09:49 test2
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Videos

Si vous souhaitez répertorier uniquement le propriétaire des fichiers et des répertoires auxquels ils appartiennent et non des groupes, utilisez la commande Linux ls avec l'argument -G .

[root@localhost ~]# ls -lG   # Disable the Group Listing and only lists owners
total 108
-rw-------. 1 root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root  4096 Apr 24 09:10 data
drwxr-xr-x. 5 root  4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root  4096 Apr 28 09:48 test
drwxr-xr-x. 2 root  4096 Apr 28 09:49 test2
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Videos

Commande ls avec -g listera le groupe de fichiers et de répertoires auxquels ils appartiennent.

[root@localhost ~]# ls -g   # Lists groups to which they belongs
total 108
-rw-------. 1 root        2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root        4096 Apr 24 09:10 data
drwxr-xr-x. 5 root        4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root           0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root       41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root        9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 sales       4096 Apr 28 09:49 test2
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Videos

commande ls avec -F listera les répertoires avec un /à la fin. Reportez-vous à l'exemple de sortie ci-dessous.

[root@localhost ~]# ls -lF   # Add's / at the end of the directories
total 100
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads/
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos/

Pour plus de référence sur les commandes liées à Linux ls et ses arguments, vous pouvez exécuter les commandes ci-dessous dans votre système Linux.

[root@localhost ~]# man help
[root@localhost ~]# ls --help


Linux
  1. 10 exemples de base de la commande Linux Netstat

  2. 7 Exemples de commandes Linux df

  3. 8 Exemples de commandes Linux TR

  4. Exemples de commande "chmod" de base sous Linux

  5. Exemples de commande rm sous Linux

14 exemples de commandes "ls" utiles sous Linux

16 exemples de commandes Echo sous Linux

9 exemples de commandes tee sous Linux

9 exemples de commande diff sous Linux

Commande Linux ip avec exemples

Exemples de commandes id sous Linux