Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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

I am trying to look for all XML files in a particular directory and all sub-directories (recursively) inside it.

ls -R *.xml is only listing files in the current directory. I am quite sure, the sub-folders themselves have several .xml files, but none are showing up.

Is this a configuration issue?

share|improve this question
up vote 30 down vote accepted

Try using Find

sudo find . -print | grep -i '.*[.]xml'
share|improve this answer
1  
is the sudo must, or it's there to ensure super user privileges? – Shamim Hafiz Jun 13 '13 at 20:36
2  
I let you decide. Sudo, No Sudo. – Mitch Jun 13 '13 at 20:44
2  
Just out of interest. What is the advantage of find over ls -R? – don.joey May 22 '14 at 9:21
1  
@don.joey This might help stackoverflow.com/questions/13830036/… – Mitch May 22 '14 at 10:08
1  
-1 for mixing find and grep, when find can do filtering using both regexes and globs, and not using find's -print0 and grep's -z when you do need to mix. – muru Apr 3 '15 at 6:54

You can do it with find only:

find . -name '*.xml'

. is the current directory. If you need to search in another directory, replace . with the directory path.

share|improve this answer
1  
Does it search for the required file recursively in the directory rooted at current directory. In my case it just checked in the current directory only, didn't check the subdirectory. – mostafiz Sep 30 '14 at 20:24
    
@mostafiz, you need to quote the '*.xml' part. I'll edit my answer. – KaeruCT Oct 1 '14 at 14:39
    
Actually I searched for .php files in current directory. But it returned only .php files in current directory, didn't searched recursively in sub-directories. That's why I'm asking whether find command searches recursively or not. – mostafiz Oct 1 '14 at 16:52
    
@mostafiz, the find command searches recursively. If you don't quote the parameter, I think your shell might do an expansion on the *, so it will match the files in the current directory. – KaeruCT Oct 1 '14 at 19:38
    
All right! May be I'd made some mistake. Now it is working perfectly! – mostafiz Oct 2 '14 at 14:02

Try this command:

ls -R | grep '.*[.]xml'

ls doesn't have options to filter the output. For that you would need to use pipe. This passes the output from ls to grep, which then filters them to show just the .xml files.

share|improve this answer
3  
Anyway to get this to show the directory it came from? – ashkan Jul 28 '15 at 20:55

protected by Community May 6 at 13:10

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.