You are not logged in.

#1 2008-11-26 11:01:46

mrbug
Member
Registered: 2007-07-17
Posts: 221

renaming large files

Could someone help me to create a script (or oneliner) that will perform this operation for a directory of files?

mv "first file.ext" "first file(word).exe"

(where the "word" part can vary)


In other words, I need to rename a file to a similarly named file that has another word in parentheses at the end (but before the extension). That word varies depending on when the file was created, so I can't just do something like "mv file.ext file(new).ext", unforunately. These filenames also have spaces.


Thanks in advance for any help!


dvdtube - download all uploads from a YouTube user and then optionally create a DVD.
(Regular version AUR link / SVN version AUR link)

Offline

#2 2008-11-26 11:28:56

robmaloy
Member
From: Germany
Registered: 2008-05-14
Posts: 263

Re: renaming large files

cd into your dir and do the following:

ADD_TEXT="add this text"
EXT=.asdf 
for file in *$EXT; 
do mv "$file" "$(basename "$file" $EXT)${ADD_TEXT}${EXT}"; done

☃ Snowman ☃

Offline

#3 2008-11-26 13:11:03

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

Re: renaming large files

mmv is perfect for this job
mmv '*.*' '#1newtext.#2' would do I believe.

EDIT: forgot the dot in the second argument

Last edited by Procyon (2008-11-26 16:31:08)

Offline

#4 2008-11-26 15:38:18

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: renaming large files

mmv ??
Where do I find that? ('which mmv' yields zilch - and it's not a packet or group)

Offline

#5 2008-11-26 15:58:16

Vladislav IV
Member
Registered: 2008-06-23
Posts: 30

Re: renaming large files

You can find it in AUR.

Offline

#6 2008-11-26 16:19:46

Bebo
Member
From: Göteborg, Sweden
Registered: 2006-06-07
Posts: 207

Re: renaming large files

Check out the rename command too. May not be useful here, though...

Offline

#7 2008-11-26 17:31:21

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: renaming large files

i've fallin in love with rename; i'm not sure i fully understand what you're going for but maybe this'll get you in the right direction

$ ls
file1.exe file2.exe file3.exe
$ rename .exe \(word\).exe *.exe
$ ls
file1(word).exe file2(word).exe file3(word).exe
$

EDIT: now i see what you going for...

as per here:
linux does not *normally* store creation dates/times... there's a myriad of ways to find 'last-modified' type information which could be passed to a variable within your script.

i'd try this, can't test right now so you'll have to tweak i'm sure...

# usage commandname [directory to act on]

if [ -z $1 ];then 
    echo Give target directory; 
    exit 0;
fi

find "$1" -name '*.exe' | while read file ; do
   mod=[command to get your "word" variable] $file
   newname=$(echo "$file" | sed s/.exe/\("$mod"\).exe/g)
   mv $file $newname
   echo ""$file" changed to -> "$newname"" # for debugging
done

exit 0

Last edited by brisbin33 (2008-11-26 17:53:51)

Offline

#8 2008-11-26 22:10:30

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,223
Website

Re: renaming large files

If it doesn't have to be command line based... Have a look at KRename...

Offline

#9 2008-11-28 21:02:20

mrbug
Member
Registered: 2007-07-17
Posts: 221

Re: renaming large files

Ah.. Thanks, everyone. basename and krename were the missing links!


dvdtube - download all uploads from a YouTube user and then optionally create a DVD.
(Regular version AUR link / SVN version AUR link)

Offline

Board footer

Powered by FluxBB