Bash - #!/bin/sh - #!/bin/bash

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

Bash - #!/bin/sh - #!/bin/bash

#1 Post by RSH »

I want to make backups of favorite directories and did wrote a little GUI application that does it. Now i want to make the program not to overwrite existing backups - so i want to add date and time to the file names.

How do i grep, sed or whatever needed date and time out, into a format (variable etc) that i can add easily onto a path-/file name?

Thanks for the Help

RSH
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#2 Post by technosaurus »

current date and time or the date the file was created?
this will give the files date@time-filename

Code: Select all

NEWFILE=`ls -l $FILE |awk '{print $6 "@" $7 "-" $8 }'`
you may want to cp it with -p

Code: Select all

cp -p "$FILE" "$NEWFILE" && rm "$FILE"
this will only remove the file iff the copy is succesful and will not overwrite existing files with the same date stamp and name

btw for current time use

Code: Select all

NEWFILE="`date +%F@%T`-$FILE"
Last edited by technosaurus on Sun 11 Mar 2012, 07:27, edited 2 times in total.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#3 Post by RSH »

technosaurus wrote:current date and time or the date the file was created?
Current date and time is needed, but maybe others wish to get date and time of file? If you know the solution for both - hm - who knows, maybe someone else will be grateful for that?

But to me current time and date is needed.
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#4 Post by technosaurus »

the problem with using current date and time is that you may end up backing up files that are already backed up... so you end up with 1000s of copies of the same picture of a funny cat with different date stamps.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#5 Post by RSH »

Thank you very much!

This --> `date +%F` <-- is exactly what i needed. I just do create directories with that name and then save the files inside the directory. :D
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

Post Reply