How To Count The Files By Extension In Linux?
Did you ever think, how to count the files based on the extension or pattern or group in the current directory?
Did you get a such a requirements?
If so, don’t worry, we are here to help you out on this.
ls command is heart for Linux user. ls command is one of the very basic and most frequently used command in Linux.
I know that we use ls command whenever we logged into the system. We can’t do anything in the Linux system without using ls command.
There are many options are available in ls command which will help to sort the directory contents in the variety of the way.
You may have interested in the following articles which is related to file manipulation.
- Four Easy Ways to Search Or Find Files And Folders in Linux
- How To Count The Number Of Files And Folders/Directories In Linux
- File Manipulation Commands
What Is ls Command?
ls command is used to list information about directory contents (the current directory by default), which included files and folders. There are many file types and few folder types are available in Linux.
If you want to know these details, navigate to the following url to understand and identify file types in Linux.
To test this, i have placed variety of file formats in the
/home/daygeek/Downloads folder so, we will show you how to do.
Let’s list out the directory contents. I have listed only part of the files because the directory has lots of the files.
# ls -lh | head -15 total 27G -rw-r--r-- 1 daygeek daygeek 12K Dec 9 10:16 007df89cda82659ee37fd7ce94052546-usb.png -rw-r--r-- 1 daygeek daygeek 29K Dec 16 03:39 116959_key_512x512.png -rw-r--r-- 1 daygeek daygeek 41K Mar 29 11:25 1200px-MySQL.svg.png -rw-r--r-- 1 daygeek daygeek 63K Mar 29 11:21 1280px-Apache_HTTP_server_logo_(2016).svg.png -rw-r--r-- 1 daygeek daygeek 16K Jan 14 20:20 1307316.png -rw-r--r-- 1 daygeek daygeek 27K Dec 31 12:33 1546239866635OjQiL7eDrYlpXATz.pdf -rw-r--r-- 1 daygeek daygeek 25K Dec 31 12:35 1546239936751llSFFHTtLjKI8T6L.pdf -rw-r--r-- 1 daygeek daygeek 24K Dec 31 12:35 1546239980174twt5oFfv7IJBfBgy.pdf -rw-r--r-- 1 daygeek daygeek 25K Dec 31 12:36 1546240021050mOb79g2Gx0rhxR2z.pdf -rw-r--r-- 1 daygeek daygeek 25K Dec 31 12:36 1546240042428lwBc0Y9MGMtnvxlM.pdf -rw-r--r-- 1 daygeek daygeek 26K Dec 31 12:37 1546240058907esahROEworBjMZje.pdf -rw-r--r-- 1 daygeek daygeek 169K Jan 9 19:52 165-1652009_temperature-temperature-sensor-icon.png.jpg -rw-r--r-- 1 daygeek daygeek 43K Mar 29 03:46 220px-Flatpak_logo.png -rw-r--r-- 1 daygeek daygeek 14K Dec 17 00:52 240px-SVG_Logo.svg.png
If you want to know how many files and folders are there in the current directory, use the following tree command. It’s showing the results recursively.
# tree -a /home/daygeek/Downloads | tail -1 3 directories, 182 files
If you would like to check the list of files in the current directory, use the following command.
# ls -l . | egrep -c '^-' 161
If you would like to check the list of files recursively, use one of the following command.
# ls -lR . | egrep -c '^-' 182 or # find . -type f | wc -l 182
# find . -type d | wc -l 4
1) How To Count The Files By Specific Extension In Linux Using ls Command?
The below command is counting only specific extension files within a directory and not recursively, like if i mention
.png its count only .png file on current directory. You need to mention your file extension which you want to count. Here i have checked two type of extension and pasted the output.
# ls *.png | wc -l 57 # ls *.pdf | wc -l 30
2) How To Count The Files Recursively By Specific Extension In Linux Using ls Command?
In this example, we are going to count the files recursively using ls command.
# ls -lR /home/daygeek/Downloads/ | grep ".pdf$" | wc -l or # ls -lR | grep --count \.pdf$ 30
3) How To Count The Files Recursively By Specific Extension In Linux Using find Command?
In this example, we are going to count the files recursively using find commmand.
# find . -name "*.png" -type f | wc -l or # find . -name "*.png" | wc -l 71
4) How To Count All The Files Extension Recursively In Linux?
The below command is counting all file extensions separately and recursively. All the files are different extension but we can able to see all together in the single output. See the below output
# find . -type f | sed -n 's/..*\.//p' | sort | uniq -c 1 avi 1 docx 1 iso 10 jpg 17 mkv 2 mp4 30 pdf 71 png 1 sh 37 svg 5 torrent 1 txt