Script for rename many files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for rename many files
# 1  
Old 04-13-2016
Script for rename many files

Hello friends!
I have a problem with my script. I'm a italian boy. Sorry for my english ehehehehehhe.

I've many files .jpg and I would like rename they in this mode:

I have not files with progressive number e I would like rename with progressive number. Example:

DSC_0012.JPG
DSC_0582.JPG
DSC_0001.JPG
DSC_0016.JPG
DSC_0092.JPG
.....etc.

rename in:
DSC_0001.JPG
DSC_0002.JPG
DSC_0003.JPG
DSC_0004.JPG
DSC_0005.JPG
.....etc.




This is my script but don't work correctly:

Code:
#!/bin/bash

echo Insert the number for start;
read n;

for i in `ls *.JPG`;

do mv $i DSC_0$n.JPG;
n=$(($n+1));
done

Thanks!
# 2  
Old 04-13-2016
try:
Code:
#!/bin/bash

echo Insert the number for start;
read n;

for i in *.JPG;
do
   mv $i DSC_$(printf "%04d" $n).JPG;
   n=$(($n+1));
done

# 3  
Old 04-13-2016
WHAT does not work correctly?
# 4  
Old 04-13-2016
Quote:
Originally Posted by rdrtx1
try:
Code:
#!/bin/bash

echo Insert the number for start;
read n;

for i in *.JPG;
do
   mv $i DSC_$(printf "%04d" $n).JPG;
   n=$(($n+1));
done

Note that if the starting number read into n is not 1, there is a chance that one or more of the original files will be copied into multiple renamed files and each time one of the original files is duplicated another original file will be destroyed. For instance, if the directory originally contained:
Code:
DSC_0002.JPG
DSC_0003.JPG
DSC_0004.JPG

and you entered 3 as the starting value, this script would end up with the files:
Code:
DSC_0003.JPG
DSC_0004.JPG
DSC_0005.JPG

all of which contained what was originally stored in the file named DSC_0002.JPG and the images that had been stored in DSC_0003.JPG and DSC_0004.JPG are lost (unless you made a backup copy of your files before running this script).

Note that if the starting number is 1 or if the starting number is greater than the highest number used in any existing file added to the number of files matching the pattern *.JPG when the script starts, you will be safe as long as all of the input files in the directory that match the pattern *.JPG also match the pattern DSC_[0-9][0-9][0-9][0-9].JPG.
# 5  
Old 04-15-2016
Quote:
Originally Posted by rdrtx1
try:
Code:
#!/bin/bash

echo Insert the number for start;
read n;

for i in *.JPG;
do
   mv $i DSC_$(printf "%04d" $n).JPG;
   n=$(($n+1));
done

This code not work Smilie
My start number is 109 in this instance.
As you can see, the number of files is less. The numbers are not progressive.

