Shell Script for end character rename

Forum rules
Before you post please read this

Shell Script for end character rename

Postby jrskero on 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
jrskero
Level 1
Level 1
 
Posts: 2
Joined: Thu Feb 07, 2013 1:04 pm

Linux Mint is funded by ads and donations.
 

Re: Shell Script for end character rename

Postby Pilosopong Tasyo on 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
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1176
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Shell Script for end character rename

Postby catweazel on 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
KDE 4.12.0, custom preemptive kernel 3.12.5,
Intel i7 4770K @ 4.7GHz, 16GB 2666MHz XMP,
4 Samsung 840 PRO 512GB SSDs in RAID0,
6TB HW RAID10, dual 24" Acer X243H,
Gigabyte nVidia GTX 680 Super Overclock
User avatar
catweazel
Level 7
Level 7
 
Posts: 1656
Joined: Fri Oct 12, 2012 9:44 pm

Re: Shell Script for end character rename

Postby xenopeek on 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
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14982
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Shell Script for end character rename

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

Worked perfect thanks!!
jrskero
Level 1
Level 1
 
Posts: 2
Joined: Thu Feb 07, 2013 1:04 pm


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest