Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I rename 2009-08-2009-08-13 to 2009-08-$my_var_till_2009-08-13 for multiple files? The 2009-08-13 portion changes with respect to the date of the file, so I need a way to grab that part of the filename. Could I use basename somehow?

share|improve this question
1  
on what OS? –  John T Sep 21 '09 at 0:55
    
I am running this on a Linux OS –  bisnez Sep 21 '09 at 0:59

4 Answers 4

If the file names are always in that date format, I think that something like this would work (in a script) - this is, of course, assuming that they are all in the same directory and '$my_var_till' is defined in the shell:

#!/bin/bash

cd <name_of_target_directory>

for i in `ls`
do
    yyyy=`echo $i | awk -F- '{print $3}`'
    mm=`echo $i | awk -F- '{print $4}'`
    dd=`echo $i | awk -F- '{print $5}'`

    mv $i "$yyyy-$mm-$my_var_till-$yyyy-$mm-$dd"
done

if '$my_var_till' is not defined in the shell, you could take it as a command line argument (of course you might want to add some error checking with the argument):

#!/bin/bash

my_var_till=$1
<continue on with code from above>
share|improve this answer

mmv can help:

If you ever have to rename or move multiple files under Linux, then the mmv shell command will make your life easy. The mmv utility will also come in handy for copying multiple files, appending content of one file to other files, as well as for creation of links.

share|improve this answer

This script will rename any file with a name like

this: 2009-08-2009-08-13
to:   2009-08-${my_var_till}_2009-08-13

For example, if my_var_till="whatever", and filename="2009-08-2009-08-13", then the result will be:

mv "2009-08-2009-08-13" "2009-08-whatever_2009-08-13"

Files with names that are not in that format will be skipped.

Try this:

#!/bin/bash

my_var_till="whatever"

for f in *
    # test if file is proper format
    echo "${f}"|grep -E '^[0-9]{4}-[0-9]{2}-[0-9]{4}-[0-9]{2}-[0-9]{2}$'>/dev/null
    if [ $? -eq 0 ]; then
        n=$(echo ${f}|sed -r 's/^(.{8})(.{10})$/'"\1${my_var_till}_\2/")
        mv "${f}" "${n}"
    fi
done
share|improve this answer

Linux:

obligatory 1-liner...

for i in `ls`;do echo $i | awk -F- '{print "mv "$1"-"$2"-"$3"-"$4"-"$5" "$1"-"$2"-""'"$my_var_till"'""-"$3"-"$4"-"$5}';done

expanded

#!/bin/bash
my_var_till=somevalue  # can replace with $1 to use command-line arg to script
for i in `ls`
     do echo $i | awk -F- '{print "mv "$1"-"$2"-"$3"-"$4"-"$5" "$1"-"$2"-""'"$my_var_till"'""-"$3"-"$4"-"$5}'
done

you could also use the rename command and supply a Perl regex.

assuming your variable is a variable you have set in the shell, which is what I think you're asking.

Windows:

rename 2009-08* 2009-08-%my_var_till%_*
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.