Before:
Code:
DSC_0011.JPG
DSC_0012.JPG
DSC_0013.JPG
DSC_0014.JPG
DSC_0015.JPG
DSC_0016.JPG
DSC_0017.JPG
DSC_0018.JPG
DSC_0019.JPG
DSC_0020.JPG
DSC_0021.JPG
DSC_0022.JPG
DSC_0023.JPG
DSC_0024.JPG
DSC_0025.JPG
DSC_0026.JPG
DSC_0027.JPG
DSC_0028.JPG
DSC_0029.JPG
DSC_0030.JPG
DSC_0031.JPG
DSC_0032.JPG
DSC_0033.JPG
DSC_0034.JPG
DSC_0035.JPG
DSC_0036.JPG
DSC_0037.JPG
DSC_0041.JPG
DSC_0042.JPG
DSC_0043.JPG
DSC_0044.JPG
DSC_0045.JPG
DSC_0046.JPG
DSC_0047.JPG
DSC_0048.JPG
DSC_0049.JPG
DSC_0050.JPG
DSC_0051.JPG
DSC_0052.JPG
DSC_0053.JPG
DSC_0054.JPG
DSC_0055.JPG
DSC_0057.JPG
DSC_0058.JPG
DSC_0059.JPG
DSC_0060.JPG
DSC_0061.JPG
DSC_0062.JPG
DSC_0063.JPG
DSC_0064.JPG
DSC_0065.JPG
DSC_0066.JPG
DSC_0068.JPG
DSC_0069.JPG
DSC_0070.JPG
DSC_0071.JPG
DSC_0072.JPG
DSC_0073.JPG
DSC_0074.JPG
DSC_0075.JPG
DSC_0076.JPG
DSC_0077.JPG
DSC_0078.JPG
DSC_0079.JPG
DSC_0080.JPG
DSC_0081.JPG
DSC_0082.JPG
DSC_0083.JPG
DSC_0085.JPG
DSC_0086.JPG
DSC_0087.JPG
DSC_0088.JPG
DSC_0089.JPG
DSC_0090.JPG
DSC_0092.JPG
DSC_0093.JPG
DSC_0094.JPG
DSC_0095.JPG
DSC_0096.JPG
DSC_0097.JPG
DSC_0098.JPG
DSC_0099.JPG
DSC_0100.JPG
DSC_0101.JPG
DSC_0102.JPG
DSC_0103.JPG
DSC_0104.JPG
DSC_0105.JPG
DSC_0106.JPG
DSC_0107.JPG
DSC_0108.JPG
DSC_0109.JPG
DSC_0110.JPG
DSC_0111.JPG
DSC_0112.JPG
DSC_0113.JPG
DSC_0114.JPG
DSC_0115.JPG
DSC_0116.JPG
DSC_0117.JPG
DSC_0118.JPG
DSC_0119.JPG
DSC_0120.JPG
DSC_0121.JPG
DSC_0122.JPG
DSC_0123.JPG
DSC_0124.JPG
DSC_0125.JPG
DSC_0126.JPG
DSC_0127.JPG
DSC_0128.JPG
DSC_0129.JPG
DSC_0130.JPG
DSC_0131.JPG
DSC_0132.JPG
DSC_0133.JPG
DSC_0134.JPG
DSC_0135.JPG
DSC_0136.JPG
DSC_0137.JPG
DSC_0139.JPG
DSC_0140.JPG
DSC_0141.JPG
DSC_0142.JPG
DSC_0143.JPG
DSC_0144.JPG
DSC_0145.JPG
DSC_0146.JPG
DSC_0147.JPG
DSC_0148.JPG
DSC_0149.JPG
DSC_0150.JPG
DSC_0151.JPG
DSC_0152.JPG
DSC_0153.JPG
DSC_0154.JPG
DSC_0155.JPG
DSC_0156.JPG
DSC_0157.JPG
DSC_0158.JPG
DSC_0159.JPG
DSC_0160.JPG
DSC_0161.JPG
DSC_0162.JPG
DSC_0164.JPG
DSC_0165.JPG
DSC_0166.JPG
DSC_0167.JPG
DSC_0169.JPG
DSC_0170.JPG
DSC_0171.JPG
DSC_0172.JPG
DSC_0173.JPG
DSC_0174.JPG
DSC_0175.JPG
DSC_0176.JPG
DSC_0177.JPG
DSC_0178.JPG
DSC_0179.JPG
DSC_0180.JPG
DSC_0181.JPG
DSC_0182.JPG
DSC_0183.JPG
DSC_0184.JPG
DSC_0186.JPG
DSC_0187.JPG
DSC_0188.JPG
DSC_0189.JPG
DSC_0190.JPG
DSC_0191.JPG
DSC_0192.JPG
DSC_0193.JPG
DSC_0194.JPG
DSC_0195.JPG
DSC_0196.JPG
DSC_0197.JPG
DSC_0198.JPG
DSC_0199.JPG
DSC_0200.JPG
DSC_0201.JPG
DSC_0202.JPG
DSC_0203.JPG
DSC_0204.JPG
DSC_0205.JPG
DSC_0206.JPG
DSC_0208.JPG
DSC_0209.JPG
DSC_0210.JPG
DSC_0211.JPG
DSC_0212.JPG
DSC_0213.JPG
DSC_0214.JPG
DSC_0215.JPG
DSC_0216.JPG
DSC_0217.JPG
DSC_0218.JPG
DSC_0219.JPG
DSC_0220.JPG
DSC_0221.JPG
DSC_0222.JPG
DSC_0223.JPG
DSC_0224.JPG
DSC_0225.JPG
DSC_0226.JPG
DSC_0227.JPG
DSC_0228.JPG
DSC_0229.JPG
DSC_0230.JPG
DSC_0231.JPG
DSC_0232.JPG
DSC_0233.JPG
DSC_0234.JPG
DSC_0235.JPG
DSC_0236.JPG
DSC_0237.JPG
DSC_0238.JPG
DSC_0239.JPG
DSC_0240.JPG
DSC_0241.JPG
DSC_0242.JPG
DSC_0243.JPG
DSC_0244.JPG
DSC_0245.JPG
DSC_0246.JPG
DSC_0247.JPG
DSC_0248.JPG
DSC_0249.JPG
DSC_0250.JPG
DSC_0251.JPG
DSC_0252.JPG
DSC_0253.JPG
DSC_0254.JPG
DSC_0255.JPG
DSC_0256.JPG
DSC_0257.JPG
DSC_0258.JPG
DSC_0259.JPG
DSC_0260.JPG
DSC_0261.JPG
DSC_0262.JPG
DSC_0263.JPG
DSC_0264.JPG
DSC_0265.JPG
DSC_0266.JPG
DSC_0267.JPG
DSC_0268.JPG
DSC_0269.JPG
DSC_0270.JPG
renameFull.sh

