GNU/Linux >> Belajar Linux >  >> Linux

Contoh Perintah "ls" dasar di Linux

Pengantar

Pada artikel ini, kita akan belajar bagaimana menggunakan perintah ls Linux di Linux. Pada dasarnya, perintah ls adalah perintah dasar di Linux yang digunakan untuk Daftar file dan direktori. Perintah ls hadir dengan begitu banyak argumen dan fitur seperti Anda dapat mengurutkan file dan direktori berdasarkan Tanggal, berdasarkan Ukuran, dapat memeriksa file dan direktori tersembunyi, izin, informasi inode, dan sebagainya.

Jadi mari kita lihat perintah ls Linux yang berguna dan sangat penting dengan contoh.

Beberapa argumen perintah ls Linux yang berguna :

  • l – Daftar file dan direktori dalam Format Daftar Panjang.
  • a – Untuk memeriksa file dan direktori tersembunyi.
  • saya – Untuk memeriksa Informasi Inode.
  • n – Cantumkan nomor UID dan GID Pemilik dan Grup tempat file dan direktori tersebut berada.
  • g – Daftar grup file dan direktori tempat mereka berada.
  • G – Mencantumkan pengguna dan bukan grup file dan direktori tempat mereka berada.
  • h – Cantumkan ukuran file dan direktori dalam format Human Readable.

Perintah Linux ls dengan Contoh

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

Anda dapat menggunakan perintah ls dengan argumen -l untuk mendapatkan informasi lebih lanjut terkait file dan direktori seperti Izin, Pemilik, Grup, Ukuran, Bulan yang Dibuat dan Diubah, tanggal dan waktu, nama file…dst…

[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

Untuk membuat daftar detail file tertentu, lihat perintah di bawah ini.

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

Untuk membuat daftar isi direktori tertentu, lihat perintah di bawah ini.

[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

Anda dapat memeriksa file dan direktori tersembunyi dengan menggunakan perintah Linux ls dengan argumen -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

Untuk memeriksa nomor inode file dan direktori gunakan perintah ls dengan opsi -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

ATAU Anda dapat menggunakan -i argumen dengan -l seperti yang ditunjukkan pada perintah di bawah ini.

[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

ls perintah dengan argumen -o akan menunjukkan pemilik file dan direktori dan tidak akan menampilkan grup.

[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

Perintah Linux ls dengan opsi -m akan mencantumkan file dan direktori yang dipisahkan dengan koma.

[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

ls perintah dengan opsi -Q akan mencantumkan file dan direktori di mana nama file dan direktori diapit tanda kutip. Lihat contoh keluaran di bawah ini.

[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"

anda dapat membuat daftar direktori secara rekursif menggunakan perintah ls Linux dengan argumen -R . Secara rekursif berarti akan mencantumkan semua direktori dengan semua subdirektori dalam format pohon.

[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:

ls perintah dengan ~ akan mengalihkan Anda ke direktori home.

[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

Perintah Linux ls dengan -t akan mencantumkan file dan direktori berdasarkan tanggal modifikasinya dalam urutan menaik, artinya yang lebih tinggi akan didahulukan.

[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

Anda dapat mengurutkan file dan direktori berdasarkan ukurannya menggunakan perintah ls dengan argumen -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

Perintah ls dengan opsi -h akan mencantumkan ukuran file dan direktori dalam format Human Readable untuk Contoh 20K (20 KB), 15M (15 MB).

[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

Perintah ls dengan opsi -n akan menampilkan nomor UID (User ID) dan GID (Group ID) dari file dan direktori.

[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

Jika Anda hanya ingin membuat daftar Pemilik file dan direktori tempat mereka berada dan bukan grup, gunakan perintah Linux ls dengan argumen -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

ls perintah dengan -g akan mencantumkan grup file dan direktori tempat mereka berada.

[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

ls perintah dengan -F akan mencantumkan direktori dengan / di akhir. Lihat contoh keluaran di bawah ini.

[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/

Untuk referensi lebih lanjut tentang perintah terkait Linux ls dan argumennya, Anda dapat menjalankan perintah di bawah ini di sistem linux Anda.

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


Linux
  1. sa Contoh Perintah di Linux

  2. w Contoh Perintah di Linux

  3. Contoh Perintah ac di Linux

  1. 10 contoh dasar perintah Linux Netstat

  2. Contoh Perintah df di Linux

  3. du Contoh Perintah di Linux

  1. 7 Contoh Perintah Linux df

  2. 8 Contoh Perintah Linux TR

  3. Contoh Perintah Dasar "chmod" di Linux