List Directory names which have the file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers List Directory names which have the file
# 1  
Old 07-14-2013
List Directory names which have the file

Hi All,

Can any one help me to list out the directory names which contain the specified file.
See for example
Code:
File name : file.201307014.LKT

Have the directory structure as below.
Code:
/app/work/data/INDIA/file.201307014.LKT
/app/work/data/AMERICA/file.201307014.KTP
/app/work/data/EUROPE/file.201307014.LKT
/app/work/data/ENGLAND/file.201307014.PNP
/app/work/data/AUSTRALIA/file.201307014.LKT

while search for the file.Has to print the directories which contain the file.
Code:
IF file.201307014.LKT contains
Output :  INDIA/EUROPE/AUSTRALIA

Please consider there are lot of directories

Thanks in advance!!!
# 2  
Old 07-14-2013
Try:
Code:
ls /app/work/data/*/file.201307014.LKT | awk -F/ '{print $(NF-1)}' | paste -sd / -

# 3  
Old 07-14-2013
Or
Code:
echo /app/work/data/*/file.201307014.LKT | awk -F/ '{print $(NF-1)}' RS=" "

# 4  
Old 07-14-2013
@MadeinGermany:
The trouble with that is, that it does not work with directories with spaces, it will produce an asterisk if the files are not present and it will produce a trailing ORS character without a newline if the desired ORS ("/") is used...
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 07-14-2013
Yes, in principle. (And yes, we should go for general correctness.)
From the example I assumed there aren't any spaces.
Like you assumed there isn't any directory "file.201307014.LKT" - for the paranoids it is ls -d (don't list contents).
# 6  
Old 07-14-2013
Yes, I was thinking of NEW ZEALAND Smilie

--
If there are really many directories:
Code:
for file in /app/work/data/*/file.201307014.LKT
do
  [ -f "$file" ] && printf "%s\n" "$file";
done | awk -F/ '{print $(NF-1)}' | paste -sd / -

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to list files names and sizes in a directory and output result to the file?

Hi , I'm trying to list the files and output is written to a file. But when I execute the command , the output file is being listed. How to exclude it ? /tmp file1.txt file2.txt ls -ltr |grep -v '-' | awk print {$9, $5} > output.txt cat output.txt file1.txt file2.txt output.txt (8 Replies)
Discussion started by: etldeveloper
8 Replies

2. Shell Programming and Scripting

Compare file names on directory

Dears, Would you please help on following bash script: I want to get the most recent file named alfaYYYYMMDD.gz in one directory: for example: in directory /tmp/ ls -ltr alfa20130715.gz holding.gz alfa20130705.gz sart.txt merge.txt.gz alfa20130802.gz my result shoud be... (1 Reply)
Discussion started by: maxsub
1 Replies

3. Shell Programming and Scripting

Script to list the file names in a directory

Hi all, I need a shell script to write into a .txt file the no of files in a directory with extension and separated by comma in between. For eg, a directory contains files like a.csv b.csv c.csv d.csv Then the output in the output.txt file should be like a.csv,b.csv,c.csv,d.csv ... (3 Replies)
Discussion started by: dubuku_01
3 Replies

4. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

5. UNIX for Dummies Questions & Answers

How can i copy a list of files with different names into others directory have the same name?

dear all. how can i copy a list of files with different names into others directory have the same name like i have 3 files 10_10 10_10_11 10_10_11_12 and i have 3 directories 10_10 10_10_11 10_10_11_12 how can i make a loop to cp this files into the directory have the same name like... (31 Replies)
Discussion started by: t17
31 Replies

6. Shell Programming and Scripting

how can i copy a list of files with different names into others directory have the same name

dear all. how can i copy a list of files with different names into others directory have the same name like i have 3 files 10_10 10_10_11 10_10_11_12 and i have 3 directories 10_10 10_10_11 10_10_11_12 how can i make a loop to cp this files into the directory have the same name like... (0 Replies)
Discussion started by: t17
0 Replies

7. Shell Programming and Scripting

How to read file names in the directory?

I am having n files in a directory i want to read all the file names from the script file .It is better if any one provide a sample script. Elaborating the scenario: i am having n number of sql files in a directory i am running all the sql files from a single script. sqlplus... (4 Replies)
Discussion started by: dineshmurs
4 Replies

8. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

9. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

10. Shell Programming and Scripting

directory names in a flat file

Hi, Consider a flat file abc.conf contains some rows. Each row contains the directory name with full path. now I want to find a particular file in every directory which are mentioned in the abc.conf file. How it can be done through unix shell script. (2 Replies)
Discussion started by: surjyap
2 Replies
Login or Register to Ask a Question