BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Showing results for 
Search instead for 
Do you mean 

file renames -

SOLVED
Go to Solution
Valued Contributor

file renames -

I have a vendor process that creates output with random file names,
I am wanting to put some organization to it, consolidating this stuff into
one location after it's been used. I've have problems in the past with duplicate
files names, I think I may be on to a creation but want to run it by the scripts
gurus.

Here is what I am thinking, see if I am full of it.

#!/bin/sh

for fn1 in `ls -la |awk '{print $4}'
do
export fn2=`date +%Y%j%H%M%S`$fn1.ext
mv $fn1 $fn2
done

By doing this I am retaining the original name in the file name, but, creating a uniq file based on the "date stamp"

Anyone with a better way? or suggestions appreciated.

9 REPLIES
Honored Contributor

Re: file renames -

You are doing right. you may want to add $$ (shell pid) to the file name.

Anil
There is no substitute to HARDWORK
Honored Contributor

Re: file renames -

Seems reasonably OK to me - only point I'd make is that if you have a LOT of files, you might blow out the max number of arguments useable by the shell (256 I think?). Also I don't think you want to include directories which ls -a will show ( and straight ls if this is run as root). A better way might be:

#!/bin/sh

find . -type f | while read fn1
do
export fn2=$(date +%Y%j%H%M%S)${fn1}.ext
mv ${fn1} ${fn2}
done

This would mean being careful about subdirectories though.

HTH

Duncan

HTH

Duncan
Valued Contributor

Re: file renames -

I can see I will need to make a copy of some sacrificial files.. Whoa!!

Thanks for the PID idea.
Valued Contributor

Re: file renames -

Duncan,

The file they build has an extension of ".saf" I was thinking to avoid the directories" i'd change my ls -la to add
ls -la *.saf

Valued Contributor

Re: file renames -

Found a problem, several of the files have spaces in the file name. God I hate vendors and users that follow no programming consistency.

Brother.

Several of the file were named like:

November balance sheet.saf
September 10 posting.saf

ARGGH!!
Honored Contributor

Re: file renames -

Hi,

Instead of 'for' try 'while'. In case if the filenames have spaces in them, 'for' wont' work

ls -1 | while read
do

mv "$fn1" "$fn2"
done

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

Re: file renames -

Geezz.. it's me..

ls -1 | while read

should have been

ls -1 | while read fn1

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

Re: file renames -

Thanks Sri. I will change the for loop for a while statement.
Highlighted
Valued Contributor

Re: file renames -

Thanks guys..