need help with shell script to rename files

need help with shell script to rename files

Post by kfrisb » Wed, 03 Aug 2005 14:13:43



I am new to linux.  I need to rename files in one directory which are
save with a file naming convention of epoch time.csv.  I need to rename
them site_YYYYMMDD_HHMMSS.csv
 
 
 

need help with shell script to rename files

Post by John W. Krah » Wed, 03 Aug 2005 16:56:53



> I am new to linux.  I need to rename files in one directory which are
> save with a file naming convention of epoch time.csv.  I need to rename
> them site_YYYYMMDD_HHMMSS.csv

perl -MPOSIX -e'
for ( <*> ) {
    next unless /^(\d+)\.csv$/;
    rename $_, strftime "site_%Y%m%d_%H%M%S.csv", localtime $1
    }
'

John
--
use Perl;
program
fulfillment

 
 
 

need help with shell script to rename files

Post by William Jame » Wed, 03 Aug 2005 18:28:24



> I am new to linux.  I need to rename files in one directory which are
> save with a file naming convention of epoch time.csv.  I need to rename
> them site_YYYYMMDD_HHMMSS.csv

Using the modern Ruby language:

Dir.glob("*").each { |filename|
  next unless filename =~ /^(\d+)\.csv$/
  File.rename( filename,
    Time.at($1.to_i).strftime("site_%Y%m%d_%H%M%d.csv") )

Quote:}

 
 
 

need help with shell script to rename files

Post by William Jame » Wed, 03 Aug 2005 18:36:02




> > I am new to linux.  I need to rename files in one directory which are
> > save with a file naming convention of epoch time.csv.  I need to rename
> > them site_YYYYMMDD_HHMMSS.csv

> Using the modern Ruby language:

> Dir.glob("*").each { |filename|
>   next unless filename =~ /^(\d+)\.csv$/
>   File.rename( filename,
>     Time.at($1.to_i).strftime("site_%Y%m%d_%H%M%d.csv") )
> }

Correction:

    Time.at($1.to_i).strftime("site_%Y%m%d_%H%M%S.csv") )

 
 
 

need help with shell script to rename files

Post by kfrisb » Wed, 03 Aug 2005 21:22:48


Thank you for the replys I left out one important piece of information
I need to do this in bash script.  I found that I can convert the epoch
time back with date -d '1970-01-01 -utc 1122984993 sec' +"%Y%m%d_%H%M%S"
 
 
 

need help with shell script to rename files

Post by Stephane Chazela » Wed, 03 Aug 2005 22:59:50



Quote:> I am new to linux.  I need to rename files in one directory which are
> save with a file naming convention of epoch time.csv.  I need to rename
> them site_YYYYMMDD_HHMMSS.csv

[...]

With GNU tools:

find . -name '*.csv' -printf \
  '%p\0%h/site_%TY%Tm%Td_%TH%TM%TS.csv\0' | xargs -r0 -n2 echo mv

Remove the echo if that's OK.

--
Stephane

 
 
 

1. rename files and shell script

Can someone tell me what changes I need to make to make the following
script so that it will handle filenames with spaces?  It works great for
filenames without spaces (i.e., "New.TXT.file") but wont work on
filenames with spaces (i.e., "New TxT File").  

Thanks!!
-----------------------------
#!/bin/tcsh -f
foreach file ( * )
mv $file `echo $file | tr A-Z a-z`
end
------------------------------

2. sound in 2.1.105 is broken...?

3. Bash script to rename multiple files needed

4. Forward all requests to NCSA Server 1.5.2

5. need help creating 3 bourne shell script files

6. "rehash" for Bourne shell ?

7. need help on shell script to remove oldest file?

8. first version of ext2 tools for windows NT available.

9. NEWBIE: Help with script to rename files!

10. Help on Script to Rename File Please

11. Help--Script for renaming multiple files

12. Help using awk and bash script to copy and rename lots of index.htm files