Loop through Sub Directories and search for set of files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Loop through Sub Directories and search for set of files
# 1  
Old 02-08-2011
Loop through Sub Directories and search for set of files

I have the below directory in unix environment

Code:
/home/bkup/daily: ls -lrt
drwxrwx--x   2 user user          256 Jan 12 18:21 20110112/
drwxrwx--x   2 user user          256 Jan 13 17:06 20110113/
drwxrwx--x   2 user user          256 Jan 14 16:44 20110114/
drwxrwx--x   2 user user          256 Jan 17 23:57 20110117/
drwxrwx--x   2 user user         4096 Feb 07 00:13 20110207/
-rw-rw----   1 user user      1495289 Feb 07 09:56 file1.txt
-rw-rw----   1 user user      9685950 Feb 07 16:47 file2.txt
-rw-rw----   1 user user       816186 Feb 07 16:52 file3.txt
-rw-rw----   1 user user    237195612 Feb 07 16:54 file4.txt

i need to ignore the files present in the directory. but i need to loop through all the sub directories present
in this directory and search for ABC.txt in each sub directories

if ABC.txt file present in a sub directory then
echo "ABC.txt present in sub_directory_name"
else
echo "ABC.txt not present in sub_directory_name"
fi

how can i accomplish this in unix shell script.
Please assist.

Last edited by radoulov; 02-08-2011 at 06:17 AM.. Reason: Code tags, please!
# 2  
Old 02-08-2011
Code:
_fn=ABC.txt

for d in */; do
  [ -f "$d/$_fn" ] &&
    printf '%s present in %s\n' "$_fn" "$d" ||
    printf '%s not present in %s\n' "$_fn" "$d" 
done

# 3  
Old 02-09-2011
Another way.

Code:
filename="ABC.txt"
find ./* -type d -print | while read DIR
do
       if [ -f "${DIR}/${filename}" ]
       then
             echo "${filename} present in ${DIR}"
       else
             echo "${filename} not present in ${DIR}"
       fi
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Move several files into specific directories with a loop

Hello, I'm a first time poster looking for help in scripting a task in my daily routine. I am new in unix but i am attracted to its use as a mac user. Bear with me... I have several files (20) that I manually drag via the mouse into several named directories over a network. I've used rsync... (14 Replies)
Discussion started by: SonnyClark
14 Replies

2. UNIX for Dummies Questions & Answers

Loop over certain user directories and find files

Hello I have user directories that contain /temp directory. Example folders: /user1/temp/ /user2/temp/ /user3/temp/ How can i loop over all user directories and find all files only in their /temp folder? Thanks a lot for help! (3 Replies)
Discussion started by: flavius42
3 Replies

3. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

4. Shell Programming and Scripting

How to access files from different directories and to perform search action in those files?

Hi, I want to access files from different directories (for example: /home/dir1/file1 , /home/dir2/file2 ...) Like this i have to access these files(file1, file2...). (3 Replies)
Discussion started by: bangarukannan
3 Replies

5. Shell Programming and Scripting

loop directories mv files to target in range

Hello, Currently I have a painstaking process that I use to move file for a monthly archive. I have to run the same two commands for 24 different directories. I wish to have a script with a for loop automate this and I have not been able to succeed. Here is what I do 24 times. I know this is... (5 Replies)
Discussion started by: jaysunn
5 Replies

6. Shell Programming and Scripting

Loop to move files in different directories

Hi, I have various log files in different paths. e.g. a/b/c/d/e/server.log a/b/c/d/f/server.log a/b/c/d/g/server.log a/b/c/h/e/server.log a/b/c/h/f/server.log a/b/c/h/g/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log and above these have an archive folder... (6 Replies)
Discussion started by: acc01
6 Replies

7. Shell Programming and Scripting

How to loop through directories to touch files

Hi, Please help me on this. Suppose i have the following directory structure. /app/data /app/data/eng /app/data/med /app/data/bsc each of the directories data,data/eng,data/med,data/bsc holds files with date extension like a.20081230 b.20081230 and so on I need a script to loop... (9 Replies)
Discussion started by: sussane
9 Replies

8. Shell Programming and Scripting

Script to loop through all files and directories.

I'm trying to write a script that will loop through all files and directories down from a path I give it, and change the permissions and ACL. I was able to do the obvious way and change the files and folders on the same level as teh path...but I need it to continue on deeper into the file... (2 Replies)
Discussion started by: cheetobandito
2 Replies

9. UNIX for Dummies Questions & Answers

Search for files in multiple directories

I want to search for a file pattern in more than one directory. How I need to do that? Here is the scenario: I am having a directory structure like the following: /log ...../20051001 ..........ftp_server_20051001.log ..........ftp_down_server.log ..........ftp_up_server.log... (7 Replies)
Discussion started by: ravikirankethe
7 Replies
Login or Register to Ask a Question