Need help in checking for files in subfolders


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need help in checking for files in subfolders
# 1  
Old 12-29-2011
Need help in checking for files in subfolders

Hi,

I am trying to print a listing of files from the top level directory, check to see if any files have the same name as the top level directory name and if so, cd to that file and list the files under it. Don't know how to check for the file in the next level.

What I have so far:
Code:
#/usr/bin/ksh!
#Usage: ksh search.ksh directory_name
rm -f cat_report.dat
cd $1 
pwd
echo "Directory is $1" >> /apps/system_admin/scripts/cat_report.dat
ls -l >> /apps/system_admin/scripts/cat_report.dat
while [[ -d $1 ]];
do
echo "Directory $1 does exist in $(pwd)"  >> /apps/system_admin/scripts/cat_report.dat 


# 2  
Old 12-30-2011
You are trying to find files with duplicate names in a directory tree. Yes?
I would try something like this:
Code:
#!/bin/ksh
find $1 -type f |
  while read fname 
  do
     d=dirname($fname)
     f=basename($fname)
     echo "$f $d  $fname "
  done | sort > tmp.tmp
awk ' arr[$1]++; END{for(i in arr){if(arr[i]>1) {print i, arr[i]}   } }' tmp.tmp  |
   sort -u > list.txt
grep -F -f list.txt  tmp.tmp

start with that.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 12-30-2011
Quote:
check to see if any files have the same name as the top level directory name and if so, cd to that file and list the files under it.
Sorry, don't understand this. You cannot "cd" to a file, you can only "cd" to a directory. Are you actually looking for duplicate directory names?
Can you give an example?
# 4  
Old 12-30-2011
Sorry, yes you are right. I would be doing a 'cd' to a directory under the current one with the same name.

Ex:

Mount point name - /cat
cd /cat
List directories under /cat using ls -l
cat
dog
fish
Find another directory called cat
cd to that directory and list the directories
cd cat
ls -l
keep checking until a directory called cat is not found.

Thanks!
# 5  
Old 12-30-2011
Code:
find "$PWD" -type d -name "${PWD##*/}"

This User Gave Thanks to xbin For This Post:
# 6  
Old 12-30-2011
Something similar to this?

Code:
find "/cat" -xdev -type d -name "cat" -print | sort | while read dir
do
        echo "${dir}"
        cd "${dir}"
        ls -ld * | grep \^\d   # Directories only
        echo ""
done

Hard to understand your requirement. Not clear whether you are suggesting using "ls" (not "find") to locate the directory or whether you actually want to display the output. I guessed the latter. Note the use of "ls -ld *" to avoid "ls" listing the contents of subdirectories.
This User Gave Thanks to methyl For This Post:
# 7  
Old 12-30-2011
Quote:
Originally Posted by methyl
Something similar to this?

Code:
find "/cat" -xdev -type d -name "cat" -print | sort | while read dir
do
        echo "${dir}"
        cd "${dir}"
        ls -ld * | grep \^\d   # Directories only
        echo ""
done

Hard to understand your requirement. Not clear whether you are suggesting using "ls" (not "find") to locate the directory or whether you actually want to display the output. I guessed the latter. Note the use of "ls -ld *" to avoid "ls" listing the contents of subdirectories.

Thanks, I'll give this a try. Yes, I need to get an actual listing of all of the directories at each level as long as the criteria of finding the same directory name in each. Once the directory does not have any other directories with the same name as the one previous it is complete.

ex: /cat - top level
ls -ld - shows the following directories; cat, dog, fish
Because cat is listed, cd cat
ls -ld - shows the following directories; cat, horse, duck
Again, cat is listed so 'cd cat'
ls -ld - shows the following directores; bird, turtle, snake
No 'cat' listed so completes.

Hope this helps to clarify.

---------- Post updated at 10:26 AM ---------- Previous update was at 10:12 AM ----------

Quote:
Originally Posted by methyl
Something similar to this?

Code:
find "/cat" -xdev -type d -name "cat" -print | sort | while read dir
do
        echo "${dir}"
        cd "${dir}"
        ls -ld * | grep \^\d   # Directories only
        echo ""
done

Hard to understand your requirement. Not clear whether you are suggesting using "ls" (not "find") to locate the directory or whether you actually want to display the output. I guessed the latter. Note the use of "ls -ld *" to avoid "ls" listing the contents of subdirectories.
Thank you. This works!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find for files within a list of subfolders