After:
Code:
before.txt
DSC_0138.JPG
DSC_0163.JPG
DSC_0168.JPG
DSC_0185.JPG
DSC_0207.JPG
DSC_0271.JPG
DSC_0272.JPG
DSC_0273.JPG
DSC_0274.JPG
DSC_0275.JPG
DSC_0276.JPG
DSC_0277.JPG
DSC_0278.JPG
DSC_0279.JPG
DSC_0280.JPG
DSC_0281.JPG
DSC_0282.JPG
DSC_0283.JPG
DSC_0284.JPG
DSC_0285.JPG
DSC_0286.JPG
DSC_0287.JPG
DSC_0288.JPG
DSC_0289.JPG
DSC_0290.JPG
DSC_0291.JPG
DSC_0292.JPG
DSC_0293.JPG
DSC_0294.JPG
DSC_0295.JPG
DSC_0296.JPG
DSC_0297.JPG
DSC_0298.JPG
DSC_0299.JPG
DSC_0300.JPG
DSC_0301.JPG
DSC_0302.JPG
DSC_0303.JPG
DSC_0304.JPG
DSC_0305.JPG
DSC_0306.JPG
DSC_0307.JPG
DSC_0308.JPG
DSC_0309.JPG
DSC_0310.JPG
DSC_0311.JPG
DSC_0312.JPG
DSC_0313.JPG
DSC_0314.JPG
DSC_0315.JPG
DSC_0316.JPG
DSC_0317.JPG
DSC_0318.JPG
DSC_0319.JPG
DSC_0320.JPG
DSC_0321.JPG
DSC_0322.JPG
DSC_0323.JPG
DSC_0324.JPG
DSC_0325.JPG
DSC_0326.JPG
DSC_0327.JPG
DSC_0328.JPG
DSC_0329.JPG
DSC_0330.JPG
DSC_0331.JPG
DSC_0332.JPG
DSC_0333.JPG
DSC_0334.JPG
DSC_0335.JPG
DSC_0336.JPG
DSC_0337.JPG
DSC_0338.JPG
DSC_0339.JPG
DSC_0340.JPG
DSC_0341.JPG
DSC_0342.JPG
DSC_0343.JPG
DSC_0344.JPG
DSC_0345.JPG
DSC_0346.JPG
DSC_0347.JPG
DSC_0348.JPG
DSC_0349.JPG
DSC_0350.JPG
DSC_0351.JPG
DSC_0352.JPG
DSC_0353.JPG
DSC_0354.JPG
DSC_0355.JPG
DSC_0356.JPG
renameFull.sh

# 6  
Old 04-15-2016
Quote:
Originally Posted by Don Cragun
Note that if the starting number read into n is not 1, there is a chance that one or more of the original files will be copied into multiple renamed files and each time one of the original files is duplicated another original file will be destroyed. For instance, if the directory originally contained:
Code:
DSC_0002.JPG
DSC_0003.JPG
DSC_0004.JPG

and you entered 3 as the starting value, this script would end up with the files:
Code:
DSC_0003.JPG
DSC_0004.JPG
DSC_0005.JPG

all of which contained what was originally stored in the file named DSC_0002.JPG and the images that had been stored in DSC_0003.JPG and DSC_0004.JPG are lost (unless you made a backup copy of your files before running this script).

Note that if the starting number is 1 or if the starting number is greater than the highest number used in any existing file added to the number of files matching the pattern *.JPG when the script starts, you will be safe as long as all of the input files in the directory that match the pattern *.JPG also match the pattern DSC_[0-9][0-9][0-9][0-9].JPG.





