1752356 Members
6303 Online
108787 Solutions
New Discussion юеВ

Re: file renames -

 
SOLVED
Go to solution
rmueller58
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 9
RAC_1
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

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

I am an HPE Employee
Accept or Kudo
rmueller58
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.
rmueller58
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

rmueller58
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!!
Sridhar Bhaskarla
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
Sridhar Bhaskarla
Honored Contributor
Solution

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
rmueller58
Valued Contributor

Re: file renames -

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

Re: file renames -

Thanks guys..