Renaming file in batch


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming file in batch
# 1  
Old 12-23-2014
Linux Renaming file in batch

Hi guys,

I need a script to change the file names e.g.:-

below are the mentioned files, i want t0 change the last character of the name 00000.cdr with e.g bep01.smsc.191214210500-00000.cdr to bep01.smsc.191214210500-92311.cdr next file be like bep01.smsc.191214210500-92312.cdr

Code:
[root@bep01 done]# ls -ltr | grep 00000.cdr

-rw-r--r--  1 ss7 ss7  58286 Dec 19 21:09 bep01.smsc.191214210500-00000.cdr
-rw-r--r--  1 ss7 ss7  52022 Dec 19 21:14 bep01.smsc.191214211000-00000.cdr
-rw-r--r--  1 ss7 ss7  58772 Dec 19 21:19 bep01.smsc.191214211500-00000.cdr
-rw-r--r--  1 ss7 ss7  51905 Dec 19 21:24 bep01.smsc.191214212000-00000.cdr
-rw-r--r--  1 ss7 ss7  52414 Dec 19 21:29 bep01.smsc.191214212500-00000.cdr
-rw-r--r--  1 ss7 ss7  49413 Dec 19 21:34 bep01.smsc.191214213000-00000.cdr
-rw-r--r--  1 ss7 ss7  54275 Dec 19 21:39 bep01.smsc.191214213500-00000.cdr
-rw-r--r--  1 ss7 ss7  46818 Dec 19 21:44 bep01.smsc.191214214000-00000.cdr
-rw-r--r--  1 ss7 ss7  53009 Dec 19 21:49 bep01.smsc.191214214500-00000.cdr
-rw-r--r--  1 ss7 ss7  49944 Dec 19 21:54 bep01.smsc.191214215000-00000.cdr
-rw-r--r--  1 ss7 ss7  51558 Dec 19 21:59 bep01.smsc.191214215500-00000.cdr
-rw-r--r--  1 ss7 ss7  50739 Dec 19 22:04 bep01.smsc.191214220000-00000.cdr
-rw-r--r--  1 ss7 ss7  47979 Dec 19 22:09 bep01.smsc.191214220500-00000.cdr

I have 500 files please help to change the name like above.
Moderator's Comments:
Mod Comment Putting QUOTE tags around your entire post and removing ICODE tags does not help us see the commands you have used, the output those commands produced, nor the input you're trying to process. Please use CODE tags when showing nulti-line code segments, sample input, and sample output. You can use ICODE tags when showing in-line input, output, or code in the middle of a sentence.

Last edited by Don Cragun; 12-24-2014 at 02:15 AM.. Reason: Removed QUOTE tags, added CODE and ICODE tags.
# 2  
Old 12-23-2014
Quote:
Originally Posted by mfaizan40
hi guys,

i need a script to change the file names e.g

abc.122314175400-00000.txt to abc.122314175400-00001.txt

abc.122314175400-00000.txt to abc.122314175400-00002.txt

abc.122314175400-00000.txt to abc.122314175400-00003.txt

i have 500 files please help to chnge the name like above.
Hello mfaizan40,

Welcome to forums, could you please use code tags for commands/codes which you are using in your posts as per forum rules.
Following link may help you in same for understanding rules for forum.

https://www.unix.com/misc.php?do=cfrules


Regarding your query, I can see all files which you want to rename are of same name(provided by you in question), is it a typo?
Could you please provide more details on same it will be helpful for us to help you in same.


Thanks,
R. Singh
# 3  
Old 12-23-2014
Code:
$ cat file
abc.122314175401-00000.txt
abc.122314175400-00000.txt
abc.122314175402-00000.txt
$ sort file | awk ' { flnm=$0;sub("00000",sprintf("%05d",NR)); print "mv " flnm " " $0 } '
mv abc.122314175400-00000.txt abc.122314175400-00001.txt
mv abc.122314175401-00000.txt abc.122314175401-00002.txt
mv abc.122314175402-00000.txt abc.122314175402-00003.txt

Code:
sort file | awk ' { flnm=$0;sub("00000",sprintf("%05d",NR)); print "mv " flnm " " $0 } ' | sh

# 4  
Old 12-24-2014
thanks for your reply. i edit the post with the exact scenario. can you please check and help me.

---------- Post updated at 01:08 AM ---------- Previous update was at 01:01 AM ----------

i edit the post with the exact scenario.can you plz check.
# 5  
Old 12-24-2014
You can use anbu23's suggestion as a starting point, with some minor changes since the names you want aren't stored in a file and since you don't want the numbers in the new names to start at 00001. If the command ls *-00000.cdr works without giving you an error for an argument list that is too long, try something like:
Code:
startseq=92311
ls *-00000.cdr | awk -v s="$startseq" '{f=$0;sub("00000.cdr",sprintf("%05d.cdr",s++)); print "mv", f, $0}' | sh

