- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- script for copying and renaming files
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2003 10:44 PM
тАО01-30-2003 10:44 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2003 10:49 PM
тАО01-30-2003 10:49 PM
Re: script for copying and renaming files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2003 11:05 PM
тАО01-30-2003 11:05 PM
Re: script for copying and renaming files
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2003 11:52 PM
тАО01-30-2003 11:52 PM
Re: script for copying and renaming files
#cd /
#ls > file
#for i in `cat file`
do
mv $i $i-new
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2003 12:16 AM
тАО01-31-2003 12:16 AM
Re: script for copying and renaming files
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2003 02:01 AM
тАО01-31-2003 02:01 AM
Solutionit 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2003 02:34 AM
тАО01-31-2003 02:34 AM
Re: script for copying and renaming files
#!/usr/bin/sh
DIR="mydir"
STRING="-new"
for VAR in $(ls $DIR 2>&- | grep -v -- "$NEW"$)
do
cp $VAR $VAR$STRING
done
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2003 03:06 AM
тАО01-31-2003 03:06 AM
Re: script for copying and renaming files
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2003 03:13 AM
тАО01-31-2003 03:13 AM
Re: script for copying and renaming files
#!/usr/bin/sh
DIR="mydir"
STRING="-new"
for VAR in $(ls $DIR 2>&- | grep -v -- "$NEW$")
do
cp $VAR $VAR$STRING
done
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2003 03:26 AM
тАО01-31-2003 03:26 AM
Re: script for copying and renaming files
I was actually more thinking about the "$NEW" variable and about to be in the correct location...
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2003 11:27 PM
тАО02-02-2003 11:27 PM
Re: script for copying and renaming files
Regards
VJ
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-03-2003 04:42 PM
тАО02-03-2003 04:42 PM
Re: script for copying and renaming files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2003 01:32 AM
тАО02-04-2003 01:32 AM
Re: script for copying and renaming files
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.