Re: How to print path of all files with same name in a given directory's sub directories?

48 views
Skip to first unread message

Hermenegildo Konstantin

unread,
Jan 24, 2013, 8:24:45 AM1/24/13
to linuxus...@googlegroups.com


Dana četvrtak, 24. siječnja 2013. 02:58:42 UTC+1, korisnik Andrew Taylor napisao je:
Hi,

I'm struggling to figure out if I can do this in bash  - can anyone offer me some advice? Within this folder:

/projects/django-stringer/txc

I have about 30 sub-folders. each one contains a folder within a folder that contains a file called "google_transit.zip" I'd like to print out this list of paths in full if I can. An example path would be:

/home/andyt/projects/django-stringer/txc/OId_CW/GTFS/google_transit.zip

Thanks!

Andy

Andrew Taylor

unread,
Jan 24, 2013, 10:34:52 PM1/24/13
to linuxus...@googlegroups.com
Thank you so much for your help, it seems so obvious to see it written down!.

Could you help with one other thing? I'm trying to extract the directory name to form a description for each path returned. This is what I have so far:

for OUTPUT in $(find `pwd` -name google_transit.zip)
do
while IFS='/' read -ra $OUTPUT; do
echo "                      <bean class=\"org.opentripplanner.graph_builder.model.GtfsBundle\">"
echo "                         <property name=\"path\" value=\""$OUTPUT"\" />"
echo "                          <property name=\"defaultAgencyId\" value=\"""${OUTPUT[6]}""\" />"
echo "                      </bean>"
done
done

What am I doing wrong?

I was trying to follow this example:

A line in OUTPUT could for example be the below. In this case I'd like to extract "OId_LG"
"/home/andyt/projects/django-stringer/txc/OId_LG/GTFS/google_transit.zip"


Thanks!

BluesRenegade

unread,
Jan 24, 2013, 1:03:17 PM1/24/13
to linuxus...@googlegroups.com
Copy and paste the following, as is, into the command lineat the top
level directory, or use the 2nd version if you want to specify the
absolute starting folder.

NOTE:
1. iname doesn't care if the name is upper or lower case
2. -ls gives the complete path in the output listing, one file per line.
The -ls option outputs like the 'ls' command.
3. The single quotes are imperative to prevent the shell from
preprocessing the search filename.
The find command receives your search text as you intended.

1st Version:

find . -iname '*google_transit.zip" -ls

2nd Version:

find /projects/django-stringer/txc -iname '*google_transit.zip' -ls



On 01/23/2013 07:58 PM, Andrew Taylor wrote:
> Hi,
>
> I'm struggling to figure out if I can do this in bash - can anyone
> offer me some advice? Within this folder:
>
> /projects/django-stringer/txc
>
> I have about 30 sub-folders. each one contains a folder within a
> folder that contains a file called "google_transit.zip" I'd like to
> print out this list of paths in full if I can. An example path would be:
>
> /home/andyt/projects/django-stringer/txc/OId_CW/GTFS/google_transit.zip
>
> Thanks!
>
> Andy
>
>
>
>
> --
> You received this message because you are subscribed to the Linux
> Users Group.
> To post a message, send email to linuxus...@googlegroups.com
> To unsubscribe, send email to linuxusersgro...@googlegroups.com
> For more options, visit our group at
> http://groups.google.com/group/linuxusersgroup
> References can be found at: http://goo.gl/anqri
> Please remember to abide by our list rules
> (http://tinyurl.com/LUG-Rules or http://cdn.fsdev.net/List-Rules.pdf)

--
Blues

tid

unread,
Jan 24, 2013, 2:53:11 AM1/24/13
to linuxus...@googlegroups.com
find /projects/django-stringer/txc -type f -a -name "google_transit.zip"

should do it.

regards

Tid

tid

unread,
Jan 25, 2013, 5:35:27 AM1/25/13
to linuxus...@googlegroups.com
have a look at the 'dirname' command - you can extract a file's
directory name with that. In your example
below you look as if you're trying to extract the directory two levels
above your file - is that what you want?

Tid
> --

Andrew Taylor

unread,
Jan 25, 2013, 7:22:46 AM1/25/13
to linuxus...@googlegroups.com, t...@bloogaloo.co.uk
Yes that's right, thanks for the advice.

I'll look into that command now
Message has been deleted

Andrew Taylor

unread,
Jan 25, 2013, 9:33:21 AM1/25/13
to linuxus...@googlegroups.com, t...@bloogaloo.co.uk
This is what I have settled on (it works!).  Are there any reasons why this could be bad practice?

for OUTPUT in $(find `pwd` -name google_transit.zip)
do
IFS='/' read -ra AGENCYID <<< "$OUTPUT"
echo "                      <bean class=\"org.opentripplanner.graph_builder.model.GtfsBundle\">"
echo "                         <property name=\"path\" value=\""$OUTPUT"\" />"
echo "                          <property name=\"defaultAgencyId\" value=\""${AGENCYID[6]}"\" />"
echo "                      </bean>"
done

On Friday, 25 January 2013 10:35:27 UTC, tid wrote:

tid

unread,
Jan 25, 2013, 9:58:08 AM1/25/13
to linuxus...@googlegroups.com
On 25 January 2013 14:33, Andrew Taylor <andyd...@gmail.com> wrote:
> This is what I have settled on (it works!). Are there any reasons why this
> could be bad practice?

It looks 'fit for purpose'. My general principle when writing scripts
is to consider whether I'm going to
reuse the code I write therein. In your case, you could pass in the
filenames you want to consider on
the command line. Writing shell scripts such that you can easily reuse
/ adapt them in future makes
for time & effort saving.
Reply all
Reply to author
Forward
0 new messages