thefekete.net

$> :(){ :|:& };:

Bash script / function to append file names

In my previous post about appending file names, I made a one-off solution.

Here’s a bash function that you can throw in your .bashrc and use more easily:

#!/bin/bash

# Put this in your ~/.bashrc and use it like a script...
append_filename() {
    USAGE="append: Puts text between filename and extension"
    USAGE="$USAGE\nUsage: $0 APPENDAGE file1 file2 file3 ..."

    if [ "$#" == "0" ]; then
        echo -e "$USAGE"
        return 1  # exit kills shell this is called from
    fi

    # get appended string
    APPENDAGE=$1
    shift

    while (( "$#" )); do
        if [ -f "$1" ]; then  # don't rename directories
            mv "$1" "${1%.*}$APPENDAGE.${1##*.}"
        fi
        shift  # grab the next file in list
    done
}

Leave a Reply

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting