The ls command is one of the very basic and most frequently used commands in Linux.
I can say in other words, it’s heart for Linux user.
I believe that everybody first use ls command when their logged into Linux system.
The ls command comes with many options and each options will give you a different kind of output that will be used for many purpose.
What’s ls Command?
ls stands for List, the ls command is used to display the directory contents.
It lists bunch of information about files and directories such as file permissions, number of links, owner name, owner group, file size, time of last modification, and file/directory name.
The ls command output comes with seven fields.
+------------------------------------------------------------------------------+ |-rw-r--r-- | 1 | daygeek | daygeek | 14M | Jan 3 2019 | 2daygeek_access.log| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | +------------------------------------------------------------------------------+
Field-1 (-rw-r--r--) : File Permissions Field-2 (1) : The number of links or directories inside this directory. Field-3 (daygeek) : File owner name Field-4 (daygeek) : File group name Field-5 (14M) : File size Field-6 (Jan 3 2019) : Last modification date and time of the file. Field-7 (2daygeek_access.log) : Name of the file.
First field comes with 10 character, which indicates Linux file Permissions. The first character indicates the type of the file. The next 9 characters in the first field represent the permissions. Each 3 characters refers the read (r), write (w), execute (x) permissions on owner, group and others.
Field-1 (-|rw-|r--|r--) : File Permissions The first character indicates the type of the file. - : It indicates for normal file d : It indicates for directory l : It indicates for link file s : It indicates for socket file 2-4 characters indicates permissions on owner. 5-7 characters indicates permissions on group. 8-10 characters indicates permissions on others.
1) List Directory Contents Using ls Command Without Any Option
When you use ls command without any option, it will show you only files and directories name with horizontal format.
It may help you to easily check list of files and directories which is there in the directory when you are not able to fit a directory contents in single page.
$ ls 2g.out cpu-alert.sh guna_server.sh* mysql_backup_2.sh* pass-multi-user.sh* port_scan.sh* service1.sh service3.sh user-add.sh* bulk-package-install.sh cpu-mem-check.sh* jwhois-check.sh mysql_backup.sh* pass-up.sh* process.sh service2a.sh servicem.sh whois-check.sh* cpu-alert1.sh disk-space-alert.sh memory-monitor.sh ovh.sh* passwd-up1.sh* server-list.txt service2.sh service.sh xsos* cpu-alert2.sh file-copy.sh mysql_backup_1.sh* pass-gen.sh* passwd-up.sh* service1a.sh service3a.sh test.txt
2) How to List Directory Contents With Detailed Format?
It can be achieved by adding -l
option with ls command. l stand for long listing. Use the following ls command to view detailed information in the given directory contents.
It shows you file permission, username, group name, file size, file created date and file created time.
$ ls -l total 276 -rw-r--r-- 1 daygeek daygeek 4780 Apr 11 14:57 2g.out -rw-r--r-- 1 daygeek daygeek 78 Apr 26 11:39 bulk-package-install.sh -rw-r--r-- 1 daygeek daygeek 764 May 26 15:33 cpu-alert1.sh -rw-r--r-- 1 daygeek daygeek 616 Jun 2 13:25 cpu-alert2.sh -rw-r--r-- 1 daygeek daygeek 538 May 25 19:25 cpu-alert.sh -rwxr-xr-x 1 daygeek daygeek 1361 Feb 9 2019 cpu-mem-check.sh* -rw-r--r-- 1 daygeek daygeek 410 Apr 27 23:58 disk-space-alert.sh -rw-r--r-- 1 daygeek daygeek 99 Jan 19 2019 file-copy.sh -rwxr-xr-x 1 daygeek daygeek 65 Apr 16 23:23 guna_server.sh* -rw-r--r-- 1 daygeek daygeek 208 May 24 12:04 jwhois-check.sh -rw-r--r-- 1 daygeek daygeek 776 Jun 14 12:00 memory-monitor.sh -rwxr-xr-x 1 daygeek daygeek 237 Apr 6 10:40 mysql_backup_1.sh* -rwxr-xr-x 1 daygeek daygeek 241 Apr 6 10:49 mysql_backup_2.sh* -rwxr-xr-x 1 daygeek daygeek 761 Apr 6 21:36 mysql_backup.sh*
3) How to List Directory Contents In Human Readable Format?
It’s same as above but additionally shows you the files and directories size in KB, MB & GB.
$ ls -lh total 276K -rw-r--r-- 1 daygeek daygeek 4.7K Apr 11 14:57 2g.out -rw-r--r-- 1 daygeek daygeek 78 Apr 26 11:39 bulk-package-install.sh -rw-r--r-- 1 daygeek daygeek 764 May 26 15:33 cpu-alert1.sh -rw-r--r-- 1 daygeek daygeek 616 Jun 2 13:25 cpu-alert2.sh -rw-r--r-- 1 daygeek daygeek 538 May 25 19:25 cpu-alert.sh -rwxr-xr-x 1 daygeek daygeek 1.4K Feb 9 2019 cpu-mem-check.sh* -rw-r--r-- 1 daygeek daygeek 410 Apr 27 23:58 disk-space-alert.sh -rw-r--r-- 1 daygeek daygeek 99 Jan 19 2019 file-copy.sh -rwxr-xr-x 1 daygeek daygeek 65 Apr 16 23:23 guna_server.sh* -rw-r--r-- 1 daygeek daygeek 208 May 24 12:04 jwhois-check.sh -rw-r--r-- 1 daygeek daygeek 776 Jun 14 12:00 memory-monitor.sh -rwxr-xr-x 1 daygeek daygeek 237 Apr 6 10:40 mysql_backup_1.sh* -rwxr-xr-x 1 daygeek daygeek 241 Apr 6 10:49 mysql_backup_2.sh* -rwxr-xr-x 1 daygeek daygeek 761 Apr 6 21:36 mysql_backup.sh*
4) How to List Hidden Files And Directories From the Given Directory In Linux?
By default ls command output doesn’t show you the hidden files and directories. In Linux, hidden files and directories comes with . (dot)
prefix.
Use the following ls command to list all together, it can be achieved by adding -a
option with ls command.
ls -lah /home/daygeek/ total 2.1G drwx------ 47 daygeek daygeek 4.0K Aug 9 22:44 ./ drwxr-xr-x 3 root root 4.0K Nov 24 2018 ../ -rw-r--r-- 1 daygeek daygeek 100M Jul 29 11:17 100MB.bin -rw-r--r-- 1 daygeek daygeek 14M Jan 3 2019 2daygeek_access.log -rw-r--r-- 1 daygeek daygeek 14M Jan 3 2019 2daygeek.com -rw-r--r-- 1 daygeek daygeek 785K Jan 3 2019 2daygeek.html drwxr-xr-x 2 daygeek daygeek 4.0K Jan 20 2019 2g/ lrwxrwxrwx 1 daygeek daygeek 47 Jan 6 2019 2g-files -> /home/daygeek/Desktop/Daily_Activity/2g-images// -rw-r--r-- 1 daygeek daygeek 4.4K Dec 24 2018 2g-test -rw-r--r-- 1 daygeek daygeek 229K Jan 5 2019 2g-test-05-01-2019.tar.gz -rw-r--r-- 1 daygeek daygeek 2.0K May 23 13:25 angergos-ends.txt -rw-r--r-- 1 daygeek daygeek 8.7K Jun 23 02:08 apt-clone.txt -rw------- 1 daygeek daygeek 14K Aug 5 02:45 .bash_history -rw-r--r-- 1 daygeek daygeek 21 Jun 14 2018 .bash_logout -rw-r--r-- 1 daygeek daygeek 95 Dec 13 2018 .bash_profile -rw-r--r-- 1 daygeek daygeek 4.2K Mar 15 12:05 .bashrc drwxr-xr-x 6 daygeek daygeek 4.0K Feb 20 17:46 betty/ -rw-r--r-- 1 daygeek daygeek 1.2K Feb 22 09:52 .betty_history drwxr-xr-x 3 daygeek daygeek 4.0K Jan 6 2019 .bundle/ drwx------ 41 daygeek daygeek 4.0K Aug 14 12:54 .cache/ -rw------- 1 daygeek daygeek 35 Mar 13 12:09 .calc_history drwxr-xr-x 4 daygeek daygeek 4.0K Dec 13 2018 .cargo/
5) How to Sort Files and Directories Based on Last Modified Time In Linux?
Some times you may need to sort files and directories based on last modified time. If so, use the following ls command, it can be achieved by adding -t
option with ls command.
$ ls -lth total 276K -rw-r--r-- 1 daygeek daygeek 296 Aug 4 01:27 servicem.sh -rw-r--r-- 1 daygeek daygeek 361 Jul 29 19:45 service3a.sh -rw-r--r-- 1 daygeek daygeek 312 Jul 29 19:38 service2.sh -rw-r--r-- 1 daygeek daygeek 324 Jul 29 19:37 service2a.sh -rw-r--r-- 1 daygeek daygeek 177 Jul 29 19:34 service1a.sh -rw-r--r-- 1 daygeek daygeek 341 Jul 29 19:26 service3.sh -rw-r--r-- 1 daygeek daygeek 288 Jul 29 19:10 service.sh -rw-r--r-- 1 daygeek daygeek 157 Jul 29 18:29 service1.sh -rw-r--r-- 1 daygeek daygeek 776 Jun 14 12:00 memory-monitor.sh -rw-r--r-- 1 daygeek daygeek 830 Jun 14 11:54 process.sh -rw-r--r-- 1 daygeek daygeek 616 Jun 2 13:25 cpu-alert2.sh -rw-r--r-- 1 daygeek daygeek 764 May 26 15:33 cpu-alert1.sh -rw-r--r-- 1 daygeek daygeek 538 May 25 19:25 cpu-alert.sh -rwxr-xr-x 1 daygeek daygeek 204 May 24 12:07 whois-check.sh*
6) How to Sort Files and Directories Based on Last Modified Time in Reverse Order on Linux?
It’s same as above but it’s sort the output in reverse order.
It may useful, when you have large number of contents in the given directory since its showing the required files in the bottom of the page. It can be achieved by adding -r
option with ls command.
$ ls -ltrh -rw-r--r-- 1 daygeek daygeek 764 May 26 15:33 cpu-alert1.sh -rw-r--r-- 1 daygeek daygeek 616 Jun 2 13:25 cpu-alert2.sh -rw-r--r-- 1 daygeek daygeek 830 Jun 14 11:54 process.sh -rw-r--r-- 1 daygeek daygeek 776 Jun 14 12:00 memory-monitor.sh -rw-r--r-- 1 daygeek daygeek 157 Jul 29 18:29 service1.sh -rw-r--r-- 1 daygeek daygeek 288 Jul 29 19:10 service.sh -rw-r--r-- 1 daygeek daygeek 341 Jul 29 19:26 service3.sh -rw-r--r-- 1 daygeek daygeek 177 Jul 29 19:34 service1a.sh -rw-r--r-- 1 daygeek daygeek 324 Jul 29 19:37 service2a.sh -rw-r--r-- 1 daygeek daygeek 312 Jul 29 19:38 service2.sh -rw-r--r-- 1 daygeek daygeek 361 Jul 29 19:45 service3a.sh -rw-r--r-- 1 daygeek daygeek 296 Aug 4 01:27 servicem.sh
7) How to List Only Directory Contents With Vertical Format When You Use ls Command Without Any Option?
It’s same as first example but it’s printing the output in vertical format instead of horizontal.
If so, use the following ls command, it can be achieved by adding -1 (Numeric One)
option with ls command.
$ ls -1 2g.out bulk-package-install.sh cpu-alert1.sh cpu-alert2.sh cpu-alert.sh cpu-mem-check.sh* disk-space-alert.sh file-copy.sh guna_server.sh* jwhois-check.sh memory-monitor.sh mysql_backup_1.sh* mysql_backup_2.sh* mysql_backup.sh* ovh.sh* pass-gen.sh*
8) How to List Directory Contents In Reverse Order On Linux?
By default ls command prints the output in an ascending order like 0-9 and a-z.
If you would like to print the output in reverse order, use the following ls command, it can be achieved by adding -r
option with ls command.
$ ls -lhr total 276K -rwxr-xr-x 1 daygeek daygeek 137K May 12 20:26 xsos* -rwxr-xr-x 1 daygeek daygeek 204 May 24 12:07 whois-check.sh* -rwxr-xr-x 1 daygeek daygeek 214 Mar 10 22:15 user-add.sh* -rw-r--r-- 1 daygeek daygeek 0 Apr 29 10:25 test.txt -rw-r--r-- 1 daygeek daygeek 288 Jul 29 19:10 service.sh -rw-r--r-- 1 daygeek daygeek 296 Aug 4 01:27 servicem.sh -rw-r--r-- 1 daygeek daygeek 341 Jul 29 19:26 service3.sh -rw-r--r-- 1 daygeek daygeek 361 Jul 29 19:45 service3a.sh -rw-r--r-- 1 daygeek daygeek 312 Jul 29 19:38 service2.sh -rw-r--r-- 1 daygeek daygeek 324 Jul 29 19:37 service2a.sh -rw-r--r-- 1 daygeek daygeek 157 Jul 29 18:29 service1.sh -rw-r--r-- 1 daygeek daygeek 177 Jul 29 19:34 service1a.sh -rw-r--r-- 1 daygeek daygeek 227 Mar 17 12:22 server-list.txt
9) How to List Directory Contents Recursively In Linux Using ls Command?
By default ls command prints the given directory contents and not it’s sub-directories contents.
To do so, use the following ls command, it can be achieved by adding -R
option with ls command.
$ ls -lhR .: total 280K -rw-r--r-- 1 daygeek daygeek 4.7K Apr 11 14:57 2g.out drwxr-xr-x 2 daygeek daygeek 4.0K Aug 16 16:59 backup/ -rw-r--r-- 1 daygeek daygeek 78 Apr 26 11:39 bulk-package-install.sh -rw-r--r-- 1 daygeek daygeek 764 May 26 15:33 cpu-alert1.sh -rw-r--r-- 1 daygeek daygeek 616 Jun 2 13:25 cpu-alert2.sh -rw-r--r-- 1 daygeek daygeek 538 May 25 19:25 cpu-alert.sh -rwxr-xr-x 1 daygeek daygeek 1.4K Feb 9 2019 cpu-mem-check.sh* -rw-r--r-- 1 daygeek daygeek 410 Apr 27 23:58 disk-space-alert.sh -rw-r--r-- 1 daygeek daygeek 99 Jan 19 2019 file-copy.sh -rwxr-xr-x 1 daygeek daygeek 65 Apr 16 23:23 guna_server.sh* -rw-r--r-- 1 daygeek daygeek 208 May 24 12:04 jwhois-check.sh -rw-r--r-- 1 daygeek daygeek 776 Jun 14 12:00 memory-monitor.sh ./backup: total 32K -rw-r--r-- 1 daygeek daygeek 177 Aug 16 16:59 service1a.sh -rw-r--r-- 1 daygeek daygeek 157 Aug 16 16:59 service1.sh -rw-r--r-- 1 daygeek daygeek 324 Aug 16 16:59 service2a.sh -rw-r--r-- 1 daygeek daygeek 312 Aug 16 16:58 service2.sh -rw-r--r-- 1 daygeek daygeek 361 Aug 16 16:58 service3a.sh -rw-r--r-- 1 daygeek daygeek 341 Aug 16 16:58 service3.sh -rw-r--r-- 1 daygeek daygeek 296 Aug 16 16:58 servicem.sh -rw-r--r-- 1 daygeek daygeek 288 Aug 16 16:57 service.sh
10) How to Sort Directory Contents Based on File Size In Linux Using ls Command?
Some times you may want to clean up big size files in your system to keep disk usage an under control.
To do so, first you need to identify big size files in the given directory so, use the following ls command, it can be achieved by adding -S
option with ls command.
$ ls -lhS total 2.1G -rwxrwxrwx 1 daygeek daygeek 1.9G Oct 30 2018 manjaro-xfce-18.0-stable-x86_64.iso* -rw-r--r-- 1 daygeek daygeek 100M Jul 29 11:17 100MB.bin -rwxr-xr-x 1 daygeek daygeek 59M Apr 12 2018 odrive-0.1.3-x86_64.AppImage* -rw-r--r-- 1 daygeek daygeek 14M Jan 3 2019 2daygeek_access.log -rw-r--r-- 1 daygeek daygeek 14M Jan 3 2019 2daygeek.com -rw-r--r-- 1 daygeek daygeek 785K Jan 3 2019 2daygeek.html -rw-r--r-- 1 daygeek daygeek 229K Jan 5 2019 2g-test-05-01-2019.tar.gz -rw-r--r-- 1 daygeek daygeek 137K Apr 18 11:11 fdisk - Easy Way To Manage Disk Partitions In Linux.txt -rw-r--r-- 1 daygeek daygeek 115K Aug 9 22:44 mpv-shot0001.jpg -rwxr-xr-x 1 daygeek daygeek 88K Nov 27 2018 check-security-updates*
11) How to List Directory Contents With INODE Number In Linux Using ls Command?
use the following ls command if you would like to add inode number in the ls command output. It can be achieved by adding -i
option with ls command.
$ ls -lhi total 280K 2490942 -rw-r--r-- 1 daygeek daygeek 4.7K Apr 11 14:57 2g.out 2498578 drwxr-xr-x 2 daygeek daygeek 4.0K Aug 16 16:59 backup/ 2491025 -rw-r--r-- 1 daygeek daygeek 78 Apr 26 11:39 bulk-package-install.sh 2491272 -rw-r--r-- 1 daygeek daygeek 764 May 26 15:33 cpu-alert1.sh 2491037 -rw-r--r-- 1 daygeek daygeek 616 Jun 2 13:25 cpu-alert2.sh 2491162 -rw-r--r-- 1 daygeek daygeek 538 May 25 19:25 cpu-alert.sh 1744131 -rwxr-xr-x 1 daygeek daygeek 1.4K Feb 9 2019 cpu-mem-check.sh* 2491042 -rw-r--r-- 1 daygeek daygeek 410 Apr 27 23:58 disk-space-alert.sh 1704244 -rw-r--r-- 1 daygeek daygeek 99 Jan 19 2019 file-copy.sh 1710121 -rwxr-xr-x 1 daygeek daygeek 65 Apr 16 23:23 guna_server.sh* 2490519 -rw-r--r-- 1 daygeek daygeek 208 May 24 12:04 jwhois-check.sh 2491694 -rw-r--r-- 1 daygeek daygeek 776 Jun 14 12:00 memory-monitor.sh 2490973 -rwxr-xr-x 1 daygeek daygeek 237 Apr 6 10:40 mysql_backup_1.sh* 2490982 -rwxr-xr-x 1 daygeek daygeek 241 Apr 6 10:49 mysql_backup_2.sh* 2490955 -rwxr-xr-x 1 daygeek daygeek 761 Apr 6 21:36 mysql_backup.sh*
12) How to List Directory Contents With UID and GID In Linux Using ls Command?
use the following ls command if you would like print UID and GID instead of actual User Name and Group Name in the ls command output. It can be achieved by adding -n
option with ls command.
$ ls -lhn total 280K -rw-r--r-- 1 1000 1000 4.7K Apr 11 14:57 2g.out drwxr-xr-x 2 1000 1000 4.0K Aug 16 16:59 backup/ -rw-r--r-- 1 1000 1000 78 Apr 26 11:39 bulk-package-install.sh -rw-r--r-- 1 1000 1000 764 May 26 15:33 cpu-alert1.sh -rw-r--r-- 1 1000 1000 616 Jun 2 13:25 cpu-alert2.sh -rw-r--r-- 1 1000 1000 538 May 25 19:25 cpu-alert.sh -rwxr-xr-x 1 1000 1000 1.4K Feb 9 2019 cpu-mem-check.sh* -rw-r--r-- 1 1000 1000 410 Apr 27 23:58 disk-space-alert.sh -rw-r--r-- 1 1000 1000 99 Jan 19 2019 file-copy.sh -rwxr-xr-x 1 1000 1000 65 Apr 16 23:23 guna_server.sh* -rw-r--r-- 1 1000 1000 208 May 24 12:04 jwhois-check.sh -rw-r--r-- 1 1000 1000 776 Jun 14 12:00 memory-monitor.sh -rwxr-xr-x 1 1000 1000 237 Apr 6 10:40 mysql_backup_1.sh*
13) How to Check Directory Permission In Linux Using ls Command?
use the following ls command if you would like to check the given directory permission instead of printing their contents. It can be achieved by adding -d
option with ls command.
$ ls -ld backup/ drwxr-xr-x 2 daygeek daygeek 4096 Aug 16 16:59 backup/
14) How to List Only Hidden Files And Directories From the Given Directory In Linux?
Use the following ls command if you would like to print only hidden files and directories from the given directory.
$ ls -ld .* or $ ls -ld .?* drwxr-xr-x 3 root root 4096 Nov 24 2018 .. -rw------- 1 daygeek daygeek 13673 Aug 5 02:45 .bash_history -rw-r--r-- 1 daygeek daygeek 21 Jun 14 2018 .bash_logout -rw-r--r-- 1 daygeek daygeek 95 Dec 13 2018 .bash_profile -rw-r--r-- 1 daygeek daygeek 4257 Mar 15 12:05 .bashrc -rw-r--r-- 1 daygeek daygeek 1198 Feb 22 09:52 .betty_history drwxr-xr-x 3 daygeek daygeek 4096 Jan 6 2019 .bundle drwx------ 41 daygeek daygeek 4096 Aug 14 12:54 .cache -rw------- 1 daygeek daygeek 35 Mar 13 12:09 .calc_history drwxr-xr-x 4 daygeek daygeek 4096 Dec 13 2018 .cargo -rw------- 1 daygeek daygeek 73 Mar 4 21:13 .clush_history drwxr-xr-x 49 daygeek daygeek 4096 Aug 14 20:55 .config drwxr-xr-x 2 daygeek daygeek 4096 Jan 6 2019 .credentials -rw-r--r-- 1 daygeek daygeek 4855 Oct 30 2017 .dir_colors drwxr-xr-x 2 daygeek daygeek 4096 Jan 6 2019 .drivesync
15) Some of the Useful Advanced ls Command Examples
These commands are not used frequently, which are considered as advanced ls command.
If you would like to only display the sub-directories excluding all other files, use the following ls command.
$ ls -ld */ drwxr-xr-x 2 daygeek daygeek 4096 Jan 20 2019 2g/ drwxrwxrwx 4 daygeek daygeek 4096 Aug 4 01:31 2g-files/ drwxr-xr-x 4 daygeek daygeek 4096 Apr 19 13:25 backup/ drwxr-xr-x 4 daygeek daygeek 4096 Feb 20 17:36 bash-insulter/ drwxr-xr-x 4 daygeek daygeek 4096 Jan 15 2019 batstat/ drwxr-xr-x 6 daygeek daygeek 4096 Feb 20 17:46 betty/ drwxr-xr-x 3 daygeek daygeek 4096 Mar 12 10:34 Desktop/ drwxr-xr-x 6 daygeek daygeek 4096 Aug 15 22:25 Documents/ drwxr-xr-x 12 daygeek daygeek 49152 Aug 16 14:38 Downloads/ drwxr-xr-x 4 daygeek daygeek 4096 Jan 7 2019 drivesync/ drwxr-xr-x 2 daygeek daygeek 4096 Apr 16 11:39 icons/ drwxr-xr-x 3 daygeek daygeek 4096 Apr 20 19:33 install-gnome-themes/ drwxr-xr-x 4 daygeek daygeek 4096 Jun 11 16:46 Music/ drwxr-xr-x 4 daygeek daygeek 20480 Aug 11 01:39 Pictures/ drwxr-xr-x 3 daygeek daygeek 4096 Dec 14 2018 projects/ drwxr-xr-x 2 daygeek daygeek 4096 Nov 24 2018 Public/ drwxr-xr-x 3 daygeek daygeek 4096 Aug 16 16:57 shell-script/ drwxr-xr-x 3 daygeek daygeek 4096 Jan 23 2019 snap/ drwxr-xr-x 4 daygeek daygeek 4096 Jan 6 2019 ssh-audit/ drwxr-xr-x 2 daygeek daygeek 4096 Nov 24 2018 Templates/ drwxr-xr-x 3 daygeek daygeek 4096 Jul 23 11:07 test/ drwxr-xr-x 5 daygeek daygeek 4096 May 30 10:30 Videos/ drwxr-xr-x 12 daygeek daygeek 4096 Jun 9 01:03 'VirtualBox VMs/' drwxr-xr-x 5 daygeek daygeek 4096 Nov 24 2018 yay/
If you would like to list files and directories with full path, use the following command.
$ ls -ld $PWD/* -rw-r--r-- 1 daygeek daygeek 4780 Apr 11 14:57 /home/daygeek/shell-script/2g.out drwxr-xr-x 2 daygeek daygeek 4096 Aug 16 16:59 /home/daygeek/shell-script/backup -rw-r--r-- 1 daygeek daygeek 78 Apr 26 11:39 /home/daygeek/shell-script/bulk-package-install.sh -rw-r--r-- 1 daygeek daygeek 764 May 26 15:33 /home/daygeek/shell-script/cpu-alert1.sh -rw-r--r-- 1 daygeek daygeek 616 Jun 2 13:25 /home/daygeek/shell-script/cpu-alert2.sh -rw-r--r-- 1 daygeek daygeek 538 May 25 19:25 /home/daygeek/shell-script/cpu-alert.sh -rwxr-xr-x 1 daygeek daygeek 1361 Feb 9 2019 /home/daygeek/shell-script/cpu-mem-check.sh -rw-r--r-- 1 daygeek daygeek 410 Apr 27 23:58 /home/daygeek/shell-script/disk-space-alert.sh -rw-r--r-- 1 daygeek daygeek 99 Jan 19 2019 /home/daygeek/shell-script/file-copy.sh -rwxr-xr-x 1 daygeek daygeek 65 Apr 16 23:23 /home/daygeek/shell-script/guna_server.sh -rw-r--r-- 1 daygeek daygeek 208 May 24 12:04 /home/daygeek/shell-script/jwhois-check.sh -rw-r--r-- 1 daygeek daygeek 776 Jun 14 12:00 /home/daygeek/shell-script/memory-monitor.sh -rwxr-xr-x 1 daygeek daygeek 237 Apr 6 10:40 /home/daygeek/shell-script/mysql_backup_1.sh
Use the following ls command to count current directory files & folders.
$ ls -1 | wc -l 36
If you only want to display matching patten files excluding all other files, use the following ls command.
$ ls -lh *.pdf -rw-r--r-- 1 daygeek daygeek 27K Dec 31 2018 1546239866635OjQiL7eDrYlpXATz.pdf -rw-r--r-- 1 daygeek daygeek 25K Dec 31 2018 1546239936751llSFFHTtLjKI8T6L.pdf -rw-r--r-- 1 daygeek daygeek 24K Dec 31 2018 1546239980174twt5oFfv7IJBfBgy.pdf -rw-r--r-- 1 daygeek daygeek 25K Dec 31 2018 1546240021050mOb79g2Gx0rhxR2z.pdf -rw-r--r-- 1 daygeek daygeek 25K Dec 31 2018 1546240042428lwBc0Y9MGMtnvxlM.pdf -rw-r--r-- 1 daygeek daygeek 26K Dec 31 2018 1546240058907esahROEworBjMZje.pdf
If you would like to count matching patten files, use the following ls command.
$ ls *.sh | wc -l 31
How to append file type indicator in ls Command Output.
$ ls -F 2daygeek_access.log 2g/ 2g-files@ pipe-test| cpu-mem-check.sh*
File type indicators
- Nothing is shown for regular files
- / indicates a directory
- @ indicates a symbolic link
- | indicates a FIFO
- * indicates a executable file
- = indicates a socket
- > indicates a door
16) How to List All Options for ls Command?
I had added most useful ls command options in the above examples. If you are looking for other options, i would suggest you to check the man page of ls command.
Also, i would request you to update your comment in our comment section, if so, we can try to help you on this.
$ ls --help or $ man ls