Use ls or find for search of subdirectories?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Use ls or find for search of subdirectories?
# 1  
Old 12-24-2013
Use ls or find for search of subdirectories?

# 2  
Old 12-24-2013
Your title is too vague to understand what you want to do. Are you just trying to list the files of type directory that appear in the file hierarchy beneath a given starting directory? Are you looking for regular files matching some pattern in a certain set of directories?

Show us an example of the files you're working with and show us the output you want!
# 3  
Old 12-24-2013
Sorry, for some reason, the text in the post didn't come out. I'll try again:

I want to find all of the files of type .vtk in a directory and its subdirectories and then do some operation on them. I don't want to list the files only.

Here's what I have now:

Code:
ls *.vtk |\                           #gets files
while read file ; do		#reads files
                                        #does stuff
done

The above works on the files in the directory only.

I've tried:
Code:
ls -R *.vtk |\

but this doesn't work to get the files from the subdirectories.

Should I use some variant of:
Code:
 find $HOME/directory_with_subs -name “*.vtk”

to operate on the files in both the directory and its subdirectories?


Thanks,

Last edited by jhsinger; 12-24-2013 at 06:41 AM.. Reason: Add CODE tags
# 4  
Old 12-24-2013
Hello,

The following may help you for same.

Code:
$ ls -ltR | grep '^d'

Output will be as follows.

Code:
drwxr-sr-x    2 singh singh1           512 Dec 11 06:48 chumma_checking_find
drwxr-sr-x    2 singh singh1           512 Dec 11 05:16 perl_learning
drwxr-sr-x    3 singh singh1           512 Nov 26 19:52 check_del
drwxr-sr-x    3 singh singh1           512 Nov 26 19:53 abc
drwxr-sr-x    2 singh singh1           512 Nov 26 19:53 abc12



Thanks,
R. Singh
# 5  
Old 12-24-2013
Quote:
Originally Posted by jhsinger
Sorry, for some reason, the text in the post didn't come out. I'll try again:

I want to find all of the files of type .vtk in a directory and its subdirectories and then do some operation on them. I don't want to list the files only.

Here's what I have now:

Code:
ls *.vtk |\                           #gets files
while read file ; do		#reads files
                                        #does stuff
done

The above works on the files in the directory only.

I've tried:
Code:
ls -R *.vtk |\

but this doesn't work to get the files from the subdirectories.

Should I use some variant of:
Code:
 find $HOME/directory_with_subs -name “*.vtk”

to operate on the files in both the directory and its subdirectories?


Thanks,
Yes, you're close. To work on files with names ending in .vtk in the current directory and its subdirectories, try:
Code:
find . -name '*.vtk' |
while read file
do
        printf "Do whatever you want to do with %s\n" "$file"
done

If you want to use your home directory as the base of the search instead of the current directory use:
Code:
find $HOME -name '*.vtk' |

as a replacement first line for the above script. If you want to search several file hierarchies, you can name more than one directory before the -name primary, as in:
Code:
find $HOME $Other_Dir $And_Another_Dir -name '*.vtk' |

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 12-24-2013
Depending on how complex the manipulation you want to do on each match is, you could use "-exec" with find.

Frinstance:
Code:
find ${HOME} -name '*.vtk' -exec chmod 744 {} \;

The "{}" is replaced with each match to the find.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search subdirectories and find and print total files

Hi All, I have a folder name lets say path/to/folder/CUSTOMER and under this i have several folders and each of these subfolder have serveral subfolders and so on and at some point i will have a folder name called "FTP_FILES" . I need to search for these folders named "FTP_FILES and then... (10 Replies)
Discussion started by: Kevin Tivoli
10 Replies

2. Shell Programming and Scripting

Find command to search files in a directory excluding subdirectories

Hi Forum, I am using the below command to find files older than x days in a directory excluding subdirectories. From the previous forums I got to know that prune command helps us not to descend in subdirectories. Though I am using it here, not getting the desired result. cd $dir... (8 Replies)
Discussion started by: jhilmil
8 Replies

3. UNIX for Dummies Questions & Answers

Find directories without subdirectories

Hello, I have to find all directories, which contain files, but dont have subdirectories. For example if i have tree like: ├── kat11 │ ├── kat21 │ │ └── Dokument\ bez\ nazwy │ └── kat22 │ ├── kat31 │ │ └── Dokument\ bez\ nazwy │ └── kat32 │ └──... (13 Replies)
Discussion started by: eValker
13 Replies

4. UNIX for Dummies Questions & Answers

How to search & list subdirectories with the given file name

Question: How to search & list subdirectories with the given file name? For example: The directory structure looks like; /Builds/DEV/Build_RL01/DDL/ a_tbl_cre.sql ...... /Builds/DEV/Build_RL01/DML/ a_upd.sql .... .... Requirements: 1. I need to find subdirectory DML which is... (0 Replies)
Discussion started by: Siva SQL
0 Replies

5. Shell Programming and Scripting

search in all subdirectories for a file containing a certain string

Hello, how can I search a directory AND all its subdirectories for a file containing a certain string? My directories contain too many sql-files and I want to know whcih one of them write into the table "customer"? Can anyone help me? Thanks in advance (1 Reply)
Discussion started by: ABE2202
1 Replies

6. UNIX for Dummies Questions & Answers

search for files in subdirectories

Hi! I want to find files located in subdirectories. I have tried ls -R | grep myfile but this won't tell me where the file is, only that it is there. Any one have a better idea? Thanks, --Euclid (3 Replies)
Discussion started by: euclid3628800
3 Replies

7. Shell Programming and Scripting

search files in a directory and its subdirectories

Hello my friends, I need to write a simple shell bad file :D that search and delete a file it's name 'Microsoft.txt' in the current directory and its subdirectories? So can you help to guide me how i can write this shell, Just give me the beginning :o thank you. (1 Reply)
Discussion started by: Net-Man
1 Replies

8. Shell Programming and Scripting

find . but not access subdirectories

Hello, I'm trying to figure out how to prevent this find command from accessing this diretories's subdirectories! I tried the maxdepth and prune but they don't seem to work on SUN. So now i'm trying to set up a nawk command to not process any files that have "REVISED" in there name: find... (3 Replies)
Discussion started by: bobk544
3 Replies

9. UNIX for Dummies Questions & Answers

How to search all subdirectories?

Dear All, I want to write the Unix command that searches through all subdirectories, finds the files named ''core'' and deletes them. I will very much appreciate your help. David (4 Replies)
Discussion started by: david_wang
4 Replies
Login or Register to Ask a Question