otherwise, try something like:
Code:
startseq=92311
ls | grep -F '-00000.cdr' | awk -v s="$startseq" '{f=$0;sub("00000.cdr",sprintf("%05d.cdr",s++)); print "mv", f, $0}' | sh


Last edited by Don Cragun; 12-24-2014 at 04:01 AM.. Reason: Changed ".txt" to ".cdr" to match name changes in post #1 in this thread.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 12-24-2014
Quote:
Originally Posted by mfaizan40
thanks for your reply. i edit the post with the exact scenario. can you please check and help me.

---------- Post updated at 01:08 AM ---------- Previous update was at 01:01 AM ----------

i edit the post with the exact scenario.can you plz check.
Hello mfaizan40,

Kindly use code tags for commands and codes which you are using your posts as per forum rules. Following may help you in same.
I have created same name test files as follows.
Code:
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214210500-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214211000-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214212000-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214211500-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214212500-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214213000-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214214000-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214213500-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214214500-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214215500-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214215000-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214220000-00000.cdr
-rw-r--r-- 1 Singh01 Singh01    0 Dec 24 01:13 bep01.smsc.191214220500-00000.cdr

Then following script may help in same.

Code:
cat move_files.ksh
default=92311
for j in "bep01.smsc.*"
do
        A=$j
done
for i in $A
do
        value=`echo $i | awk '{gsub(/\-.*/,X,$0);print}'`
        new_value=$value"-"$default"".cdr"
        mv $i $new_value
        echo file $i " has been renamed to " $value"-"$default".cdr"
        let "default = default + 1"
done

Output will be as follows after running the script.
Code:
./move_files.ksh
file bep01.smsc.191214210500-00000.cdr  has been renamed to  bep01.smsc.191214210500-92311.cdr
file bep01.smsc.191214211000-00000.cdr  has been renamed to  bep01.smsc.191214211000-92312.cdr
file bep01.smsc.191214211500-00000.cdr  has been renamed to  bep01.smsc.191214211500-92313.cdr
file bep01.smsc.191214212000-00000.cdr  has been renamed to  bep01.smsc.191214212000-92314.cdr
file bep01.smsc.191214212500-00000.cdr  has been renamed to  bep01.smsc.191214212500-92315.cdr
file bep01.smsc.191214213000-00000.cdr  has been renamed to  bep01.smsc.191214213000-92316.cdr
file bep01.smsc.191214213500-00000.cdr  has been renamed to  bep01.smsc.191214213500-92317.cdr
file bep01.smsc.191214214000-00000.cdr  has been renamed to  bep01.smsc.191214214000-92318.cdr
file bep01.smsc.191214214500-00000.cdr  has been renamed to  bep01.smsc.191214214500-92319.cdr
file bep01.smsc.191214215000-00000.cdr  has been renamed to  bep01.smsc.191214215000-92320.cdr
file bep01.smsc.191214215500-00000.cdr  has been renamed to  bep01.smsc.191214215500-92321.cdr
file bep01.smsc.191214220000-00000.cdr  has been renamed to  bep01.smsc.191214220000-92322.cdr
file bep01.smsc.191214220500-00000.cdr  has been renamed to  bep01.smsc.191214220500-92323.cdr

Kindly let me know if this helps.

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-24-2014 at 02:56 AM.. Reason: Added a comment
# 7  
Old 12-24-2014
Quote:
Originally Posted by RavinderSingh13
Hello mfaizan40,

... ... ...

Code:
cat move_files.ksh
default=92311
for j in "bep01.smsc.*"
do
        A=$j
done
for i in $A
do
        value=`echo $i | awk '{gsub(/\-.*/,X,$0);print}'`
        new_value=$value"-"$default"".cdr"
        mv $i $new_value
        echo file $i " has been renamed to " $value"-"$default".cdr"
        let "default = default + 1"
done

... ... ...
Ravinder correctly notes that expanding the parameter list in a for statement avoids the possible argument list too long problem.

But:
  1. I don't understand the need for the 1st for loop.
  2. Calling awk 500 times seems like a lot of overhead when parameter substitution can be done in the shell without the overhead of invoking a separate process.
  3. There is a mismatched double quote in $value"-"$default"".cdr". Perhaps "$value-$default.cdr" is what was intended.
  4. While just expanding $default works fine as long as it starts out as a five digit number, it might be safer to use printf with a five digit leading zero filled format to cover the general case.
  5. It seems to be important to match the terminating -00000.cdr at the ends of the filenames in case other files starting with bep01.smsc. have already been renumbered.
  6. The let command is less portable than arithmetic substitution.

