Getting directories resulted from Find in one file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Getting directories resulted from Find in one file
# 1  
Old 08-18-2012
Getting directories resulted from Find in one file

Hi

Need help for the following scenario.

I am having two directories /tmp/a and /tmp/b. /tmp/a again has subdirectories /tmp/a/aa and /tmp/a/ab.

I want to run a script from /tmp/b to search for a file user.lst in the folders /tmp/a/aa and /tmp/a/ab, if available, i want to make a file in /tmp/b new_user.lst which will contain the content of each user.lst file found plus the directory path (in which it has been found) for each line of the file.

E.g;

Content of user.lst in /tmp/a/aa
Quote:
apple
orange
Content of user.lst in /tmp/a/ab
Quote:
cat
rat
Resulting file new_user.lst in /tmp/b should contain
Quote:
apple /tmp/a/aa
orange /tmp/a/aa
cat /tmp/a/ab
rat /tmp/a/ab
Thanks,
Sudeep
# 2  
Old 08-18-2012
Like this?:
Code:
#!/usr/bin/ksh
exec 2>/dev/null
rm new_user.lst
find /tmp/a -type f -name user.lst|while read i
do
 if [[ $i == "/tmp/a/aa/user.lst" || $i == "/tmp/a/ab/user.lst" ]]
 then
  pathfound=${i%/*}
  sed 's:$: '"$pathfound"':' "$i" >> new_user.lst
 fi
done

Alternatively, you could try (if only 2 directories need to be looked at for the file):
Code:
#!/usr/bin/ksh
rm -f new_user.lst
files='/tmp/a/aa/user.lst /tmp/a/ab/user.lst'
for i in $files
do
 if [[ -r $i && ! -d $i ]]
 then
  pathfound=${i%/*}
  sed 's:$: '"$pathfound"':' "$i" >> new_user.lst
 fi
done

I'm assuming that you are running the script from /tmp/b so that the PWD inherited by the sub-process is /tmp/b.

Last edited by elixir_sinari; 08-18-2012 at 07:03 AM..
# 3  
Old 08-18-2012
Assuming every user.lst file in the hierarchy rooted at /tmp/a is desired:
Code:
find /tmp/a -name user.lst -exec awk 'FNR==1 {p=FILENAME; sub("/[^/]*$", "", p)} {print $0,p}' {} + > new_user.lst

Regards,
Alister
# 4  
Old 08-18-2012
Also, using grep (and sed):
Code:
find /tmp/a -type f -name user.lst -exec grep -H '.*' '{}' + | sed 's|/user.lst:| |'  >> /tmp/b/new_user.lst

To have the exact desired output (switching the user and the path):
Code:
find /tmp/a -type f -name user.lst -exec grep -H '.*' '{}' + | sed -e 's|/user.lst:| |' -e 's|^\(.*\) \([a-z_][a-z0-9_-]*\)$|\2 \1|' >> /tmp/b/new_user.lst

--
Bye
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing resulted string name of a gzipped file as an argument to another piped tool

Hi, I have a .pcap.gz file and I would like to initially gzip it and then pass the resulting .pcap filename as an argument to a piped tool; the right-hand tool is not standardized linux tool but a custom one that strictly requires the string name of a given .pcap file in order for the pcap file... (2 Replies)
Discussion started by: amarn
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Find the newest file in two directories

Hi i have two directories and files inside them Directory 1: Directory 2: These files are the same but song.mp3 in Directory 1 is newer than song.mp3 in Directory 2, and work.txt in Directory 2 is newer than work.txt in Directory 1. Now is my question. How i can compare these files... (10 Replies)
Discussion started by: Falstaff
10 Replies

3. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

4. Shell Programming and Scripting

How to find 777 permisson is there or not for Directories and sub-directories

Hi All, I am Oracle Apps Tech guy, I have a requirement to find 777 permission is there or not for all Folders and Sub-folders Under APPL_TOP (Folder/directory) with below conditions i) the directory names should start with xx..... (like xxau,xxcfi,xxcca...etc) and exclude the directory... (11 Replies)
Discussion started by: gagan4599
11 Replies

5. Shell Programming and Scripting

How to find the recent file in many sub-directories?

Hi guys, Under my root directory there are many sub-directories which contains log file for every day of running. How can I find , in one command only, the recent log file in each sub-directory? For example, If I run the following: find . -name "exp_prod_*_*_yes_*_.log" -exec ls -ltr {} \;... (12 Replies)
Discussion started by: nir_s
12 Replies

6. Shell Programming and Scripting

Find Directories That Do Not Contain A Certain File

I'm having problems figuring out the process to find directories that DO NOT contain a certain file. I have a mp3 collection that all the album art is name "folder.jpg". Not all the albums have images. I need a way to find the albums/directories that do not contain "folder.jpg". I can find the... (2 Replies)
Discussion started by: subsonic
2 Replies

7. Shell Programming and Scripting

find directories that do NOT contain a certain file extension

for x in `find /vmfs/volumes/v01tstn01a01/ -type d`; do find $x -name '*.vmx' > /dev/null || echo $x; done; The goal of this is to find the subdirectories that do NOT contain a file with the extension of .vmx Any help would be great! (6 Replies)
Discussion started by: hodnov
6 Replies

8. Shell Programming and Scripting

find directories

I am looking a script which will find garbase directories (10 char. long)..which need to delete empty dir excluding symlink and system directories. I tried this partial script but it just find directories create a file which contains link directories..can somebody help me to complete this... (4 Replies)
Discussion started by: ddk2oo5
4 Replies

9. Shell Programming and Scripting

Find a file in sub-directories.. o/p just the path

Hello All, I am somehow stumped with this ting. 'Find' will sure show me.. but I want only thepath of all the occurences of the file in any of the sub-dirs.. Any help will be sincerely appreciated. thanx! (3 Replies)
Discussion started by: pranavagarwal
3 Replies

10. Programming

to find a file in set of directories

Hi, what is the command in unix to find a file abc.c in a directory which had n number of sub-directories. thanx (3 Replies)
Discussion started by: jazz
3 Replies
Login or Register to Ask a Question