Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why can't I use a command like this to find all the pdf files in a directory and subdirectories? How do I do it? (I'm using bash in ubuntu)

ls -R *.pdf

EDIT

How would I then go about deleting all these files?

share|improve this question
up vote 16 down vote accepted

Why can't I use a command like this to find all the pdf files in a directory and subdirectories?

The wildcard *.pdf in your command is expanded by bash to all matching files in the current directory, before executing ls.


How do I do it? (I'm using bash in ubuntu)

find is your answer.

find . -name \*.pdf

is recursive listing of pdf files. -iname is case insensitive match, so

find . -iname \*.pdf

lists all .pdf files, including for example foo.PDF

Also, you can use ls for limited number of subfolders, for example

ls *.pdf */*.pdf

to find all pdf files in subfolders (matches to bar/foo.pdf, not to bar/foo/asdf.pdf, and not to foo.PDF).

If you want to remove files found with find, you can use

find . -iname \*.pdf -delete
share|improve this answer

As others have said, find is the answer.

Now to answer the other part.

  • How would I then go about deleting all these files?

    find . -iname *.pdf -exec rm {} \;

Should do it.

share|improve this answer
2  
You need to quote your glob to keep it from being prematurely expanded. – Dennis Williamson Feb 15 '11 at 15:43

Use find instead of ls

find . -name '*.pdf'
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .