Author Topic: [SOLVED] Removing last character of filename (Batch)  (Read 2969 times)

0 Members and 1 Guest are viewing this topic.

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1620
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
[SOLVED] Removing last character of filename (Batch)
« on: December 31, 2012, 05:03:59 pm »
I have a load of video files that I have converted and the program that did it has renamed them with a '1' at the end, e.g. Batman1.avi
What I now need to do, with a LOT of files is remove the '1' and leave the rest of the name intact. I have had a go with Krusader/Krename but don't know how to do it properly with either of them, if in fact it is possible with either of them, I have had a look through the instructions but donít know if I have done it right?
« Last Edit: January 05, 2013, 10:37:48 pm by pooky2483 »

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-68-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline Mad Penguin

  • #Mad_Penguin_UK
  • Administrator
  • Hero Member
  • *****
  • Posts: 1320
  • Karma: 10017
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Removing last character of filename (Batch)
« Reply #1 on: December 31, 2012, 06:32:42 pm »
How about something like this;

Code: [Select]
for i in `ls`
do
 dst=`echo $i | sed -e "/1\./s//\./"`
 echo "mv -v $i $dst"
done
Caveat; untested and as-is, will just print the rename commands rather then execute ..
(remove the echo and quotes from the "mv" line to activate)
- Assumes regular filenames with only one "." in them ... (!)

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1620
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Removing last character of filename (Batch)
« Reply #2 on: January 01, 2013, 02:28:19 am »
I'm not that good on using terminal.
Do I enter each line in terminal?
If not, how do I do it?

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-68-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline SeZo

  • Hero Member
  • *****
  • Posts: 1524
  • Karma: 120
  • Gender: Male
    • View Profile
    • Awards
Re: Removing last character of filename (Batch)
« Reply #3 on: January 04, 2013, 12:35:44 am »
How about a one-liner (assuming the '1.avi' is constant and all files are in the same folder) :
Code: [Select]
rename -v 1.avi .avi *1.avi
or non verbose:
Code: [Select]
rename 1.avi .avi *1.avi
Just make sure to change into the directory first ;)

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1620
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Removing last character of filename (Batch)
« Reply #4 on: January 05, 2013, 03:51:05 pm »
How about a one-liner (assuming the '1.avi' is constant and all files are in the same folder) :
Code: [Select]
rename -v 1.avi .avi *1.avi

I tried it and got;
pooky2483@pooky2483-ubuntu12:/media/Drive3/An Idiot Abroad/Season 01$ rename -v 1.avi .avi *1.avi
Bareword found where operator expected at (eval 1) line 1, near "1.avi"
   (Missing operator before avi?)
syntax error at (eval 1) line 2, near "1.avi
"
pooky2483@pooky2483-ubuntu12:/media/Drive3/An Idiot Abroad/Season 01$

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-68-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline SeZo

  • Hero Member
  • *****
  • Posts: 1524
  • Karma: 120
  • Gender: Male
    • View Profile
    • Awards
Re: Removing last character of filename (Batch)
« Reply #5 on: January 05, 2013, 04:04:52 pm »
Hmm, that worked on Arch :)
It appears that Debian based distros implement the rename command differently, try this (untested not on Debian at the minute)
Code: [Select]
rename -v 's/1.avi/.avi/' *1.avi
« Last Edit: January 05, 2013, 04:51:55 pm by SeZo »

Offline Mad Penguin

  • #Mad_Penguin_UK
  • Administrator
  • Hero Member
  • *****
  • Posts: 1320
  • Karma: 10017
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Removing last character of filename (Batch)
« Reply #6 on: January 05, 2013, 04:59:13 pm »
Just to be pedantic, the Unix/Linux command for renaming files is "mv" rather than "rename" ... :-)

Sent from my rk30sdk using Tapatalk HD

Offline SeZo

  • Hero Member
  • *****
  • Posts: 1524
  • Karma: 120
  • Gender: Male
    • View Profile
    • Awards
Re: Removing last character of filename (Batch)
« Reply #7 on: January 05, 2013, 05:24:28 pm »
Quote
Just to be pedantic, the Unix/Linux command for renaming files is "mv" rather than "rename" ... :-)

OK, the mv version (slightly longer) would be:
Code: [Select]
for f in *1.avi; do mv "$f" "${f/1.avi/.avi}"; done
« Last Edit: January 05, 2013, 05:37:10 pm by SeZo »

Offline Mad Penguin

  • #Mad_Penguin_UK
  • Administrator
  • Hero Member
  • *****
  • Posts: 1320
  • Karma: 10017
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Removing last character of filename (Batch)
« Reply #8 on: January 05, 2013, 05:37:55 pm »
Sure, you can always condense stuff, indeed there was a Perl 'fad' (many years ago now) of seeing how much you could get on one line, or indeed writing an entire (complex) program on one line .. this sort of died out once people figured out that the next day they couldn't make head nor tail of what they'd just written .. :)

I was just pointing out that 'rename' isn't a standard command, rather it's a Perl script - Perl being the root of all evil in the scripting world and to be avoided at any cost ... ;-)

I must admit I'd not seen the inline pattern matching before (see my more verbose 'sed' example above) , is this standard in all shells, or specific to bash and / or dash ???


Sent from my rk30sdk using Tapatalk HD

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1620
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Removing last character of filename (Batch)
« Reply #9 on: January 05, 2013, 06:51:10 pm »

OK, the mv version (slightly longer) would be:
Code: [Select]
for f in *1.avi; do mv "$f" "${f/1.avi/.avi}"; done

'Fraid nothing happened!

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-68-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline SeZo

  • Hero Member
  • *****
  • Posts: 1524
  • Karma: 120
  • Gender: Male
    • View Profile
    • Awards
Re: Removing last character of filename (Batch)
« Reply #10 on: January 05, 2013, 08:00:08 pm »
Quote
'Fraid nothing happened!

That command is not verbose (would not print to screen)
Did you cd into the directory?
Also did you try the rename command?
« Last Edit: January 05, 2013, 08:07:43 pm by SeZo »

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1620
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Removing last character of filename (Batch)
« Reply #11 on: January 05, 2013, 08:20:15 pm »
Sorry, it DID work.
I had to COMPLETELY close 'window' - nautilus 'Drive3/An_Idiot_Abroad/Season_01/filename.avi'
when I just collapsed the window and re-opened it, there was no change visible?
Any idea why that is?

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-68-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline SeZo

  • Hero Member
  • *****
  • Posts: 1524
  • Karma: 120
  • Gender: Male
    • View Profile
    • Awards
Re: Removing last character of filename (Batch)
« Reply #12 on: January 05, 2013, 08:26:12 pm »
Quote
Any idea why that is?

Nautilus (and other file managers) scan for changes in set intervals, not constantly, and eating up resources in the process. :D
All you had to do is click Refresh ;)

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1620
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Removing last character of filename (Batch)
« Reply #13 on: January 05, 2013, 08:43:05 pm »
I'm seriously going to have to get a book on terminal commands

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-68-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 13978
  • Karma: 347
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: Removing last character of filename (Batch)
« Reply #14 on: January 06, 2013, 12:41:23 am »
Sent from my rk30sdk using Tapatalk HD

I thought you had a Motorola Xoom ?

Are you going to do a mini review of the rk30sdk ?
WARNING: You are logged into reality as 'root'

logging in as 'insane' is the only safe option.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal