LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-12-2013, 03:10 AM   #1
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Rep: Reputation: 31
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.

Last edited by miros84; 02-12-2013 at 03:13 AM.
 
Old 02-12-2013, 06:46 AM   #2
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201
Blog Entries: 3

Rep: Reputation: 37
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?
 
Old 02-12-2013, 09:14 AM   #3
radygrenek
Member
 
Registered: Jun 2012
Posts: 426

Rep: Reputation: Disabled
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

Last edited by radygrenek; 02-12-2013 at 11:32 AM.
 
2 members found this post helpful.
Old 02-12-2013, 09:54 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594
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
 
2 members found this post helpful.
Old 02-12-2013, 11:26 AM   #5
radygrenek
Member
 
Registered: Jun 2012
Posts: 426

Rep: Reputation: Disabled
@unSpawn: Thank you!

Last edited by radygrenek; 02-12-2013 at 11:42 AM.
 
Old 02-12-2013, 12:02 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594
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).
 
1 members found this post helpful.
Old 02-12-2013, 04:44 PM   #7
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
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?

Last edited by miros84; 02-12-2013 at 04:45 PM.
 
Old 02-12-2013, 07:06 PM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594
Quote:
Originally Posted by miros84 View Post
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?
 
Old 02-13-2013, 02:56 AM   #9
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compress and append date to a file using PHP Freddy49 Programming 5 11-05-2012 02:24 PM
how to append in a existing file descriptor in linux and also insert a new line. mot12 Programming 4 08-30-2011 08:48 AM
how to append contents of one file to another existing file amit_pansuria Programming 1 11-17-2008 11:01 AM
script to change file name and append the date john_551 Linux - Software 4 08-26-2006 07:56 AM
How to append today's date in a script anjaan Programming 4 06-12-2004 09:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration