Shell Script for end character rename

Forum rules
Before you post please read how to get help
jrskero
Level 1
Level 1
Posts: 2
Joined: Thu Feb 07, 2013 1:04 pm

Shell Script for end character rename

Postby jrskero » Thu Feb 07, 2013 1:08 pm

I would like to rename a1b2c3b4pp987.txt to only use the last 5 charcters plus extension pp987.txt

User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1360
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Shell Script for end character rename

Postby Pilosopong Tasyo » Thu Feb 07, 2013 11:41 pm

jrskero wrote:I would like to rename a1b2c3b4pp987.txt to only use the last 5 charcters plus extension pp987.txt

Refer here for some clues.

<off topic>
Why do I have the feeling this is a homework question? :shock:
I'm just sayin'
</off topic>
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].

User avatar
catweazel
Level 7
Level 7
Posts: 1658
Joined: Fri Oct 12, 2012 9:44 pm

Re: Shell Script for end character rename

Postby catweazel » Fri Feb 08, 2013 1:36 am

jrskero wrote:I would like to rename a1b2c3b4pp987.txt to only use the last 5 charcters plus extension pp987.txt


Code: Select all

#!/bin/bash
mv a1b2c3b4pp987.txt pp987.txt
ls


HTH
Mint Testing Team & Mint Donor #3606
Intel i7 4820K @ 4.7GHz, 32GB Corsair Vengeance Pro
2133MHz XMP, 4 Samsung 850 PRO 512GB SSDs in
RAID0, 12TB HW RAID10, 3 x 24" Samsung UlHD,
Custom pre-emptive kernel 4.7.5,
Dual nVidia GTX 1080

User avatar
xenopeek
Level 23
Level 23
Posts: 19377
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Shell Script for end character rename

Postby xenopeek » Sat Feb 09, 2013 4:52 am

Making that a bit more flexible (note this is hacked together and only works for files in your current directory that have a three letter extension):

Code: Select all

#!/bin/bash
file="a1b2c3b4pp987.txt"
mv $file ${file: -9}

Or more flexible, letting you run the command with the name of file you want to rename as an argument (so "./scriptname a1b2c3b4pp987.txt"):

Code: Select all

#!/bin/bash
mv $1 ${1: -9}

Or if you want to do that for all .txt files in the current directory, just run the following one command :wink:

Code: Select all

for file in *.txt; do mv $file ${file: -9}; done
Image

jrskero
Level 1
Level 1
Posts: 2
Joined: Thu Feb 07, 2013 1:04 pm

Re: Shell Script for end character rename

Postby jrskero » Thu Feb 14, 2013 3:09 pm

Worked perfect thanks!!


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 2 guests