Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Renaming groups of files from the command prompt
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
helmers
Guru
Guru


Joined: 16 Sep 2002
Posts: 553
Location: Stange, Norway

PostPosted: Wed Oct 23, 2002 12:36 pm    Post subject: Renaming groups of files from the command prompt Reply with quote

One of the features don't know how to use, which I used a lot in Windows, is renaming files like this: "move *.wav *.mp3", and it would automagically understand it. Somehow I can't figure out how to do this :wink:


--
Regards,
Jan Henrik Helmers
Back to top
View user's profile Send private message
dippen
Tux's lil' helper
Tux's lil' helper


Joined: 15 Sep 2002
Posts: 83

PostPosted: Wed Oct 23, 2002 12:41 pm    Post subject: Reply with quote

its allmost the same
Code:
mv *wav *mp3

(without dots)

look at
Code:
man mv
for more information
Back to top
View user's profile Send private message
helmers
Guru
Guru


Joined: 16 Sep 2002
Posts: 553
Location: Stange, Norway

PostPosted: Wed Oct 23, 2002 12:47 pm    Post subject: Reply with quote

Okay, to make it a bit tougher, what if the files contain these letters? Any more "solid" way? :roll:

--
Regards,
Helmers
Back to top
View user's profile Send private message
progster
Apprentice
Apprentice


Joined: 16 Jul 2002
Posts: 271

PostPosted: Wed Oct 23, 2002 1:05 pm    Post subject: Reply with quote

then dippens way will work as well since manwavis does not equal *wav ;-)

~Progster
Back to top
View user's profile Send private message
helmers
Guru
Guru


Joined: 16 Sep 2002
Posts: 553
Location: Stange, Norway

PostPosted: Wed Oct 23, 2002 2:39 pm    Post subject: Reply with quote

Great! Feel a bit dumb though, this have been bothering me for over a year. Guess you don't have to be smart to use 'nix ;)

Thanks a lot for helping out!


--
Regards,
Helmers
Back to top
View user's profile Send private message
n0n
Guru
Guru


Joined: 13 Jun 2002
Posts: 355

PostPosted: Wed Oct 23, 2002 4:03 pm    Post subject: Reply with quote

dippen wrote:
its allmost the same
Code:
mv *wav *mp3

(without dots)


Um, this shouldn't actually work. And it certainly doesn't for me. When I need to rename files like that, I typically do:
Code:
for file in *.wav; do mv $file `basename $file .wav`.mp3; done

Note that's bash syntax, other shells' mileage may vary.

Also, I'm sure you're probably aware of this, but simply changing the extension of a .wav file to .mp3 will not create an mp3 file for you. You'll need to invoke an actual mp3 encoder to create an mp3 file.
Back to top
View user's profile Send private message
phong
Bodhisattva
Bodhisattva


Joined: 16 Jul 2002
Posts: 778
Location: Michigan - 15 & Ryan

PostPosted: Wed Oct 23, 2002 6:56 pm    Post subject: Reply with quote

The fundamental difference here is that in IIRC dos/windows the parameters are passed to programs more or less as typed. In *nix they are expanded by the shell BEFORE passing. In this case, if you had 3 wav files in the current directory (a.wav, b.wav and c.wav), the program would recieve the exact list of wav files as its parameters. Since there are no .mp3 files, that part gets expanded to nothing. The rule for mv is if you're moving multiple files, the last parameter (the target) must be a directory, since it makes no sense to move lots of files to one target filename. In windows/dos the wildcards are interpreted by the ren command so it can say "Oh, they said *.wav and *.mp3, they want to change everything that has an extension of .wav to be .mp3 instead".

The tradeoff is that in *nix you can do the "for" loop on the command line as mentioned by n0n.
_________________
"An empty head is not really empty; it is stuffed with rubbish. Hence the difficulty of forcing anything into an empty head."
-- Eric Hoffer
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20067

PostPosted: Wed Oct 23, 2002 9:37 pm    Post subject: Reply with quote

phong wrote:
they want to change everything that has an extension of .wav to be .mp3 instead".
Quote:
in *nix you can do the "for" loop on the command line
Those two don't necessarily seem like they should be mutually exclusive.
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
n0n
Guru
Guru


Joined: 13 Jun 2002
Posts: 355

PostPosted: Wed Oct 23, 2002 9:58 pm    Post subject: Reply with quote

kanuslupus wrote:
Those two don't necessarily seem like they should be mutually exclusive.


Sure, not necessarily, but the behavior of the "mv" command has been very well defined. If you run this from the command line:
Code:
mv "*.wav" "*.mp3"
mv will attempt to move the file named literally "*.wav" to a new file named (literally) "*.mp3." Expanding based off of a GLOB, at least in the UNIX world, belongs to the shell, and as such, the mv command can't be expected to do it's own expansions. Now, would it be too difficult to make your own "mv" that understands these things? Here, let's call it "bmv" for "bulk mv":
Code:
#!/bin/bash
# Run as: bmv wav mp3
foreach file in *.$1
do
  mv $file `basename $file $1`.$2
done

I didn't test that out at all, but it should basically be fine. Save it in your path and you're good to go . . . But the "mv" command itself shouldn't be responsible for those expansions.
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20067

PostPosted: Wed Oct 23, 2002 10:27 pm    Post subject: Reply with quote

n0n wrote:
Expanding based off of a GLOB, at least in the UNIX world, belongs to the shell
I hadn't considered that... good point.

Quote:
I didn't test that out at all, but it should basically be fine.
I don't think it will handle spaces in file names, but I haven't tested it either.
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
gauched
n00b
n00b


Joined: 12 Jul 2002
Posts: 6
Location: SLC, Utah

PostPosted: Fri Oct 25, 2002 5:20 pm    Post subject: Reply with quote

Globbing can have strange effects for someone who is more familiar with the dos world. I had alot of problems with accidently moving all of my /etc/* files into /etc/X11 because I expected a doslike behavior.
To address the original question, "emerge rename" should help you do what you want to do.
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20067

PostPosted: Fri Oct 25, 2002 5:36 pm    Post subject: Reply with quote

A brief explanation of rename, as it is not initially obvious. See man for other nifty things you can do.

rename files with extension of .dat to .data (including filenames with spaces).
rename <from> <to> <files to change>
rename .dat .data *.dat
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
ryan83vt
Guru
Guru


Joined: 28 Oct 2002
Posts: 370
Location: Blacksburg, VA

PostPosted: Thu Jan 30, 2003 8:30 pm    Post subject: Reply with quote

kanuslupus wrote:
A brief explanation of rename, as it is not initially obvious. See man for other nifty things you can do.

rename files with extension of .dat to .data (including filenames with spaces).
rename <from> <to> <files to change>
rename .dat .data *.dat


can I do the same with some variant of the copy command?
Back to top
View user's profile Send private message
Donovan
Tux's lil' helper
Tux's lil' helper


Joined: 08 Feb 2003
Posts: 97
Location: Halifax, NS, Canada

PostPosted: Sat Jun 14, 2003 4:51 pm    Post subject: Reply with quote

'rename' is no longer a separate package in portage. However, it is installed on my system, it must be included in some other package, perhaps baseutils or something.

If you really want to mass rename, check out krename... The version in portage is outdated at moment, I filed a report - see https://bugs.gentoo.org/show_bug.cgi?id=22826 to emerge the newest version as of this writing...

I would say there is no limit to what you can do with that krename... you can extract attributes from the files (photo details, mp3 details) and add them as part of the file name!
_________________
Gentoo Linux * DirecTivo * Who needs heaven?
Back to top
View user's profile Send private message
far
Guru
Guru


Joined: 10 Mar 2003
Posts: 394
Location: Stockholm, Sweden

PostPosted: Sat Jun 14, 2003 5:01 pm    Post subject: Reply with quote

Donovan wrote:
'rename' is no longer a separate package in portage. However, it is installed on my system, it must be included in some other package, perhaps baseutils or something.

Code:
prompt> qpkg -f `which rename`
sys-apps/util-linux *

_________________
The Porthole Portage Frontend
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum