You are not logged in.

#1 2013-07-12 15:55:20

Lockheed
Member
Registered: 2010-03-16
Posts: 1,521

[solved] Bulk file rename

I've been googling since few hours, but I found nothing what I could use.

I need to remove last 12 characters from many files at one, and because those files are huge, it cannot be copy, but just rename action (and 'sed' seems to be copying it before renaming).

How could I do that?

Last edited by Lockheed (2013-07-13 17:38:26)

Offline

#2 2013-07-12 15:59:05

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,606

Re: [solved] Bulk file rename

Are the last 12 characters the same for each file? If so, the rename command is a simple way to do it. If they're not the same, a script could get the last 12 characters and then use rename.

Online

#3 2013-07-12 16:00:25

Lockheed
Member
Registered: 2010-03-16
Posts: 1,521

Re: [solved] Bulk file rename

No, they are different for every file and can be either letters or digits. I found a great tool called ReNamer which can do it in a blink of an eye. They only drawback is it's for Windows and I have to use it inside virtual machine.

Last edited by Lockheed (2013-07-12 16:01:26)

Offline

#4 2013-07-12 16:06:50

HalosGhost
Forum Moderator
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 2,095
Website

Re: [solved] Bulk file rename

How different are the characters? If they follow a fairly obvious pattern, you could use a for loop coupled with `rename` or `mv` to do this pretty effectively.

All the best,

-HG

Last edited by HalosGhost (2013-07-12 16:09:18)

Offline

#5 2013-07-12 16:10:10

args
Member
Registered: 2012-03-06
Posts: 9

Re: [solved] Bulk file rename

you can write a shell script to do this

for f in *
do
    mv $f ${f:0:-10}
done

Apple hater. Panda lover.

Offline

#6 2013-07-12 16:13:44

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [solved] Bulk file rename

Use "vidir" from the package "moreutils". It allows you to change file names by using Vim commands.

For your example, I would:

qa (start recording a macro called "a")
$ (go to the end of the line)
11h (move the cursor to the twelth character to the left)
12x (delete 12 characters to the right)
0 (go to the beginning of the line)
j (go to the next line)
q (stop recording a macro)
1000@a (run the macro called "a" on the next 1000 lines)

...I'm sure someone has a much more elegant way of doing it, but hey, there's a solution, and it would have taken me about 20 seconds.

Offline

#7 2013-07-12 16:21:16

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Bulk file rename

args' script works, but
* OP wanted to remove last 12 characters, not 10 :-)
* all files have to be of valid length, or else you get

./<scriptname>: line 5: -10: substring expression < 0

(filename was too short).

Last edited by karol (2013-07-12 16:22:02)

Offline

#8 2013-07-12 16:58:56

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: [solved] Bulk file rename

Give metamorphose2 a try. It is in the AUR.

Offline

#9 2013-07-12 17:17:06

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [solved] Bulk file rename

Wait, how come nobody has mentioned perl-rename?

@args, dude quote your $shit or it's gonna leak.

Last edited by lolilolicon (2013-07-12 17:18:44)


This silver ladybug at line 28...

Offline

#10 2013-07-12 17:27:22

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [solved] Bulk file rename

lolilolicon wrote:

Wait, how come nobody has mentioned perl-rename?

I want to be cheeky and say "because it's Perl", but I was the person that recommended using Vim... hmm

EDIT: More specifically, crazy Perl regular expressions...

EDIT EDIT: I think nobody has mentioned it because nobody can figure out how to use it. What command would you use to fix the OP's problem?

Last edited by drcouzelis (2013-07-12 17:28:48)

Offline

#11 2013-07-12 17:34:46

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [solved] Bulk file rename

There must some trap here... but I'll risk being a fool now...

perl-rename 's/.{12}$//' *

This silver ladybug at line 28...

Offline

#12 2013-07-12 17:56:30

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [solved] Bulk file rename

I just realized a very nice and somewhat unique feature to vidir: You can see what the "results" will be before committing to the renaming.

Offline

#13 2013-07-12 18:04:30

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [solved] Bulk file rename

perl-rename can dry run and show it to you if you give it -n, too.


This silver ladybug at line 28...

Offline

#14 2013-07-12 18:32:59

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [solved] Bulk file rename

lolilolicon wrote:

perl-rename can dry run and show it to you if you give it -n, too.

Very cool! I knew this thread was going to be a learning experience for me. big_smile

Offline

#15 2013-07-12 18:37:31

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [solved] Bulk file rename

Thunar has a built in bulk renamer, but I've never used it.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#16 2013-07-12 21:21:00

stewie
Member
Registered: 2009-05-16
Posts: 20

Re: [solved] Bulk file rename

You might want to check if there will be any dupes after renaming the files.

$ ls
abc123  abc456  abc789

$ perl-rename -n 's/.{3}$//' *
abc123 -> abc
abc456 -> abc
abc789 -> abc

Without the dry run

$ perl-rename 's/.{3}$//' *

$ ls
abc

Edit: added pics

Thunar's bulk rename will not let you make any changes if you would end up with some dupes(afaict). The rename button is greyed out.
pic of greyed out button http://i.imgur.com/yJS5Alt.jpg

If you didn't put all the files into bulk rename in your current dir and you would end up with some dupes after renaming, bulk rename will give you an error message.
pic of failed to rename http://i.imgur.com/pbBLNjD.jpg

Last edited by stewie (2013-07-12 22:01:43)


We wants it, we needs it. Must have the precious. Arch Linux.

Offline

#17 2013-07-12 21:46:27

dag
Member
From: US
Registered: 2013-01-20
Posts: 216

Re: [solved] Bulk file rename

there is pyrenamer which has extensive options which include delete number of characters of file

Last edited by dag (2013-07-12 21:53:46)


--------------------------------------
alcoves wonder creates the wonder unto the ages; never lose that.

Offline

#18 2013-07-12 22:00:57

Gnarl
Member
Registered: 2010-11-18
Posts: 63

Re: [solved] Bulk file rename

For a lazy GUI try gprename

Offline

#19 2013-07-13 03:23:29

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [solved] Bulk file rename

@stewie, good point wrt dupes after rename. One could avoid it by using the '-i' option so it prompts before overwriting, or by making backups, e.g.

perl-rename -b -V numbered 's/.{12}$//' *

This silver ladybug at line 28...

Offline

#20 2013-07-13 08:43:02

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: [solved] Bulk file rename

Wow almost every single person has responded with a different program. Maybe there should be "Which file renamer do you use thread" tongue

Last edited by x33a (2013-07-13 08:43:15)

Offline

#21 2013-07-13 09:38:38

Lockheed
Member
Registered: 2010-03-16
Posts: 1,521

Re: [solved] Bulk file rename

I learnt a lot thanks to your suggestions.
In the end, I found metamorphose2 recommended by x33a as the most versatile and convenient to me solution, which does exactly what I wanted.
Thanks, guys!

Offline

#22 2013-07-13 17:34:26

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Bulk file rename

There're also renameutils with their qmv that works similarly to vidir https://bbs.archlinux.org/viewtopic.php … 0#p1299390



Lockheed, please remember to mark the thread as solved https://bbs.archlinux.org/viewtopic.php?id=130309

Offline

Board footer

Powered by FluxBB