Find and rename part of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and rename part of a file
# 1  
Old 03-20-2014
Find and rename part of a file

hi,
Need your help.
I need to write a script for below..
i have two files in directory
Code:
/home/abc

as below:
Code:
 
Watch_20140203_abc.dat
Watchnow_20140203_abc.dat

I have to copy this file from
Code:
 
/home/abc to /home01/home02

after that i have to rename the date part in above two files with the input date that that i should be able to pass as part of my script.

i copied the file as below:
Code:
 
cp /home/abc /home01/home02

Not sure how to proceed after that..Please help.
# 2  
Old 03-20-2014
I did not test this and I didn't bother to check to see if
the date in the filename is an actual date, I just parsed
based on the fact that if you use "_" as a delimiter.

Code:
input_string=${1}
source_dir="/home/abc"
target_dir="/hom01/home02"
ls ${source_dir} | while read infile
do
        pre=`echo ${infile} | cut -d"_" -f1`
        post=`echo ${infile} | cut -d"_" -f3`
        new_name="${pre}_${input_string}_${post}"
        cp ${source_dir}/${infile} ${target_dir}${new_name}
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to rename last part of file ?

Hi, I have large number of files like below - UNIX FY17 D21-1c Active user audit - NPP dir owner listing(vctmstt01)_072816 - Notepad.pdf UNIX FY17 D21-1c Active user audit - TTMS dir owner listing(pvcdmot35)_072816 - Notepad.pdf UNIX FY17 D21-1c Active user audit - PCP dir owner... (3 Replies)
Discussion started by: solaris_1977
3 Replies

2. UNIX for Dummies Questions & Answers

Find and rename file recursively

Hi, I have a directory which contains multiple files with .txt extension, i want to rename all these file to .bak extension using find command, this is what i've tried, please help me to correct this : find /home/application/test -name '*.txt' -exec rename 's/txt/bak/' {} \; seems to... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

3. Shell Programming and Scripting

How to find and rename of particular pattern in file.?

Hi Guys, I have folder called /input/temp. Inside the folder I have lot of files. I need to find the file of pattern Article_????_test_?????????.txt and replace by format below. Article_????_?????????.txt Below is the one which I tried but it doesn't works can you please help us.... (4 Replies)
Discussion started by: Vinoth Kumar G
4 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Rename file name / remove part of name

I have a whole file structure with jpeg files where I want to remove a part of the file name. An application added in many files a case conflict in the naming "xyz 017.jpg (Case Conflict 1)" So, can someone help me how to get rid of the " (Case Conflict 1)"? What I have is this: find . -name... (2 Replies)
Discussion started by: borobudur
2 Replies

5. Shell Programming and Scripting

Problem with Find and rename file

I want to find a file say IIFT and check its size is zero or not. If its zero then I have to rename anothe file say WWFT , which is in another folder to WWFT$Todaysdate. I tried below command: cd dir2 (*File WWFT is in dir2) find dir/ -type f -name 'IIFT*' -size 0 -exec mv WWFT... (3 Replies)
Discussion started by: ammbhhar
3 Replies

6. UNIX for Dummies Questions & Answers

Find and Replace then rename file

Hi, This is probably quite simple for an expert, but I keep getting confused about the best approach, grep, awk, sed. What I have is a range of files numbered 1 to 100. They go file1.txt file2.txt and so on In each file I need to find and replace a couple of items and rename add a... (5 Replies)
Discussion started by: chickenhouse
5 Replies

7. Shell Programming and Scripting

Find and Rename File using Terminal

I need help finding a file through terminal and then renaming it automatically. Here is what I have so far to find the file: cd /User/Applications find . */SourceM.app/banner.png | while read line; do mv "$line" banner-.png; done I want the script to rename the file "banner.png" to... (6 Replies)
Discussion started by: rbisconti97
6 Replies

8. Shell Programming and Scripting

rename file by removing some part of the file name

I am special requirements to rename file. I have files with names like below: 1_firstname1_lastname1.html 2_firstname2_lastname2.html 3_fistname3_lastname2.html I would like these file to be renamed as below firstname1_lastname1.html firstname2_lastname2.html... (5 Replies)
Discussion started by: McLan
5 Replies

9. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question