Renaming a file and replacing the special character in the name with date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming a file and replacing the special character in the name with date
# 1  
Old 11-17-2009
Renaming a file and replacing the special character in the name with date

HI all,

How can i rename some files and replace the special character in the name with todays date

ex: Name#file1.txt
Name#file2.txt

to be renamed as

Name.20091119.file1.txt
Name.20091119.file2.txt
# 2  
Old 11-17-2009
One way:

Code:
ls *#*.txt | awk -v var=$(date "+%Y%m%d") '{s=$0;sub("#",var);print "mv "s " "$0}' | sh

Try it first without the pipe command (the colored part).
# 3  
Old 11-17-2009
great help;

its working with the pipe
# 4  
Old 11-17-2009
Code:
d=$(date +%Y%M%d)
for file in Name*.txt
do 
    newfile=${file/Name/Name."$d".}
    echo mv $file ${newfile//#} 
done

Or if you have Python 2.4+ and don't mind, you can use this Python script.
usage:
Code:
$ filerenamer.py -p "Name#" -e "Name.`date +%Y%m%d`." -l "*"
==>>>>  [ /home/Name#file2.txt ]==>[ /home/Name.20091117.file2.txt ]
==>>>>  [ /home/Name#file1.txt ]==>[ /home/Name.20091117.file1.txt ]

remove -l option to commit changes

Last edited by ghostdog74; 11-17-2009 at 11:36 AM..
# 5  
Old 11-18-2009
can we achieve this using sed??
# 6  
Old 11-18-2009
Quote:
Originally Posted by abhinav192
can we achieve this using sed??
Something like this?

Code:
sed 's/\(.*\)#\(.*\)/mv & \1.'"$(date "+%Y%m%d")"'.\2/' | sh

# 7  
Old 11-18-2009
hi FRanklin,

As always, great help...

Can you please explain this command in parts...It would be a great help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Any tip to replacing the special characters in a file

Hi, Please find attached a file that has special characters on it. It is a copy and paste from a Micro$oft file. I don't want to use strings as it remove all the 'indentations' / 'formatting' so I am replacing them with space instead. I am using the sed command below sed "s/$(printf... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. UNIX for Dummies Questions & Answers

Replacing special character with sed

Hi All, I have a text file that contains I1SP2 *=*=Y=M=D001D My requirement is to replace all occurrence of =* to =Z expected o/p is I1SP2 *=Z=Y=M=D001D I have tried with sed 's/=*/=Z/g' file sed 's!\=*!\=Z/g' file sed 's!\=*!\=Z!g' file sed 's!\=\*!\=Z!g' file but its not... (3 Replies)
Discussion started by: gotamp
3 Replies

3. Shell Programming and Scripting

Replacing Date in the file with Create date and timestamp

Hello, I have files that with a naming convention as shown below. Some of the files have dates in the file name and some of them don't have dates in the file name. imap-hp-import-20150917.txt imap-dell-gec-import-20150901.txt imap-cvs-import-20150915.txt imap-gec-import.txt... (8 Replies)
Discussion started by: Saanvi1
8 Replies

4. Shell Programming and Scripting

Removing Special Character from File.

Hi, My file has this special character "^M" I would like to remove this characters. eg: abc,abc,^M i tried using sed but doesnt work. i used octal dump command to see special character it returns following: 015 \r Appreciate your reply. (6 Replies)
Discussion started by: pinnacle
6 Replies

5. Shell Programming and Scripting

parse a file for a special character

hello, How to parse a file to see if a specific line is commented by '#' character? filename: file1 cat file1 ... # /usr/bin/whatever ... thank you (9 Replies)
Discussion started by: melanie_pfefer
9 Replies

6. UNIX for Dummies Questions & Answers

Special character in my file

I have a special character in my file. It displays as a '#' sign but when I do this command I do not find the line. fgrep 'G#ant' file1 I want to replace the special character with another value but I need to know what character it really is. Any ideas on how to replace this '#' value with... (3 Replies)
Discussion started by: Ryan2786
3 Replies

7. Shell Programming and Scripting

delete a special character in file

hi i want to delete a particular character in file. example file name:abcsample abc=bbbqw3/ hidh=ajjqiwio4/ xyx=hakjp/ ........../ ......./ i want to delete that special character (/) in abcsample file Permnently.please give the required commands for my requirement. required... (1 Reply)
Discussion started by: srivsn
1 Replies

8. Shell Programming and Scripting

delete a special character in file

hi i want to delete a particular character in file. example file name:abcsample abc=bbbqw3/ hidh=ajjqiwio4/ xyx=hakjp/ ........../ ......./ i want to delete that special character (/) in abcsample file.please give the required commands for my requirement. thank you (3 Replies)
Discussion started by: srivsn
3 Replies

9. Solaris

Mount a character special file

Hi together I have 2 systems, mars and venus. The configuration is the same. Every system has a SDLT. I will now backup the datas from mars on the tapedevice from venus. I have shareed the tapedevice (venus) and mounted on mars. Now my problem: when I write on the mountet tapedevice, the... (1 Reply)
Discussion started by: MuellerUrs
1 Replies

10. Shell Programming and Scripting

replacing string with special character ???

the problem is while replacing the old string with new one with the help of SED i am unable to replace the special characters with new strings. how can i do that? i dont want the user to be given the trouble to write '\' before every special characters like * , . , \ , $ , &. sed... (4 Replies)
Discussion started by: imppayel
4 Replies
Login or Register to Ask a Question