copying only new file to other location
I am a fresher in shell script, I want to copy only new file in a directory to some other location. I am able to find new file using
"ls -ltrh | tail -1", it is showing new file. But I don't know how to add in the shell script to copy that new file to other location. Can anybody please help me this. |
Is there a reason why you're not simply using rsync?
Cheers, Tink |
This should help
Code:
file=$(ls -lt|head -1) EDIT: Argh, typo !!!didn't mean to put -l (lower L) switch. That's the one that shows total and curr/parent dirs. Sorry about that ... |
copying only new file to other location
Yes, there is a reason, because previous Linux admin have made shell script to take backup, but it is not storing on remote machine, & that daily backup is storing on local machine, & manually we are taking backup to remote machine using "scp". I read rsync this is very good tool. but right now we wanted to copy those tar ball also to remote machine. So in that /backup/daily_backup directory it should find the newone & copy to different machine using scp. So I am struggling regarding this.
As you said, I made a shell script as below #!/bin/bash cd /home/linux/temp file=$(ls -lt|head -1) cp $file /backup & ran the shell script, the out put as below #./check_move.sh cp: cannot stat `total': No such file or directory cp: cannot stat `12244': No such file or directory it is finding 1st line which is the size of that directory & not able to copy. for e.g plz see this below direcoty $pwd /home/linux/temp $ ls -ltrh total 12M -rw-r--r-- 1 linux linux 9.5M Apr 30 2010 boot.iso -rw-rw-r-- 1 linux linux 2.3M Jul 13 16:53 testdisk-6.12.linux26.tar.bz2 -rw-rw-r-- 1 linux linux 157K Jul 18 16:35 Screenshot.jpg -rw-rw-r-- 1 linux linux 46K Jul 18 16:42 hindu_god_ram.jpg -rw-rw-r-- 1 linux linux 16K Jul 19 10:24 mdstat.doc in above directory mdstat.doc is newone & I want to copy only that file, for that I made a shell script as below #!/bin/bash cd /home/linux/temp file=$(ls -ltr|tail -1) cp $file /backup and gave the excecute permission & ran that shell script, but it is not working. plz see the below output #./check_move.sh cp: invalid option -- w Try `cp --help' for more information. if I run as below from the terminal it is showing the new file. $ls -ltr|tail -1 -rw-rw-r-- 1 linux linux 15872 Jul 19 10:24 mdstat.doc but it is not copying when through shell script. So plz tell me how can I do this. I will be waiting for your kind reply. Quote:
|
Just give ,
ls -ltr|tail -2 (or) ls -ltr|tail -n 2 It will print last line of the output. But ls -ltr|tail -1 should work, I don't no why its not working. If anybody know this plzz let me know. |
Quote:
In any case, if you want to retrieve the file name using the ls command, don't use the option -l, since you don't need the detailed information about the file: Code:
cp "$(ls -rt | tail -1)" /backup |
copying only new file to other location
Thank you very much master
I tried in the shell script using cp "$(ls -rt | tail -1)" /destination, it is working. once again thank you very much for your quick reply. Regards Ananth Quote:
|
Aaah, typo in my orig post; didn't mean to put -l (lower L) switch. That's the one that shows total and curr/parent dirs. Sorry about that ...
|
All times are GMT -5. The time now is 04:29 PM. |