Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Use ls or find for search of subdirectories?


 
Thread Tools Search this Thread
# 2  
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  
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  
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  
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  
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.
 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Search subdirectories and find and print total files
Kevin Tivoli
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...... Shell Programming and Scripting
10
Shell Programming and Scripting
Find command to search files in a directory excluding subdirectories
jhilmil
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...... Shell Programming and Scripting
8
Shell Programming and Scripting
search for files in subdirectories
euclid3628800
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... UNIX for Dummies Questions & Answers
3
UNIX for Dummies Questions & Answers
How to search all subdirectories?
david_wang
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... UNIX for Dummies Questions & Answers
4
UNIX for Dummies Questions & Answers

Featured Tech Videos