Copy file only if newer


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy file only if newer
# 1  
Old 10-09-2018
Copy file only if newer

I only want the file copied if it is newer. But it still copies the file?

Code:
zip -u Ubuntu_Documents.zip ./*[.txt *.doc *.rtf *.html *.png *.pdf *.odt *.ods *.odg] 
cp -u Ubuntu_Documents.zip $DOCS_Backup/Ubuntu_Documents_`date +"%Y-%m-%d-%H-%M"`.zip

# 2  
Old 10-09-2018
try:
Code:
[[ Ubuntu_Documents.zip -nt $DOCS_Backup/Ubuntu_Documents_`date +"%Y-%m-%d-%H-%M"`.zip ]] && cp Ubuntu_Documents.zip $DOCS_Backup/Ubuntu_Documents_`date +"%Y-%m-%d-%H-%M"`.zip

Not sure if shell used supports it. It was not specified.
This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 10-09-2018
You need to be very fast to not copy your file - the -u, --update option, copies "only when the SOURCE file is newer than the destination file or when the destination file is missing". You'd need to copy twice within a minute to hit a file with the same name older than your actual one.
These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 10-09-2018
I use Bash as my shell.

Script is not working.

-rw-rw-r-- 1 andy andy 77896 Oct 9 22:15 Ubuntu_Scripts.zip

andy@7_/media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Script_Backups$ ls
Ubuntu_Scripts_2018-10-09-15-46.zip Ubuntu_Scripts_2018-10-09-22-15.zip
Ubuntu_Scripts_2018-10-09-20-00.zip Ubuntu_Scripts_2018-10-10-10-00.zip

Last edited by drew77; 10-10-2018 at 05:23 PM..
# 5  
Old 10-09-2018
Yes, the test utility operator -nt stands for newer than. If you check the man page for the test utility on your system or look for the term "conditional expression" on the man page for your shell, you'll find lots of useful tests that can be performed in your shell scripts. (Note that "conditional expression" might be all lowercase, all uppercase, or have the first letter of each word capitalized depending on which shell's man page you're reading.)

Most of the operators that are available with the [[ command (if your shell has a [[ command) are also available with the test utility invoked with:
Code:
test expression

or with:
Code:
[ expression ]

The test and [ utilities are available with all Bourne shell derived shells.
# 6  
Old 10-10-2018
A solution to the problem mentioned in post#3 - a timestamp file.
Code:
last_bu=$DOCS_Backup/last_backup
if [[ ! -f $last_bu ] || [[ Ubuntu_Documents.zip -nt $last_bu ]]
then
  cp Ubuntu_Documents.zip $DOCS_Backup/Ubuntu_Documents_`date +"%Y-%m-%d-%H-%M"`.zip &&
  >$last_bu
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

If condition to check one file newer than the other

Dear All, I'm new to unix scripting. I'm trying to write an utility script which has to check if one file is newer than another one. $3 $4 $5 $6 are files .txt. Help me please. for i in $3 $4 $5 $6 do if then echo "$1 is newer than $i" else echo "$i is newer than $1" fi (9 Replies)
Discussion started by: Manueldo
9 Replies

2. Shell Programming and Scripting

Perl script, replace file with newer file

Hello, Can you please help me one this: I have two servers: Server A and server B. Every day on 03.00AM in only one on these two servers (randomly)is generated one file, lets say file.txt. I want to copy this file also to the other server. I want to create a perl script that does... (2 Replies)
Discussion started by: arrals_vl
2 Replies

3. Shell Programming and Scripting

how to find a file then overwrite with a newer version

This should be a simple script, but can't find one with google search. I just need to find the file that is in many directories, then overwrite that file with a newer version i.e. find file.jar then overwrite with /root/file.jar All I get in searches is substitute text with new test inside... (1 Reply)
Discussion started by: haircat
1 Replies

4. Shell Programming and Scripting

How to script to find the newer date in a text file?

Hi, I have a text file, foo.txt, it looks something like below. In the file there is a line that gives the date in the form of: Mon Jun 15 11:09:31 2008. I need to find which date is the newest and then store certain details of that list data to another file. So, in this sample text file, I... (6 Replies)
Discussion started by: boolean2222
6 Replies

5. Shell Programming and Scripting

If condition to check one file newer than the other - first file name uncertain

Dear All, I'm new to unix scripting. I'm trying to write an utility script which has to check if one file is newer than another one. The condition is I dont know the full name of the first file. I searched google and this forum for any such examples, but couldn't find any or may be I would have... (9 Replies)
Discussion started by: achilles5
9 Replies

6. Shell Programming and Scripting

Copy newer files without create

I want the same effect as "cp -u" but without create missing files, it is possible ? ps: bash (2 Replies)
Discussion started by: mrxrsd
2 Replies

7. Solaris

To copy the files newer than specific date

Dear all, Can you help me in copying files newer than speciifc date Thanks in advance, Rajesh (3 Replies)
Discussion started by: RAJESHKANNA
3 Replies

8. Shell Programming and Scripting

copy only newer files? (xcopy equivalent)

Howdy folks. I have a problem - I'm sure the answer is very simple, but I can't work it out. I want to create a UNIX shell script that does what I've been doing in DOS batch files for years - that is, backing up files. By which I mean copying files from a source directory to a target... (4 Replies)
Discussion started by: Chomps
4 Replies

9. UNIX for Dummies Questions & Answers

tar --newer = tar --newer-mtime ?

Hi, I have the following question : As far as I know unix doesn't store file creation dates. Would that imply the following? tar -cvzf backup.tar --newer is equal to: tar -cvzf backup.tar --newer-mtime ? (1 Reply)
Discussion started by: jamesbond
1 Replies
Login or Register to Ask a Question