Using rename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using rename
# 1  
Old 03-22-2014
Using rename

I want to rename files

Files show like this

Code:
1977SSD_rsdtst.pdf
1976SDP_rstdtsr.pdf
1943FDT_rstdsrt.pdf
1996DFF_stdstrd.pdf

I want to introduce _ after the year, and move the characters to the end
to get

Code:
1977_rsdtst_SSD.pdf
1976_rstdtsr_SDP.pdf
1943_rstdsrt_FDT.pdf
1996_stdstrd_DFF.pdf

# 2  
Old 03-22-2014
And what have you tried?
# 3  
Old 03-23-2014
This is purely longhand to show a way of building up each line ready for use.
There are no loops nor the usage of the mv command.
It uses __builtins__ only and is step by step in the code shown/
It is a little tongue-in-cheek but shows the power of the shell.
OSX 10.7.5, default bash terminal.
Code:
#/bin/bash
echo "1977SSD_rsdIUIUYIU2345tst.pdf
1976SDP_rstdtYsr.pdf
1943FDT_rstt.pdf
1996DFF_stdstrd.pdf" > /tmp/text
# Save IFS for later.
ifs_str="$IFS"
IFS="
"
read -d '' -r text < /tmp/text
echo "$text"
# This is purely longhand using builtins; set up an array using newlines only as a separater...
arr=($text)
# First array.
length="${#arr[0]}"
echo "$length"
txt="_${arr[0]:4:3}"
echo "$txt"
rename="${arr[0]:0:4}${arr[0]:7:$[ ( $length - 11 ) ]}$txt${arr[0]:$[ ( $length - 4 ) ]:4}"
echo "$rename"
# Second array.
length="${#arr[1]}"
echo "$length"
txt="_${arr[1]:4:3}"
echo "$txt"
rename="${arr[1]:0:4}${arr[1]:7:$[ ( $length - 11 ) ]}$txt${arr[1]:$[ ( $length - 4 ) ]:4}"
echo "$rename"
# Third array would go here but this IS longhand so now work out any loops to automate it.
# Reset IFS to default...
IFS="$ifs_str"
exit 0

Results...
Code:
Last login: Sun Mar 23 11:23:38 on ttys000
AMIGA:barrywalker~> chmod 755 rename.sh
AMIGA:barrywalker~> ./rename.sh
1977SSD_rsdIUIUYIU2345tst.pdf
1976SDP_rstdtYsr.pdf
1943FDT_rstt.pdf
1996DFF_stdstrd.pdf
29
_SSD
1977_rsdIUIUYIU2345tst_SSD.pdf
20
_SDP
1976_rstdtYsr_SDP.pdf
AMIGA:barrywalker~> _

# 4  
Old 03-23-2014
Code:
for i in $(ls *.pdf)
do
bname="${i%.*}"
ext="${i##*.}"
Y=$(cut -c1-4 <<<${bname})
X=$(cut -f1 -d "_" <<<${bname} | cut -c5-)
Z=$(cut -f2 -d "_" <<<${bname})
j="${Y}_${Z}_${X}.${ext}"
mv ${i} ${j}
done

# 5  
Old 03-23-2014
Ahem...

Quote:
Originally Posted by SriniShoo
Code:
Y=$(cut -c1-4 <<<${bname})
X=$(cut -f1 -d "_" <<<${bname} | cut -c5-)
Z=$(cut -f2 -d "_" <<<${bname})

This might work in bash (i do not know bash all too well), but definitely not in ksh. I don't believe a POSIX-shell would fathom it.

Quote:
Originally Posted by SriniShoo
Code:
for i in $(ls *.pdf)
do
bname="${i%.*}"
ext="${i##*.}"
Y=$(cut -c1-4 <<<${bname})
X=$(cut -f1 -d "_" <<<${bname} | cut -c5-)
Z=$(cut -f2 -d "_" <<<${bname})
j="${Y}_${Z}_${X}.${ext}"
mv ${i} ${j}
done

You use 4(!) fork()s to manipulate a simple filename, which is a waste of resources. Much better: split the filename using variable expansion like you already did, but instead of splitting into name and extension (which you do not need) split into the first 4 characters and the remainder. Then simply add the additional underscore. I am sure you can work out how. (but please do not post it here until the threads o/p has responded)

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using rename

Renaming files using rename on the following files rename 's/.99999/.99999.sac.pzs/g' *.99999 sac_pzs_iv_esml_hhz__2013.074.10.18.23.0000_2599.365.23.59.59.99999 sac_pzs_iv_favr_hhe__2010.187.00.00.00.0000_2599.365.23.59.59.99999... (1 Reply)
Discussion started by: kristinu
1 Replies

2. Shell Programming and Scripting

How to use function 'rename' ?

hello, all I have following files: file_1 file_2 file_3 ... file_9 Now I want to rename them as: file_001 file_002 file_003 ... file_009 how to use function 'rename' to accomplish this task? (2 Replies)
Discussion started by: littlewenwen
2 Replies

3. UNIX for Dummies Questions & Answers

Rename a file

how to rename a file if I don't know file location ? Can we below command : $ find / | mv file1.txt I am not sure........can any1 help me out ? Thanks, Tushar Joshi:mad: (1 Reply)
Discussion started by: tusharjoshi
1 Replies

4. Shell Programming and Scripting

Rename files

Hi, I am new to Unix and i have a requirement where i need to write a shell script where i have to loop through various files present in a directory and rename them based on below criteria. Files in the folder are in the following format. _YYYYMMDD.dat] SDL_V1_20100530.dat... (6 Replies)
Discussion started by: bishoo
6 Replies

5. UNIX for Dummies Questions & Answers

need help with rename

hi guys i am writing a script to change the filename which is enterered as input to lower case letter even if one letter is upper case i have to change it to lower case i get the input and use sed comand should i use like that sed/s/a-z/A-Z/d will it be like that can u please help me (8 Replies)
Discussion started by: farhan_t49
8 Replies

6. Shell Programming and Scripting

rename

hi, im doin an assignment which requires you to build a shell script to do the MS DOS style equivilant (sp?) of the rename function. What i have to do is a loop which checks the following rules and output messages (same): Can't have anything after target Can't have more than one dot Can't... (1 Reply)
Discussion started by: bohoo
1 Replies

7. UNIX for Dummies Questions & Answers

rename files help

Hi, I've already search for this issue but I found different scripts for rename files, but I don't know how to customize it for my needs. Here's what I want to do: I have a lot of files inside many directories, like this: /aa/01.txt /aa/02.txt /ab/01.txt /ab/02.txt I want all those files... (2 Replies)
Discussion started by: piltrafa
2 Replies

8. Shell Programming and Scripting

rename while doing ftp

hi friends, I writing a script to psot dataset to ceratin server.....while doing ftp I have to rename the file while which is in present server like present :test server filename data.097878. ftp server: it sholu be renamed as data.097878.zip thanks, The thing is that while... (1 Reply)
Discussion started by: Nagabhushan
1 Replies

9. UNIX for Dummies Questions & Answers

how can I rename the following=-^

I have a file named -^, I want to look at it, rename, etc. Any help out there?? (5 Replies)
Discussion started by: nj78
5 Replies

10. UNIX for Dummies Questions & Answers

rename using mv ?

Hi all how can can remove the underscore from this number in this series _1234567.abc _1234567.abcd I was trying mv _1234567* 1234567 but did not work ? thanks s (2 Replies)
Discussion started by: simon2000
2 Replies
Login or Register to Ask a Question