Look if a folder has some files with certain extensions

7,991

Solution 1

for dir in Abc/ Qwe/ Zxc/ Rty/; do
  result="$(find "$dir" -maxdepth 1 -type f -name '*.pdf' -printf .)"
  echo "Folder '${dir}' contains ${#result} *.pdf files."
done

This ignores subdirectories.

Solution 2

Use find /home/Desktop -type f -name '*.pdf'.

Solution 3

There are several different ways to do this, depending on what you want to do with the information.

  • If you want a list of directories which directly contain .pdf files (i.e. only at the top level, not within subdirectories), I'd go with this:

    dirname */*.pdf | sort | uniq
    
  • If you want a list of directories which contain .pdf files at any level (so they could be within subdirectories of the directory), try

    find . -name '*.pdf' | cut -d/ -f2 | sort | uniq
    

    or, if you want to search only certain subdirectories,

    find Abc Qwe Zxc Rty -name '*.pdf' | cut -d/ -f1 | sort | uniq
    
  • If you want a list of all the .pdf files within the subdirectories, that's just

    find . -name '*.pdf'
    
  • If you want a count of .pdf files within each directory, to count only files at the top level I'd use

    dirname */*.pdf | sort | uniq -c
    

    or to include files in subdirectories

    find . -name '*.pdf' | cut -d/ -f2 | sort | uniq -c
    

    (you can tell the difference is simply adding the -c option to uniq).

Solution 4

Using find is certainly the fastest way to do it. If you want to restrict the directories to look for, you can use:

find <dir1> <dir2> ... -type f -name '*.pdf'

If you want to count the number of files:

find <dir1> <dir2> ... -type f -name '*.pdf' | wc -l

Solution 5

With zsh:

for dir (Abc Qwe Zxc Rty) {
  pdf=($dir/*.pdf(ND.))
  print -r "Directory '$dir' contains ${#pdf} *.pdf files"
}
Share:
7,991

Related videos on Youtube

gkmohit
Author by

gkmohit

I am an Entrepreneur, Web Designer and Online Business Consultant. My mission is to help small businesses grow by leveraging the power of the internet. I believe in automating tasks by using tools so that you can focus on your core business. I have always been a curious person. The first time I used a computer was in grade 8 and fascinated by how you could create digital art using Corel Draw. In class 10, I had the opportunity to use the first mobile phone, and I was very intrigued by how the OS integrated with the hardware. That same curiosity led me to write my first piece of code in grade 10, and I then realized the power a programmer had in this world. In the mid-2011 family and I moved from Bangalore, India to Toronto, Canada, where I started my undergraduate degree in Computer Science at York University. As a student, I couldn't wait to get some industry experience, and I was fortunate to land my first job in IT at the University Information Technology department. I started as a Technical Analyst and slowly grew to be a software developer at the Student Information System. Gaining some industry experience gave me the confidence to go and attend a few hackathons across North America. I was fortunate to win a few awards from companies like Google, IBM, Bank of Nova Scotia and more while attending hackathons. With the help of my awards, experience and my skills, I started my internship at SAP Labs in Waterloo, Canada. My course was great, but I was seeking something more challenging, so my hackathon team members and I decided to start a fast-growing development shop Hyfer Technologies. At Hyfer Technologies, I stumbled upon Product Management and Business Analysis while managing a team of developers remotely. So far, I have been able to work with 10+ clients from conception to production. As a product manager, I have had a few failed projects but also some that are still growing strong. As of March 2020, I am working with The Ottawa Hospital as a Business Analyst. As a Product Manager &amp; Business Analyst, my skills include but are not limited to: Management Strategy Growth Strategy Customer, partner and client relations, Organizational Design Process Improvements Statistical Analysis and Data Mining Marketing and Brand Strategy Running Product-Related Sessions Managing technical team Through these skills and experience, I am confident I can add a lot of values to any growing team. I am always open to learning more about you and your business. Feel free to reach out to me or follow me on LinkedIn.

Updated on September 18, 2022

Comments

  • gkmohit
    gkmohit over 1 year

    Suppose I have a file structure:

    $ cd /home/Desktop
    $ ls -d */
    Abc/ Qwe/ Zxc/ Rty/
    $
    

    Now I want to iterate through every directory and see if they have any ".pdf" files. Could some one please tell what is the best way to do this?

  • gkmohit
    gkmohit almost 10 years
    This works exactly I want it to :D Thanks Hauke :)
  • Stéphane Chazelas
    Stéphane Chazelas almost 10 years
    That assumes GNU find.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 10 years
    This lists all the PDF files without indicating which toplevel directories contain any. That's like giving someone a cow when they ask for a hamburger.
  • gena2x
    gena2x almost 10 years
    Incorrect. This lists all pdf files with directories for each file. So you can easily see which file belongs to which directory. Yes, it seems author wanted something more like true/false, but i see no way to figure this out from question