List files of arbitrary depth according to a given pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers List files of arbitrary depth according to a given pattern
# 1  
Old 03-01-2017
List files of arbitrary depth according to a given pattern

I would like to list all the .c and .h files in the current directory or any of its subdirectories.
I tried ls -R *.c *.h or ls -R | *.c *.h but that doesn't work.

A related question : how to copy all the .c and .h files in the current directory or any of its subdirectories into another directory.

Last edited by RudiC; 03-01-2017 at 02:13 PM..
# 2  
Old 03-01-2017
Try the find command.
# 3  
Old 03-01-2017
I remember struggling with the find command a few months ago. Part of my problem comes from my having the Unix version of find (it's a Mac I'm using), which is the poor man's version of the Linux version of find.
The examples I could google about find were about the Linux version and don't work on my machine.
Although when I do
Code:
man find

I get told that "The find utility recursively descends the directory tree for each path listed," but when I try
Code:
 find -f *.c *.h

(or variants thereof, for example adding -E, using -name) I get exactly what I would get with
Code:
 ls *.c *.h

, i.e. only the files in the immediate directory are listed, not the one in the subdirectories.
# 4  
Old 03-01-2017
When you use *.c and *.h without quotes, they are expanded by the shell before find ever sees them as operands. And, there is no need for any options for what you're trying to do. What you need is an expression that tells find what you want it to do.

To list files in the file hierarchy rooted in the current working directory that have names ending in .c or .h, that would be something like:
Code:
find . \( -name '*.c' -o -name '*.h' \)

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files to match file list pattern

Hi All, I have 100 folders with the first delimiter has a unique name i.e (123_hello and 575_hello) and each folder have atlist 1000 plus files with naming convention i.e (575_hello_1.iso ... 575_hello_1000.iso). 575_hello/575_hello_1.iso 575_hello/575_hello_2.iso 575_hello/575_hello_3.iso... (8 Replies)
Discussion started by: lxdorney
8 Replies

2. UNIX for Dummies Questions & Answers

List only files based on a pattern

Hi Gurus, I need to list only the files with out certain extension. For eg from the following list of files: I need to only list: Thanks Shash (7 Replies)
Discussion started by: shash
7 Replies

3. Shell Programming and Scripting

Working on files with names containing arbitrary indices in shell

Hi, I want to run a command in bash on files with names as file0001.txt, file0002.txt, file0003.txt and so on. The indices I have to work on are arbitrary like from file0009.txt to file0161.txt. So basically I want a string that can be updated from 0009 to 0010 to 0011 and so on. How can I do... (6 Replies)
Discussion started by: KidD312
6 Replies

4. Shell Programming and Scripting

list files with a specific pattern

How can I list files with the following specific criteria? I am trying this $> ls *.log or $>ls *.log? --> but it only gives me fsaffa.log1, rwerw.log2. How can I get all three files with a simple selection criteria ? (5 Replies)
Discussion started by: kchinnam
5 Replies

5. UNIX for Dummies Questions & Answers

Script to list non matching files using pattern

Hi, I am trying to write a script that list down all the files that do not match the pattern My pattern will be like this "*.jpg|*.xml|*.sql". This pattern will be stored in a file. The script need to read this pattern from the file and delete the files that does not match this pattern. It... (7 Replies)
Discussion started by: meenavin
7 Replies

6. Shell Programming and Scripting

Want to zip the all files till nth depth

All, i need a script which can zip the all files which are in directories and its subdirectories for example: dir1 contains file1,file2,dir1a,dir1b now dir1a also contains fil11,fil12 ,dirab so script should look for files in dir or sub dir till files not found and... (2 Replies)
Discussion started by: vijays3
2 Replies

7. Shell Programming and Scripting

how to list files with certain pattern

Hi I have files in the following naming convention, for example, /t1/bin/asrv270 /t1/bin/asrv270.sav /t1/bin/asrv2392.px2392.has_been_deleted.sav /t1/bin/asrv2713.sav.sav etc... The number after "asrv" is different. I need to list only the files which end up with the number, for example, ... (2 Replies)
Discussion started by: aoussenko
2 Replies

8. Shell Programming and Scripting

using sed to replace a pattern in list of files

Hi All, using the below grep command,I get the following output: $grep -irl "bc" /home/applmgr/amit > file_list.log $cat file_list.log /home/applmgr/amit/xyz.log /home/applmgr/amit/abc.log Requirement ========= Need sed utility to replace "bc" with "xy" pattern in the list of files... (4 Replies)
Discussion started by: a1_win
4 Replies

9. UNIX for Dummies Questions & Answers

List files that do not match the search pattern

I need to list the files that do not match the search pattern: Example: cat file1 This is how it should work cat file2 This is why I like Unix grep -option? Unix * (or some other command) returns file1 (7 Replies)
Discussion started by: olapxpert
7 Replies

10. IP Networking

List files that do not match the search pattern

I need to list the files that do not match the search pattern: Example: cat file1 This is how it should work cat file2 This is why I like Unix grep -option? Unix * (or some other command) returns file1 (1 Reply)
Discussion started by: olapxpert
1 Replies
Login or Register to Ask a Question