BashScripts.org
http://bashscripts.org/forum/

rename all files in a directory with a date
http://bashscripts.org/forum/viewtopic.php?f=7&t=63
Page 1 of 1

Author:  jbsnake [ Tue May 17, 2005 7:44 pm ]
Post subject:  rename all files in a directory with a date

this script is good for log files...
Code:
#!/bin/bash
# syntax: appDate /home/bob/pictures `date +%b%d` (for current date)
# syntax2: appDate /home/bob/pictures 620 (for specified)
# takes the directory to run in
# and what to append to the front of the filename

# change to directory specified in command line
cd $1

# put the second command in a new variable
append=$2

# see if there is a space in the second command
# (i.e. check if it's using the 'date +format' way)
if echo $append | grep " "
then
   # if so execute the code with the ` `
   append=`$append`
fi

# get the current directory
dir=`pwd`

# check if the last character is a /
lastchar=${dir:${#dir}:1}
if [[ $lastchar != '/' ]]
then
   # if not, make it that way
   dir=$dir/
fi

# list files in directory to a file
ls > files.lst

# loop through the file using all filenames you added
until ! read Record
do
    if [[ $Record != 'files.lst' ]]
    then
        # rename the files in the directory to what you specified
        mv $dir$Record $dir$append$Record
    fi
done < files.lst

# remove temp file
rm files.lst

# change to the orginal directory you started in
cd ~-

ofcourse we can modulate this and use it for more things than adding dates at the beginning of file names :)

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com