Grep and rename the filename

Hi All,

Can you please help me.

The situation is like this. There are many different file name in this directory.
I have to grep all the file that the name start with "PTWO" and rename it to COM with the current date.

This is the script that I have done and it hit an error..:confused::confused:
#!/bin/bash

DAY=`/bin/date +%Y%m%d`

for hs in `ls $target/FTPUSERs/$hh`; do

   \`grep -i ^PTWO $target/FTPUSERs/$hh/\`
    
    \`mv $hs COM$DAY.txt\`

    done

Hope u can help me...:o:o

here is a basic code to rename multiple files having common name . You have to modify it as per your requirement

===============
for filename in *;
do
newname=`echo $filename | sed -e 's/oldname/newname/g'`
mv $filename $newname
done

Thanks amitranjansahu,

But my biggest problem right now is how to grep all the filename that start with PTWO and change them to the same new name. There are also other filename in the directory. :confused::confused:

for file in PTWO/*
do
mv "$file" "COM$(date + "%Y%m%d").txt"
done

cheers,
Devaraj Takhellambam

Thanks devtakh,

But I think u have misunderstand my problem.

PTWO is not the folder name. It is a file name.

Let me clear the situation to you. In folder A there are many different files with different names.
I need to grep all the files in that folder which the name start with "PTWO" and rename them to "COM" with the current date.

Thanks for your help~

for file in `ls PTWO* 2>/dev/null` # from folder A
do
mv "$file" "COM$(date + "%Y%m%d").txt"
done

cheers,
Devaraj Takhellambam

devtakh,

I don't think you want to do that, it moves all the selected files to 1 file.

badbunny9316,

Better is to give some examples of the file names and the desired names.

I agree to this. We should probably have a counter appended to the new filenames.

mv "$file" "COM_$i_$(date + "%Y%m%d").txt"

and increment i for each file. just a thought but depends on the exact requirement.

Hi all,

Let me explain ya..

In folder A, there are several file with different name.
Example :

PTWO121.txt
PTWO122.txt
PONE145.txt
PTWO151.txt
PONE181.txt
PTWO191.txt

I only want to grep the file that the name start with PTWO and rename it to COM and current date.

Desired output :

COM20090412.txt
COM20090412.txt
PONE145.txt
COM20090412.txt
PONE181.txt
COM20090412.txt

Thanks for your help~

It's not possible to have 4 files with the same name in a directory.

Two different files can't have the same name. It will overwrite the exiting one when you try to move another one with the same name. That is why I am suggesting you have filenames like below. If that works out for you, you can use my old code with the i.
COM_1_20090412.txt
COM_2_20090412.txt

Hi all,

Thanks Franklin52 for the reminder.

This is my desired output :

COM20090412_1.txt
COM20090412_2.txt
PONE145.txt
COM20090412_3.txt
PONE181.txt
COM20090412_4.txt

To devtakh : The script that you give hit an error.

I still can't solve it..

Thanks for your help~

What is the script you are using and what is the error it is giving.

Here is the code for your issue.

=========code================

count=`ls PTWO* | wc -l`
old=PTWO
day=`date +%Y%m%d`
new=COM$day

while [ "$count" -gt 0 ]
do
count=`ls PTWO* | wc -l`
file=`ls PTWO* | tail -1`
newfile=`echo "$file" | sed "s|"$old"|"$new"|g"`
mv $file $newfile > /dev/null

done

echo " RENAME DONE"

I use this script for such task:

day=`date +%Y%m%d`
count=1
list_file=`ls -1 | grep PTWO`
for file in $list_file
do
mv $file COM${day}_${count}.txt
count=`echo $count + 1 | bc`
done

Have a nice day.:wink:

Hi,

Thanks guys for all the script.
After reviewing all of your script, I have choose the one from alepi..
And my program has work after customizing the script a little bit..

Thank You so Much!! Although I am a newbie in shell scripting, it is very enjoyable doing it because of you guys..

Anybody know on how to use shell script to insert all the data in the database??

Let me use the example from the situation before this :

After I have rename all the file name that start with the word "PTWO", I want to keep track all the previous file name(before the renaming process) and the latest filename (after it has been rename).

So I want to create a table that have 2 column which are "previous file name" and "new file name".

Anybody know on how to do it??:confused:

ls -1 + grep is useless.

for file in PTWO*
do
...
done