LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Append date to existing file name? (https://www.linuxquestions.org/questions/linux-software-2/append-date-to-existing-file-name-4175449765/)

miros84 02-12-2013 02:10 AM

Append date to existing file name?
 
I want to append the date to existing file name.

I was looking for and found that. It works, but it doesnot keep the existing name. I just want to put the date after the existing name when copy.

Code:

cp test.back "test.backup-"`date +"%d-%m-%Y"`
The problem is that in this case I know exact name of the file which is test.backup, but in other cases, I dont know the exact name because it is random name. Is there any way to append date when coping existing files with random names?

Script I have is: cp /test/ /backup
In folder /test I have several files with random names. I want to copy them in /backup and keep random names they have but append the date after their names.

bloodstreetboy 02-12-2013 05:46 AM

If you don't know the source file name, then what is your process to copy the files.
Is your files path in any text file so you are going to use cat command with cp to copy these files?
In this case you can use a shell script which adds the date after the file name?

Philip Lacroix 02-12-2013 08:14 AM

Hi,

some time ago I wrote a script for a similar task, and I used ls -1 to create a plain list of the files contained in the source directory. Then sed in a while loop reads the lines from the list until it's over, and the outputs are used to make a copy of each file, appending a date or whatever to its original name.

Code:

#!/bin/bash
#

SOURCE=~/source
TARGET=~/target
LIST=~/files.list
APPEND=`date +%d-%m-%Y`

cd "${SOURCE}"
ls -1 > "${LIST}"

FILE_NR=1
FILE_NAME=`sed -n "${FILE_NR}"p "${LIST}"`

while [[ "${FILE_NAME}" != "" ]]
do
        cp "${SOURCE}/${FILE_NAME}" "${TARGET}/${FILE_NAME}.${APPEND}"
        FILE_NR=$((${FILE_NR} + 1))
        FILE_NAME=`sed -n "${FILE_NR}"p "${LIST}"`
done

rm "${LIST}"

exit 0

[Edit] I wrote this script as a non-professional, it worked for me but could cause issues in particular cases. Please see the post below for some valuable advice. Thanks!

Kind regards,
Philip

unSpawn 02-12-2013 08:54 AM

You shouldn't rely on parsing 'ls' output in scripts. Please see http://mywiki.wooledge.org/BashPitfalls.
This should copy the source file preserving the relative path and append an ISO-style date:
Code:

#!/bin/bash --
# Set default behaviour:
LANG=C; LC_ALL=C; export LANG LC_ALL
# Set debug mode while testing:
set -vxe
SOURCE=~/source; TARGET=~/target; DATE=$(/bin/date +%Y%m%d)
cd "${SOURCE}" || exit 1
find . -type f -printf "%f+++%h\n"|while read ITEM; do
 FILE="${ITEM//+++*/}"; SRCPATH="${ITEM//*+++/}";
 DSTPATH=${SRCPATH:2}
 install -d "${TARGET}/${DSTPATH}"
 cp "${SRCPATH}/${FILE}" "${TARGET}/${DSTPATH}/${FILE}-${DATE}"
done
exit 0


Philip Lacroix 02-12-2013 10:26 AM

@unSpawn: Thank you! :hattip:

unSpawn 02-12-2013 11:02 AM

You're welcome. If you like shell scripting then you may like
Code:

function howto() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ"; }

and maybe follow posts by David the H. and other "shell scripting evangelists" (in the positive sense). You'll get an idea about shell scripting basics (and how many times even the simplest of rules get violated ;-p).

miros84 02-12-2013 03:44 PM

Hi unSpawn.
You are a guru man. I never will create such a hard script I think. I tested it and it works like a charm.

But let me make it more difficult? May I?

Instead of cp I used
Code:

rsync --ignore-existing -vr
and that confuse all. rsync copy allready copied files. Something goes wrong, because rsync copy the same file again. I think that's because of the $date?... Can you understand me? is there any fix for that?

unSpawn 02-12-2013 06:06 PM

Quote:

Originally Posted by miros84 (Post 4890202)
Instead of cp I used
Code:

rsync --ignore-existing -vr
and that confuse all. rsync copy allready copied files. Something goes wrong, because rsync copy the same file again. I think that's because of the $date?... Can you understand me? is there any fix for that?

Rsync is an efficient tool and unless you force it to it only copies what changed. Sure we could patch things but that would only be ineffective if you keep mixing cp and rsync. If you want something based on rsync then you should look at Back in Time, Flyback, TimeVault (or rsnapshot?) instead?

miros84 02-13-2013 01:56 AM

Well, I really like your script and I prefer to do it by cronjob instead of install new software. Rsync will copy everyday the same file and just will change the name with current day. That's because of --ignore-existing, right?

I use --ignore-existing because only that protect my backup of virus. A time ago, a virus encrypt my backup changeing the name from backup.back to backup.encrypt and delete backup.back.
Using --ignore-existing it will not affect to original name, but if I use option only to update the file, rsync will update the original name and encrypt part ot it. So my backups will be encrypted too. I dont know if you understand me.


All times are GMT -5. The time now is 09:50 PM.