- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: mass file move / rename -
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
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-28-2009 09:28 AM
тАО01-28-2009 09:28 AM
We have a consistent Naming convention
Currently we have files that are named based on
for each
We are upgrading to a new revision of the web application but need to create a script that will copy the file from one name to the "new name"
As we can select which directory we can place the file, we no longer require the
The vendor has added an iteration #, we are not using this so the value will always be '0'.
The New File Name string will strip off the
so the new file name would be as follows:
I need to take the existing files and copy them from our existing folder and place them with the new file name in the new subdirectory.
Any ideas on how best to handle this.. There are 13000 documents.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2009 09:59 AM
тАО01-28-2009 09:59 AM
Re: mass file move / rename -
for i in `ls -1 DISTID*`
do
filename=`echo $i | tr -d DISTID`
echo $i $filename
done
if echo command ok then replace echo by mv .
Hasan
- Tags:
- mv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2009 10:27 AM
тАО01-28-2009 10:27 AM
Re: mass file move / rename -
The "find" command will produce a list of filenames, one per line, without wasting time in sorting them.
Pipe that to a "while read FILENAME; do ... done" style loop.
Within the loop, you'll get the current filename (including the path) in $FILENAME.
There are various ways to split it into components; the "cut" command might be the easiest.
"basename $FILENAME | cut -d _ -f 1" outputs
"dirname $FILENAME" will output the directory path without the filename.
Capture the output into variables by using the "VARIABLE=$( commandline )" syntax.
Now you should have a series of variables you can use to assemble the necessary mv command within the loop. Consider using the -i option of cp so that you won't accidentally overwrite any files.
Test your script by prefixing the cp command line with "echo" so that it only displays what it would do.
When you're made sure it works correctly, remove the "echo" prefix, and start the script for real.
MK
- Tags:
- find
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2009 10:34 AM
тАО01-28-2009 10:34 AM
Re: mass file move / rename -
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1308418
For example (untested)
$ cd
$ mv
$ perl -e 'for (@ARGV) { $old=$_; if( /.*?_([^_]+)(.*)/) { $new=$1 . q(_0_) . $2; print qq(rename $old, $new\n)} }' *.tmp
Remove the print( \n) to make it real:
Hein.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2009 01:10 PM
тАО01-28-2009 01:10 PM
Re: mass file move / rename -
I am pretty perl and regex generally illiterate. Forgive me for responding slowly other issues came up.
I have a directory with files that have a naming convention as follows:
mpsplus_1234_1.ext
the new value should be
1234_0_1.ext
Basically in need to string the mpsplus_ from the front and insert a "0_ after the 1234. The zero is non-changing value but required by the database to index the document correctly.
for each of 13000 files ..
the consistent thing is the current mpsplus_
everything all things being equal we want to retain the 1234_"0_"1.ext
Based on your script Perl will issue an edit "-e" for values in @ARGV
Does ARGV assume files in the current working directory? or do I need to be more explicit.. I have copied some files into a test directory I will attempt with ..
DO I remove the entire
"print qq(rename $old, $new\n"
To see if it works? I got no output when I ran it as shown. is there a way to echo out a file per file change?
perl -e 'for (@ARGV) { $old=$_; if( /.*?_([^_]+)(.*)/) { $new=$1 . q(_0_) . $2; print qq(rename $old, $new\n)} }' *.tmp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2009 01:24 PM
тАО01-28-2009 01:24 PM
Solutionfor file in `ls -1 mpsplus_*`
do
field2=`echo $file | cut -d"_" -f2`
field3=`echo $file | cut -d"_" -f3|cut -d"." -f1`
ext=`echo $file|cut -d"." -f2`
newfile=${filed2}_0_${field3}.${ext}
echo ${file} ${newfile}
#mv ${file} ${newfile}
done
if the output of the echo is looking fine, just comment move the # sign in front of echo line and remove it from mv line.
Hope this helps
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2009 01:28 PM
тАО01-28-2009 01:28 PM
Re: mass file move / rename -
Get the following error:
-bash: /bin/ls: Argument list too long
Any thoughts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2009 01:45 PM
тАО01-28-2009 01:45 PM
Re: mass file move / rename -
I resolved the ls issue by going the "find" command
for file in `find . -name "mpsplus*" -print |awk '{print substr($1,3,30)}'`
do
field2=`echo $file | cut -d"_" -f2`; field3=`echo $file | cut -d"_" -f3|cut -d"." -f1`
ext=`echo $file|cut -d"." -f2`; newfile=${field2}_0_${field3}.${ext}
mv ${file} ${newfile}
done
Thanks all for the assistance!! Points assigned
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2009 01:47 PM
тАО01-28-2009 01:47 PM
Re: mass file move / rename -
(again, there should be a more elaborate way of doing this but for all intents and purposes, this should work)
ls -1 mpsplus_* >/tmp/filelist.txt
echo "QQQ" >> /tmp/filelist.txt
counter=0 # for visual entertainment purposes
file=dummy # initial value
while [ "$file" != "QQQ" ]
do
file=`head -1 /tmp/filelist.txt`
field2=`echo $file | cut -d"_" -f2`
field3=`echo $file | cut -d"_" -f3|cut -d"." -f1`
ext=`echo $file|cut -d"." -f2`
newfile=${filed2}_0_${field3}.${ext}
echo ${file} ${newfile}
#mv ${file} ${newfile}
(( counter=${counter}+1 )); echo ${counter}
cat /tmp/filelist.txt | sed -e "1,1d" > /tmp/filelist.tmp
mv /tmp/filelist.tmp /tmp/filelist.txt
done
on the last line, where it will read the filename QQQ, the mv command will give you a file not found error but it is not a problem, just cosmetic detail.
what this construct does is reads the top line, processes filename then deletas that line from the file and repeats until the file gets exhausted. The display of counter value will show you the progress and you can figure out how fast or slow it is going.
good luck
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2009 02:05 PM
тАО01-28-2009 02:05 PM
Re: mass file move / rename -
I resolved it with the find command, like you I am sure there are dozens of more elegant ways of doing things but it is doing the job. Thanks again all of you..
rexm