Work on multiple directories, same filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Work on multiple directories, same filename
# 1  
Old 07-31-2013
Work on multiple directories, same filename

Can this be done and if yes how?
I have 10 directories that end with the name 'cuff'

eg dir1-cuff , dir2-cuff

All these directories have a file named xyz.gtf


How do I run the same code for each directory and print an output inside the directory

something like
Code:
for dir in *cuff
do
cd $dir
awk '{.......}' xyz.gtf > abc.txt
cd ..
done

# 2  
Old 07-31-2013
Code:
for dir in *cuff
do
        [ -d "$dir" ] && awk '{...}' "${dir}/xyz.gtf"
done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 07-31-2013
..., and apply the same ${dir} to the output file: > "${dir}/abc.txt"
# 4  
Old 07-31-2013
Quote:
Originally Posted by Yoda
Code:
for dir in *cuff
do
        [ -d "$dir" ] && awk '{...}' "${dir}/xyz.gtf"
done

You can leverage pathname expansion to further simplify:
Code:
for dir in *cuff/
do
        awk '{...}' "${dir}/xyz.gtf"
done

Regards,
Alister
This User Gave Thanks to alister For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

reading filename in nested directories

Hi all, I have some directory structure as $ls -ltr drwxr-xr-x 1 mscprod us_msc 512 May 10 08:34 650 drwxr-xr-x 1 mscprod us_msc 512 May 10 08:51 652 drwxr-xr-x 1 mscprod us_msc 512 May 10 08:51 640 drwxr-xr-x 1 mscprod us_msc512 May 10 09:13 654 $ cd 650/ $ls fileabc.root and within... (7 Replies)
Discussion started by: emily
7 Replies

2. UNIX for Dummies Questions & Answers

Deleting multiple directories inside multiple directories

Hi, Very unfamiliar with unix/linux stuff. Our admin is on vacation so, need help very quickly. I have directories (eg 40001, 40002, etc) that each have one subdirectory (01). Each subdir 01 has multiple subdirs (001, 002, 003, etc). They are same in each dir. I need to keep the top and... (7 Replies)
Discussion started by: kkouraus1
7 Replies

3. Shell Programming and Scripting

Find directories that contains more than n matches of a certain filename

I need to construct a command that finds directories which contains more than n matches of a certain filename. E.g. I have many directories at different locations and want to find all directories that has 2 or more .dat-files. I thought of using find and maybe the exec parameter to issue an... (5 Replies)
Discussion started by: odyssey
5 Replies

4. Shell Programming and Scripting

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (4 Replies)
Discussion started by: Sriranga
4 Replies

5. UNIX for Dummies Questions & Answers

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (2 Replies)
Discussion started by: Sriranga
2 Replies

6. Shell Programming and Scripting

Substitution in a file dont work with an Array in filename

Hi. I´ve a script that should substitude the 8th line in a file called xxx.num6. The "xxx" is set by an array filled with this command: j=0 for Par in *.sys ; do Par=`echo $Par | sed 's/\(.*\).sys/\1/'` ; Par2="$Par" ; echo "${Par2}" j=$((j + 1)); done Now i try... (0 Replies)
Discussion started by: Lock3
0 Replies

7. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

8. Shell Programming and Scripting

searching for a filename in ALL directories

Hi, I am searching for a file named "Backup.txt" but I don't know in which directory it is. Can someone tell me, how I can search recursiv in all directories and subdirectories? thanks (2 Replies)
Discussion started by: ABE2202
2 Replies

9. Shell Programming and Scripting

how does set--$(ls -l filename) work?

i'm not too sure how to use set--$(ls -l filename) to set values of positional parameters. i'm supposed to write a script that accepts a directory as an optional command line parameter. and for that directory, i'm supposed to display a single line of information about each file within it: file... (3 Replies)
Discussion started by: jaay
3 Replies

10. Shell Programming and Scripting

moving directories to new directories on multiple servers

Hi - I am new to unix scripts...I need to move several directories on multiple servers to new directories. (0 Replies)
Discussion started by: mackdaddy07
0 Replies
Login or Register to Ask a Question