Renaming Files using Shell Script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Renaming Files using Shell Script
# 1  
Old 05-04-2009
Renaming Files using Shell Script

Hi Gurus,
I have some files(all ending with .out as extension).
Ex:
aa1.out
aa2.out
aa3.out

I would like to append each file with the current date to the end of the file so that they should become aa1_20090504.out.
So I am using rename as follows:

for i in path/aa* ; do mv $i `basename dd`_`date +"%Y%m%d"`.out ; done ;
But when I rename, they become as "aa1.out_20090504.out".How can I remove the ".out" extension in the file which comes after the aa1?


Thanks,
Floyd
# 2  
Old 05-05-2009
Hi,
I got the same when I tried

for i in path/aa* ; do mv $i `basename $i .out dd`_`date +"%Y%m%d"`.out ; done ;

Thanks
Floyd
# 3  
Old 05-05-2009
Try This ,it will work only if you want three character filename
i.e. aaa_20090505.out
It will not work for aaaa.out

dd=`date +"%Y%m%d"`
for i in `ls -lrt`
do
x=`echo $i | cut -c 1-3`
y=$x_$dd.out
mv $i $y
done
# 4  
Old 05-05-2009
Try...
Code:
for i in aa?.out
do 
      mv $i ${i%.out}$(date "+_%Y%m%d.out")
done

# 5  
Old 05-05-2009
It will work definately ....
for filename of any length
dd=`date +"%Y%m%d"
for i in `ls- lrt`
do
x=`basename $i .out`
mv $i $x_dd.out
done
# 6  
Old 05-05-2009
Try this:

Code:
for i in path/aa* 
do 
name=${i%.*}
mv $i $name"_`date +"%Y%m%d"`.out" 
done


cheers,
Devaraj Takhellambam
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script for renaming and moving Files - Easy?

Hey guys, ive been working on this for about 2hrs now - without any solution. At first I need to say I dont have skills in linux bash scripting, but I tried to use some codesnippets and manuals from google. What I want to do: I have different folders including 2 different filestypes with... (15 Replies)
Discussion started by: peter1337
15 Replies

2. Shell Programming and Scripting

Shell Script Help..Renaming Quoted files removing the timestamp

Hi all, i am new to this forum, unix and shell scripting. I would really appreciate if you all can help me here.. I have files coming in the below format 'filename20513'13May06:03:45 filename are characters.. like 'ABDDUT20513'13May06:03:45 i need it to be renamed as... (17 Replies)
Discussion started by: khman
17 Replies

3. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 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

Shell renaming files

I'm completely new to Shell scripting and I need to make a script that renames files with names like this: "aaa-b_ccc" where aaa b and ccc can change but b is a single character into files with names like this one: "ccc'_bbb'_aaa" where ccc', bbb', aaa' are values I set accordingly to the... (5 Replies)
Discussion started by: Ollow
5 Replies

6. Shell Programming and Scripting

renaming directories with shell script

Hi All after looking around the website and various other resources I become stuck. I'm trying to rename directories from Firstname Initial Lastname to lastname,_firstname_initial so far ive got for f in {./} do rename -n 'y/A-Z/a-z/' * rename -n 's/\ /_/g' * ... (2 Replies)
Discussion started by: harlequin
2 Replies

7. Shell Programming and Scripting

Renaming multiple files with a shell script

Hey guys, I'm really new to UNIX and shell scripting in general. For my internship I need to rename a bunch of files. Specifically, I need to change the first letter of each of the files to lowercase and I have to change the endings so they all basically look like "file_cone.jpg". I know I... (4 Replies)
Discussion started by: jjzieve
4 Replies

8. Shell Programming and Scripting

Script for renaming files

I wanna back up the original version of files in a directory by appending .ORIG to them. I'm guessing I'd need CP and AWK in some form or fashion. Can someone give me a template? Thanks (3 Replies)
Discussion started by: stevenswj
3 Replies

9. Shell Programming and Scripting

Need help with shell script for moving/deleting/renaming files

Hi. I need help with a little script that will be used to move some files to their parent directory, delete the directory, rename one file in the parent directory and delete another, then continue to the next. Here's an example: /var/media/Music/Genesis/1970 album - Trespass (2008 Box -... (4 Replies)
Discussion started by: aflower
4 Replies

10. Shell Programming and Scripting

copying files and Renaming them + shell script

Hi, I have a problem. I have some text files in a folder. The names can be like: emp.txt emp1.txt emp3.txt 32emp4.txt What i need is i have to copy all the files which have "emp" string in their filename to a different folder and those file names... (7 Replies)
Discussion started by: pathanjalireddy
7 Replies
Login or Register to Ask a Question