سؤال

I'm trying to copy files from one directory to another and append current date to this filename. The script look like this

#!/bin/bash

echo 'Move to homedir'
cd $HOME

echo 'Copy .txt files'
NOW=$(date +"%d%m%Y")

for FILENAME in *.txt
do
        cp "${FILENAME}" "/newdir/${FILENAME}${NOW}"
done

This generates an error because date is appended after file extension, like this

file1.txt10082013

How to avoid that?

هل كانت مفيدة؟

المحلول

Try extracting the extension and renaming the file:

NAME="${FILENAME%.*}"
EXT="${FILENAME##*.}"

cp "${FILENAME}" "/newdir/${NAME}${NOW}.${EXT}"
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top