Sponsored Content
Full Discussion: Renaming file in batch
Top Forums Shell Programming and Scripting Renaming file in batch Post 302929655 by RavinderSingh13 on Wednesday 24th of December 2014 01:41:51 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 12:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy