Re: file renames - (160 Views)
Reply
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 1 of 10 (160 Views)
Accepted Solution

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.

Please use plain text.
Honored Contributor
RAC_1
Posts: 5,920
Registered: ‎03-21-2002
Message 2 of 10 (160 Views)

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
Please use plain text.
Honored Contributor
Duncan Edmonstone
Posts: 5,678
Registered: ‎08-05-2000
Message 3 of 10 (160 Views)

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
Please use plain text.
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 4 of 10 (160 Views)

Re: file renames -

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

Thanks for the PID idea.
Please use plain text.
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 5 of 10 (160 Views)

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

Please use plain text.
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 6 of 10 (160 Views)

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!!
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 7 of 10 (160 Views)

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
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 8 of 10 (160 Views)

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
Please use plain text.
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 9 of 10 (160 Views)

Re: file renames -

Thanks Sri. I will change the for loop for a while statement.
Please use plain text.
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 10 of 10 (160 Views)

Re: file renames -

Thanks guys..
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation