Bash Command for Finding the size of all files with particular filetype in a directory in ubuntu

1.4k views Asked by At

I have a folder which contains several file types say .html,.php,.txt etc.. and it has sub folders also .Sub folders may contain all the file types mentioned above.

Question1:- I want to find size of all the files having the file type as '.html' which are there in both root directory and in sub- directories

Question2:- I want to find size of all the files having the file type as '.html' which are there only in root directory but not in sub folders.

I surfed through the internet but all i am able to get is commands like df -h, du -sh etc..

Are there any bash commands for the above questions? Any bash scripts?

1

There are 1 answers

0
Rodrigo Quesada On BEST ANSWER

You can use the find command for that.

#### Find the files recursively
find . -type f -iname "*.html"
#### Find the files on the r
find . -type f -maxdepth 1 -iname "*.iml"

Then, in order to get their size, you can use the -exec option like this:

find . -type f -iname "*.html" -exec ls -lha {} \;

And if you really only need the file size (I mean, without all the other stuff that ls prints):

find . -type f -iname "*.html" -exec stat -c "%s" {} \;

Explanation:

iname search of files without being case sensitive

maxdepth travels subdirectories recursively up to the specify level (1 means only the immediate folder)

exec executes an arbitrary command using the found paths, where "{}" represents the path of the file

type indicates the type of file (a directory is a file in Linux)