Register
It is currently Sun Dec 21, 2014 3:53 pm

rename all files in a directory with a date


All times are UTC - 6 hours


Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Tue May 17, 2005 7:44 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
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 :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP