Linux Move/Rename file

Discussion in 'General Linux' started by captain747480, Jul 9, 2012.

  1. captain747480

    captain747480 New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I need to be able to strip the _DateStamp from a filename, use the resulting filename to create a directory and then move the file to the created directory without the Date Stamp in the filename.

    So 'blah_120709.rec' moves to a file 'blah.rec' in directory 'blah'.


    I've done some searching and found lots of posts about awk and sed but I'm afraid they're over my head.

    Could someone help me with a Linux script that will do this.

    Thanks in advance
  2. ddd-222

    ddd-222 New Member

    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    0
    try this:

    for i
    do
    base_name=`echo $i | sed -n 's/\(.*\)_[0-9]*\.rec/\1/p'`
    if [ -n "${base_name}" ]
    then
    mkdir ${base_name}
    mv $i ${base_name}/${base_name}.rec
    else
    echo "$i is bad"
    fi
    done



    from within a directory containing the files:
    script.sh *.rec
  3. captain747480

    captain747480 New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0

Share This Page