You're right. I lose the files. How can I fix?
Thank you
# 7  
Old 04-15-2016
Quote:
Originally Posted by vegetablu
You're right. I lose the files. How can I fix?
Thank you
Unless you had backups of your files before you destroyed some of them, they are gone. To avoid the problem in the future, change the script suggested by rdrtx1 to something more like:
Code:
#!/bin/bash

printf 'Enter starting number (0-9999): '
read n
if [ "$n" != "${n#*[^0-9]*}" || [ ${#n} -lt 1 ] || [ ${#n} -gt 4 ]
then	printf 'Starting number ("%s") is invalid\n' "$n" >&2
	exit 1
fi

# Move existing files into a different namespace...
for file in DSC_*.JPG
do	mv "$file" "old_$file"
done

# Now move them to the new locations in the original namespace.
for file in old_DSC_*.JPG
do	mv "$file" DSC_$(printf "%04d" $n).JPG
	n=$(($n+1))
done

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

2. Shell Programming and Scripting

script to rename mp3 files

hi there, i'm using OS X. i have a bunch of mp3 files strewn across a directory tree that i'd like to rename. specifically i'd like to remove any track numbers and leading non-alphabetic characters from the filenames like this: 01 - song1.mp3 2 song2.mp3 become: song1.mp3... (6 Replies)
Discussion started by: creakyshrimp
6 Replies

3. Shell Programming and Scripting

Rename files in the script

Hi All, I want to write a script to rename the file in to the incremental order for example Original file filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img I want output shpuld be... (4 Replies)
Discussion started by: mangeshpardhi
4 Replies

4. Shell Programming and Scripting

Script to rename files

I have the following directories in my home directory, my scripts dbmig es ms_done my-home I want my output to look like the following MyScripts DbmigEs MsDone MyHome Basically, I want to get rid of spaces,special characters and convert the first letter of each word to uppercase and... (1 Reply)
Discussion started by: ramky79
1 Replies

5. Shell Programming and Scripting

Script Rename files

Hello, I have this problem. In a directory I have 4 csv files with this format: PHOENIX_KM_INTERAZIONI_YYYYMMDD.csv PHOENIX_KM_TRIPLETTE_YYYYMMDD.csv NEWCAB_KM_INTERAZIONI_YYYYMMDD.csv NEWCAB_KM_INTERAZIONI_YYYY_MM_DD.csv YYYYMMDD: format CURRENT date I wont rename all files in... (4 Replies)
Discussion started by: manichino74
4 Replies

6. UNIX for Dummies Questions & Answers

Script to Rename Files

I wrote a simple script that converts my windows text files to unix, so that I can compare them to different unix files purposes of my project. win2unix file1.txt file1Win.txt win2unix file2.txt file2Win.txt etc Is there a way to simplify this to: <while .txt in... (5 Replies)
Discussion started by: idano530
5 Replies

7. Shell Programming and Scripting

Shell Script to rename files

Hi, i need a bit of help writting a tcsh script which renames all ascii text files in the current directory by adding a number to their names before the extension so for example, a directory containing the files Hello.txt Hello.t Hello should have the following changes, Hello.txt... (2 Replies)
Discussion started by: yakuzaa
2 Replies

8. Shell Programming and Scripting

Script to rename files

Let me preface this by stating I have absolutely no idea what I'm doing in this arena, but I'm in need of a little help here. I need to take filenames like this: amwed_0402c-slug~1-cp.jpg And reduce them to slug~1.jpg That is, I need to remove the first 12 and last 3 characters. The... (3 Replies)
Discussion started by: cpreovol
3 Replies

9. UNIX for Dummies Questions & Answers

Script to rename files

Have files of the sort 3p1522015.dgn and need to have them renamed to 152201.dgn. Essentially dropping the 1st 2 characters and the last. I'm relatively new to UNIX and uncertain of where to start. I hope this provides enough detail. Thanks (5 Replies)
Discussion started by: Dinkster
5 Replies

10. OS X (Apple)

Rename Files with a script ?

Hi All !!! Is there any solution to get rid of / " * in old files names WITH A SCRIPT (About 100 Gb of old files) I know it can be done i just dont know how ! Hope that some one can help Best R. Yovel (1 Reply)
Discussion started by: yoveln
1 Replies
Login or Register to Ask a Question