Rename file-name minus one?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Rename file-name minus one?
# 1  
Old 03-17-2010
[Solved]Rename file-name minus one?

I need to rename a directory full of files named like:

Code:
page_001.jpg
page_002.jpg
page_003.jpg

to the file name minus one. Meaning instead of page_001.jpg that file becomes page_000.jpg so:

Code:
page_001.jpg => page_000.jpg
page_002.jpg => page_001.jpg
page_003.jpg => page_002.jpg

I was thinking like, but i don't know how to write it so it actually works:
Code:
rename page_%03d.jpg ???????????-1.jpg   *.jpg

or will this work?

for i in page_[0-9][0-9][0-9].jpg
do
mv $i ${i%-1.jpg}.jpg
done


Last edited by RacerX; 03-18-2010 at 09:26 AM.. Reason: solved
# 2  
Old 03-17-2010
Check the output first without the coloured part:
Code:
ls page_*.jpg | 
awk -F"[_.]" '{
s=$0
$0=$1"_"sprintf("%03d",int($2)-1)"."$3}
{print "mv " s " " $0}' | sh

# 3  
Old 03-18-2010
It almost works after a bit of debugging: a missing semi-colon after s=$0, but it errors out because the path has underscores in it too! The updated code i am using in php:
Code:
$renamecmd = `ls $mydir/page_*.jpg | awk -F"[_.]" '{ s=$0; $0=$1"_"sprintf("%03d",int($2)-1)"."$3} {print "mv " s " " $0}' | sh`;

Results in the error:
Code:
mv: cannot move `/home/usr1/public_html/cards/my_programs/h083de63/NFLD/NFLD199007300730PM/pages/page_001.jpg' to `/home/usr1/public_-01.programs/h083de63/NFLD/NFLD199007300730PM/pages/page': No such file or directory

Anyway to fix this?

---------- Post updated 03-18-10 at 08:33 AM ---------- Previous update was 03-17-10 at 10:52 AM ----------

Solved the underscore in path problem by sending a change directory command before the solution posted by Franklin52. Hope it helps somebody else avoid wasting a whole work day trying to figure out how to rename ordered files:

Code:
$renamecmd = `cd $mydir; ls page_*.jpg | awk -F"[_.]" '{ s=$0; $0=$1"_"sprintf("%03d",int($2)-1)"."$3} {print "mv " s " " $0}' | sh`;


Last edited by RacerX; 03-17-2010 at 06:23 PM.. Reason: Updated after debugging
# 4  
Old 03-18-2010
Quote:
Originally Posted by RacerX
It almost works after a bit of debugging: a missing semi-colon after s=$0
That's not a bug. It's is the behaviour of awk, if you have more commands on one line, they must be separated by a semicolon.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to minus one day from a date given

Hello Folks, I have a variable output holding date as below - output = "20141220" I need to extract a day out of it and store it in another variable i.e. something similar to below - output1=20141219" and if the month is changing i.e. date in on 31st or 1st it should be taken care of "date... (5 Replies)
Discussion started by: ektubbe
5 Replies

2. Shell Programming and Scripting

How to get a time minus 60 minutes?

Hello, date --date '-60 min ago' +'%Y-%m-%d %H:%M:%S,%3N' Above command gives the date and time minus 60 minutes but the problem i am facing is, i do not want to hardcode the value 60 it is stored in a variable var=60 now if i run below command , i get error date --date '-$var min... (3 Replies)
Discussion started by: Ramneekgupta91
3 Replies

3. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Date minus 1 day

Hi the below code is failing i am trying to generate do the following: 2014-10-22 11:26:00 (Substract 24 hrs) should produce 2014-10-21 11:26:00 I need the same formatting below because that gets inputted into code for an... (4 Replies)
Discussion started by: Samuel12
4 Replies

5. UNIX for Dummies Questions & Answers

Minus 5 minutes

Hi, I need to subtract 5 minutes from the date. Example $date +"%Y-%m-%d-%H.%M.%S" displays 2014-06-26-06.06.38 I want to show it as 2014-06-26-06.01.38 (5 mins are subtracted from date) Any help would be appreciated. I am currently on AIX version 6.1 -Vrushank (10 Replies)
Discussion started by: vrupatel
10 Replies

6. Shell Programming and Scripting

Minus minus to plus

Hi there, I have a problem with arithmetic ops in awk. Here is what my script does right now. while read nr val ; do case $nr in 400) awk '$2~/eigenvectors/ {print $NF-'$val'};' input.txt >> output.txt;; esac done < frames.txtI have a file named frames.txt with two columns (nr and... (2 Replies)
Discussion started by: tobias1234
2 Replies

7. Shell Programming and Scripting

Minus of files

File 1 contains data : CALL_ID SOR_ID SEG_SEQ_NUM CHK_PNT_MENU_TYPE_CD PTNR_ID ACTVN_RTRN_CD PRIM_ACTVN_DCLN_REAS_CD SCNDRY_ACTVN_DCLN_REAS_CD ACTVN_SCCS_IND CARD_ACTVTD_IND MAX_ACTVN_FAILD_ATMP_IND EDW_PUBLN_ID File 2 contains data: PRIM_ACTVN_DCLN_REAS_CD... (3 Replies)
Discussion started by: ysvsr1
3 Replies

8. Shell Programming and Scripting

Minus of 2 files -- Please help

Hello people, awk '{print $0}' input1.txt input2.txt |sort -u Will give the union of the 2 files. Similarly what is command to get just the diff data (i.e minus) of the 2 files. Can I use the "diff" command to get just the diff data. Please let me know. Regards, Tipsy. (7 Replies)
Discussion started by: tipsy
7 Replies

9. UNIX for Dummies Questions & Answers

minus col1 and col2

my file looks like this: 101928 101943 101928 101944 101929 101943 101929 101943 101929 102044 i want to insert bc to get answer like this: 101928 101943 000015 101928 101944 000016 101929 101943 000013 101929 101943 000014 101929 102044 000115 total 000173 my... (3 Replies)
Discussion started by: tjmannonline
3 Replies

10. UNIX for Dummies Questions & Answers

minus sign

why a minus sign is put for options in unix commands suggestions plz (2 Replies)
Discussion started by: trichyselva
2 Replies
Login or Register to Ask a Question