Operating System - HP-UX
1820390 Members
3467 Online
109623 Solutions
New Discussion юеВ

script for copying and renaming files

 
SOLVED
Go to solution

script for copying and renaming files

I have hit a brick wall with a script.
Can anyone plese help.

I need to copy all the files from a directory to files within the same directory with a different name. For example.
filename1 ---> filename1-new
filename2 ---> filename2-new
filename3 ---> filename3-new
This will be done daily so I don't want to end up with additional copies as the script is run each day, like filename1-new and filename1-new-new and filename1-new-new-new, I want the new file to be overwritten.

Then I want to sort through these new files for a string of text and replace it.

Any help or advise would be greatly appreciated.
12 REPLIES 12
Sridhar Bhaskarla
Honored Contributor

Re: script for copying and renaming files

Hi,

Renaming the files is simple.

#!/usr/bin/ksh
DIR=/your_dir

for FILE in ls $DIR
do
cp ${DIR}/${FILE} ${DIR}/${FILE-new}
done

Now what kind of text is this?. Is this same everyday or does it change?.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: script for copying and renaming files

I will re-write the script with a correction and to replace string.

#!/usr/bin/ksh
DIR=/your_dir
STRING="this is a string"
REPLACE="replace it"

for FILE in $(ls ${DIR})
do
sed 's/'"$STRING"'/'"$REPLACE"'/g' ${DIR}/${FILE} > ${DIR}/${FILE}-new
done

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Ravi_8
Honored Contributor

Re: script for copying and renaming files

Hi,

#cd /
#ls > file

#for i in `cat file`
do
mv $i $i-new
done
never give up
Jean-Louis Phelix
Honored Contributor

Re: script for copying and renaming files

Hi,

I think that previous scripts don't care about the -new-new-new... problem:

#!/usr/bin/sh
DIR="mydir"
STRING="-new"
for VAR in $(ls $DIR 2>&- | grep -v "$NEW"$)
do
cp $VAR $VAR$STRING
done

Regards.
It works for me (┬й Bill McNAMARA ...)
john korterman
Honored Contributor
Solution

Re: script for copying and renaming files

Hi,
it is not practical to use the hyphen/slash character as extension for filenames, particularly not if you have to specify it as parameter for a command, e.g. grep: the shell mistakes it for a command option. Underscore is better.
You could try that as a variation on the script made by Jean Phelix, e.g.:

#!/usr/bin/sh
DIR="mydir"
STRING="_new"
cd $DIR
for VAR in $(ls $DIR 2>&- | grep -v "$STRING"$)
do
cp $VAR $VAR$STRING
done

regards,
John K.
it would be nice if you always got a second chance
Jean-Louis Phelix
Honored Contributor

Re: script for copying and renaming files

Thanks John ... I didn't really test the script ! But if you want to keep "-new" string then you can also simply add "--" after grep options :

#!/usr/bin/sh
DIR="mydir"
STRING="-new"
for VAR in $(ls $DIR 2>&- | grep -v -- "$NEW"$)
do
cp $VAR $VAR$STRING
done

Regards.
It works for me (┬й Bill McNAMARA ...)
john korterman
Honored Contributor

Re: script for copying and renaming files

Hi Jean-Phelix,
you are of course right. However, the reason why I added your script in full was that I suggested two other changes to it.

regards,
John K.
it would be nice if you always got a second chance
Jean-Louis Phelix
Honored Contributor

Re: script for copying and renaming files

Ooooops ... Last mistake in the pattern ! The ending $ should be inside.

#!/usr/bin/sh
DIR="mydir"
STRING="-new"
for VAR in $(ls $DIR 2>&- | grep -v -- "$NEW$")
do
cp $VAR $VAR$STRING
done

Regards.
It works for me (┬й Bill McNAMARA ...)
john korterman
Honored Contributor

Re: script for copying and renaming files

Hi again,
I was actually more thinking about the "$NEW" variable and about to be in the correct location...

regards,
John K.
it would be nice if you always got a second chance
vasundhara
Frequent Advisor

Re: script for copying and renaming files

Why can't we write rm *-new as the first statement of the script and then proceed to copy the existing files to _new files?

Regards
VJ
Wodisch
Honored Contributor

Re: script for copying and renaming files

Hi,

you like living dangerously, all of you, don't you?

Nobody test for the extistance of the "new" filename :-(

So you may cause a lot of loss!

I would try to be more carefull (but then, it's not my data):

#!/bin/sh
if [ -d "$1" ]
then echo "no DIR to start from" 1>&2
exit 1
fi
cd "$1"
ls -1 | # could be MANY files!
while read oldname
do new="${oldname}-new"
if ls "$new" > /dev/null
# could already exist as:
# file/directory/blockdev/chardev/pipe/symlink/socket
then echo "${oldname} cannot be renamed: ${new} already exists!" 1>&2
exit 2
else if mv "$oldname" "$new"
then : #ok!
else echo "mv $oldname $new did NOT work!" 1>&2
exit 3
fi
done
# end

this may still have typos in , but can you see my point?
Be careful, check for errors!

FWIW,
Wodisch
john korterman
Honored Contributor

Re: script for copying and renaming files

Hi Wodisch,
I can of course only speak for myself, as I single-handledly stole Jean-Louis' script. The simple answer is that my interpretation of the demands for the script is different from yours.
It is for instance required that the files be copied, not moved. It is also said that:

I want the new file to be overwritten.

Therefore, I did not think that the script should avoid overwriting existing files that have the "copied extension".
I imagined a scenario where the files in the directory that did not have the "copied extension" were daily overwritten/updated by some application, and then - at a certain point in time - the files should be copied by the script, in order to create a "daily copy of things as they were at that particular point in time". I thought that made good sense, as there afterwards should be made some search and replacement in the "daily copied" files.
But it is only my interpretation and I could very well be wrong - unfortunately, a situation that would not be totally new to me.

regards,
John K.
it would be nice if you always got a second chance