copying files and Renaming them + shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copying files and Renaming them + shell script
# 1  
Old 05-24-2005
Lightbulb 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 should be appended by "date and timestamp" before the .txt


Can any one tell a shell script for this?


Example

All the above txt files should be renamed as

emp"Date and TimeStamp".txt
emp1"Date and TimeStamp".txt
emp3"Date and TimeStamp".txt
32emp4"Date and TimeStamp".txt


Thanks in advance....
# 2  
Old 05-24-2005
Its always good if you have a starting script to work on. You learn better.


Code:
#! /bin/sh

for i in `ls *emp*.txt`
do
mv $i path/to/new/folder/$i$(date +%F-%T).txt
done

Not tested.

Vino
# 3  
Old 05-24-2005
Thanks !!!!!!!!!!!!

Thanks Vino.

It worked.
# 4  
Old 05-24-2005
It did ? Didnt you get the .txt twice ? Smilie

Vino
# 5  
Old 05-24-2005
Any way to remove .txt before the date field

Hi Vino,

I got the .txt twice but for my problem i think it should be fine.

If you have any way to remove the .txt before the date let me know.

I can adjust with the code sent by you....

Thanks.....
# 6  
Old 05-24-2005
This should do it.
Code:
#! /bin/sh

for i in `ls *.cpp`
do
tmp=`echo $i | awk -F"." '{ print $1 }'`
mv $tmp.cpp $tmp$(date +%F-%T).cpp
done


Vino
# 7  
Old 05-24-2005
Thanks Again !!!!!

Thanks Vino.........

This one worked perfectly fine......
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files to a directory, renaming it if a file with the same name already exists

Hi All, I need to copy files from one directory to another with the files to be renamed while copying if a file with the same name already exists in the target directory. THanks, Dev (2 Replies)
Discussion started by: dev.devil.1983
2 Replies

2. Shell Programming and Scripting

Copying files from one directory to another, renaming duplicates.

Below is the script i have but i would like simplified but still do the same job. I need a script to copy files not directories or sub-directories into a existing or new directory. The files, if have the same name but different extension; for example 01.doc 01.pdf then only copy the .doc file. ... (1 Reply)
Discussion started by: Gilljambo
1 Replies

3. UNIX for Dummies Questions & Answers

Renaming files with part of their pathname and copying them to new directory

Hi I think this should be relatively simple but I can't figure it out. I have several files with the same name in different folders within a directory (the output of a program that I ran). Something like this: ./myAnalysis/item1/round1/myoutput.txt ./myAnalysis/item1/round2/myoutput.txt... (2 Replies)
Discussion started by: jullee
2 Replies

4. Shell Programming and Scripting

Copying and renaming files

hi, source directory as /home/home01 target directort as /home/home02 I have below files in source directory: FrontOf_history.dat FrontOf_history1.dat In target directory have many files as: Kront_2014.dat Kront_2015.dat Kront_2016.dat Now i want to copy the two files... (1 Reply)
Discussion started by: Vivekit82
1 Replies

5. Shell Programming and Scripting

Copying and Renaming script

Hey all! Sorry for the noobish question.. I am wondering how to I copy multiple files, and rename them based on their directory at once. So, I have files like this /logs/server/2011-10/server-1/log/file/folder/some.log.gz /logs/server/2011-10/server-2/log/file/folder/some.log.gz... (3 Replies)
Discussion started by: m3power206
3 Replies

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

7. Shell Programming and Scripting

Copying files and renaming

The goal is to read names of files defined ouside in upload.conf and rename them using date, time and proper extension. I have made short script while read; do cp "$RELAY" "$RELAY(date +%Y-%m-%d_%H:%M:%S)_DEPLOYED.ear" done < upload.conf but unfortunatelly it fails printiong the... (9 Replies)
Discussion started by: Michal Janusz
9 Replies

8. Shell Programming and Scripting

Need help copying and renaming files

I would like to copy files from one directory to another directory while renaming them at the same time. Does anyone have any ideas on how to do this in a script? Basically, would like to copy files from /user/data/prod/*.txt to /user/data/bck/*.txt.bck (3 Replies)
Discussion started by: thunderkiss65
3 Replies

9. UNIX for Dummies Questions & Answers

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... (5 Replies)
Discussion started by: asmfloyd
5 Replies
Login or Register to Ask a Question