Taking the above considerations into account, you might want to also try something like:
Code:
#!/bin/ksh
sequence=92311
for i in bep01.smsc.*-00000.cdr
do	new="${i%00000.cdr}$(printf '%05d.cdr' "$sequence")"
	# mv $i $new
	echo "file $i has been renamed to $new"
	sequence=$((sequence + 1))
done

which will work with ksh, bash, or any other shell that performs POSIX standard parameter expansions and arithmetic substitutions.

If the echo statements show that the renaming calculations are correct, remove the # from the start of the mv command line so the script will actually move the files.
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. UNIX for Beginners Questions & Answers

Renaming batch by removing substring

I wish to rename all files ending in .txt by removing .tex. Currently I have me@me-Inspiron-518:~$ ls a.tex.txt bin b.tex.txt c.tex.txt Desktop Documents Downloads d.tex.txt Music Pictures Public Templates VideosDesired outcome me@me-Inspiron-518:~$ ls a.txt ... (2 Replies)
Discussion started by: Xubuntu56
2 Replies

2. Shell Programming and Scripting

Script for batch renaming

What is wrong with this script to rename batch of similar files? For example renaming all *.txt files to *.tab . $ for i in seq {01..10}; do touch ${i}.txt; done $ ./rename.sh *.txt txt tab Error: mv: ‘01.txt’ and ‘01.txt’ are the same file. Code is: #!/usr/bin/bash # renames.sh #... (6 Replies)
Discussion started by: yifangt
6 Replies

3. Shell Programming and Scripting

SFTP batch not renaming file with "put"

I have a .ksh script that creates an sftp batch file and runs it through sftp. It works except for one thing. If I try to "put" to a different name, it doesn't use the specified remote name...it still "puts" the original local name. I've tried both of these, and neither work...it will always... (4 Replies)
Discussion started by: dbiggied
4 Replies

4. Shell Programming and Scripting

Need help in batch renaming files with bash shell script.

I have some 50+ files in the following format : abcd_vish_running_ZEBRA_20140818.dat_08-14-2014_23:08:23 abcd_vish_running_ZEB-RA_20140818.dat_08-14-2014_23:08:35 abcd_vish_running_ZEB_RA_20140818.dat_08-14-2014_23:08:37 abcd_vish_running_RI-NG_20140818.dat_08-14-2014_23:08:42... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

5. Shell Programming and Scripting

BASH Batch renaming insert additional zero into filename

Hi all, Wondering how this could be accomplished........ a directory contains sequentially numbered files from fw01 to fw999. How would I insert an additional zero so that the directory lists these files in a proper manner? (i.e. all double digit files from fw01 to fw99 would become... (3 Replies)
Discussion started by: putter1900
3 Replies

6. Shell Programming and Scripting

script for renaming a batch of files

hi i have a folder full of files. some of the names are quite off because the dimensions were the same and i had to put a 'b' after the initial number so that it didnt overwrite. what i want is a script in unix to overwrite the filwe name leaving some of the title intact, e.g. below are some... (3 Replies)
Discussion started by: shabs1985
3 Replies

7. UNIX for Dummies Questions & Answers

Batch Renaming of Files

Hello all, thanks for your time (and this forum, what an awesome resource for newbs like myself!) Anyways, I've been given the task of importing content from a directory of about...7000 HTML files. They are all named appropriately and broken down by name depending on what book they belong too.... (8 Replies)
Discussion started by: gratefulhokie
8 Replies

8. Shell Programming and Scripting

batch renaming ...

hi all, given a path, for example : /<pwd>/artist/album/ what i would like to do is to rename the album directory like that : /<pwd>/artist/artist | album/ and i would like to do the latter for all the "artist" directories and for all the "album" directories that belong to an artist ... (4 Replies)
Discussion started by: OneDreamCloser
4 Replies

9. UNIX for Dummies Questions & Answers

Batch Renaming: Change files' extensions in many sub-directories

Hi all - I'm trying to rename a large number of files all at once and need some help figuring out the command line syntax to do it. I've already done quite a bit of research with the rename and mv commands, but so far haven't found a solution that seems to work for me. So: The files exist... (10 Replies)
Discussion started by: dave920
10 Replies

10. UNIX for Dummies Questions & Answers

simple batch renaming...45-*.php to 46-*.php

in Bash i'm trying to rename directories full of files. the file name pretty much stays the same except for the numerical prefix which will be the same for all files. so, i want to rename these... 45-body.php 45-header.php 45-footer.php etc. to... 46-body.php 46-header.php... (2 Replies)
Discussion started by: bcamp1973
2 Replies
Login or Register to Ask a Question