Helo Is there a better way to search within a list of subfolders : A_START_PATH="/data_1/data_2" # # dir2, dir3, dir6, ..... dir59 exists # A_LIST="$A_START_PATH/dir1 $A_START_PATH/dir4 $A_START_PATH/dir5" find "$A_LIST" -type f -name"*.txt" Now searching for all files in any subdirs... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Splitting a folder containing different files into subfolders

I have a folder with 4000 (*3) files like gr_q4_gb-1.anc gr_q4_gb-1.anc_cdr_st.txt gr_q4_gb-1.anc_cdr_tr.txt gr_q4_gb-2.anc gr_q4_gb-2.anc_cdr_st.txt gr_q4_gb-2.anc_cdr_tr.txt gr_q4_gb-3.anc gr_q4_gb-3.anc_cdr_st.txt gr_q4_gb-3.anc_cdr_tr.txt . . gr_q4_gb-4000.anc... (6 Replies)
Discussion started by: sammy777888
6 Replies

3. Shell Programming and Scripting

List all the files in the present path and Folders and subfolders files also

Hi, I need a script/command to list out all the files in current path and also the files in folder and subfolders. Ex: My files are like below $ ls -lrt total 8 -rw-r--r-- 1 abc users 419 May 25 10:27 abcd.xml drwxr-xr-x 3 abc users 4096 May 25 10:28 TEST $ Under TEST, there are... (2 Replies)
Discussion started by: divya bandipotu
2 Replies

4. Shell Programming and Scripting

Help to move folders, subfolders and files from unix to windows

Hi Unix Gurus, I am able to copy only files that exist in the parent folder. My parent folder has sub folders and within sub folders there are lots files. I need to copy folder, sub folders and files from Unix to the remote windows SFTP location. The directory structure is something like... (1 Reply)
Discussion started by: shankar1dada
1 Replies

5. Homework & Coursework Questions

unique words in files of folder and its subfolders

Hello, I tried to count all unique words of all files in one folder and its subfolders. Can anybody say me, why this doesnt work: ls| find -d | cat | tr "\ " "\n"| uniq -u | wc -l ??? Cat writes only the names of those files, but not the wors, which should be in them. Thanks for any advice. ... (9 Replies)
Discussion started by: Dworza
9 Replies

6. UNIX for Dummies Questions & Answers

How to obtain a count of files in a folder and it's subfolders

First of all, the extent of my unix knowledge is next to nil. I've been able to telnet to a unix box, and thanks to the Computer Hope website, I've been able to learn a few basic commands to navigate from folder to folder, and view contents. What I really need to do is obtain a count of all... (2 Replies)
Discussion started by: scarfinv
2 Replies

7. UNIX for Dummies Questions & Answers

Split 200.000 files into different subfolders

Dear UNIX-Community, can help me doing 2 things in Debian 5.0? 1.) Create 100 folders Format: ./0/0, ./0/1, ./0/2, ...,./0/9, ./1/0/, ..., ./9/9 2.) Move over 200.000 files into the subdirectories according to their last digits. 12398123.dat -> ./3/2/12398123.dat 48161.dat ->... (2 Replies)
Discussion started by: diadas
2 Replies

8. Shell Programming and Scripting

Replace string in all files in a folder and subfolders.

i need to change string in all files in current folder and all subfolders. i wrote the following script. It works good except it dont delete temp file from subfolders. for z in `find . -type f -name "*.html" -o -name "*.htm"`; do sed -e 's@abc@xyz@g' $z>temp; mv temp $z; done any idea?... (1 Reply)
Discussion started by: crazynups
1 Replies

9. UNIX for Dummies Questions & Answers

How to learn the number of files under a particular folder, containing subfolders

Hi ALL I would like know how many files there under a particular folder, which contains subfolders. Thanks (5 Replies)
Discussion started by: cy163
5 Replies

10. UNIX for Dummies Questions & Answers

rename files in subfolders

Hello i have this script : foreach f ($1/*.cpp ) mv $f $f:r.c end that renames me files in dir , how can i change it so it will rename me also in subdirectorys? thanks (0 Replies)
Discussion started by: umen
0 Replies
Login or Register to Ask a Question