You are not logged in.

#1 2008-12-09 17:01:21

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Bash command question

I volunteered to download my school's entire website, as the current host is going into liquidation and they can't get a copy of the database. So I downloaded the whole thing with wget. Now, the problem is that many (several hundred) of the files have names like "featureDetails.asp?featureID=2", which is fine on a JFS partition, but can't be on a FAT partition.

So, my question is, how do I replace all "?"s, and "="s with "-"?

PS: I really have no idea where to post this, so picked this board at random tongue

Offline

#2 2008-12-09 17:06:09

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Bash command question

If it's just a matter of renaming the files then you can use something line this:

for i in *; do j=$(echo "$i" | sed 's/?/-/g;s/=/-/g'); mv "$i" "$j"; done

This will rename all files in the current directory.
EDIT:
or something like this would work as well:

for i in *; do j=$(echo "$i" | sed 's/[?=]/-/g'); echo "$i" "$j"; done

Last edited by fwojciec (2008-12-09 17:08:52)

Offline

#3 2008-12-09 17:14:13

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: Bash command question

Wonderful, I now have about 1000 files that FAT will be happy with. Thanks.

Offline

#4 2008-12-09 17:45:58

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash command question

I recommend using:

mv --backup=t --

to automatically solve overwrites

EDIT: never mind, there is no real danger of filename conflicts in this situation.

Last edited by Procyon (2008-12-09 23:17:31)

Offline

#5 2008-12-09 23:09:50

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: Bash command question

fwojciec wrote:

If it's just a matter of renaming the files then you can use something line this:

for i in *; do j=$(echo "$i" | sed 's/?/-/g;s/=/-/g'); mv "$i" "$j"; done

This will rename all files in the current directory.
EDIT:
or something like this would work as well:

for i in *; do j=$(echo "$i" | sed 's/[?=]/-/g'); echo "$i" "$j"; done

These certainly work, and for small jobs they are reasonable, but they are both very inefficient because a new process is forked for every file to be renamed.  Instead consider this:

for i in *; do mv "$i" "${i//\?/-/}" ; done

This syntax is documented in the "Parameter Expansion" portion of the bash(1) manpage.

Offline

#6 2008-12-09 23:16:24

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Bash command question

@tam1138  This is cool.  I'm always confused about parameter expansion in bash, for some reason, but now I'll remember this one smile

Offline

#7 2008-12-11 13:26:12

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: Bash command question

man rename

Offline

Board footer

Powered by FluxBB