LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-20-2011, 12:54 AM   #1
ananthkadalur
Member
 
Registered: Mar 2011
Posts: 38

Rep: Reputation: 0
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.
 
Old 07-20-2011, 12:59 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Is there a reason why you're not simply using rsync?


Cheers,
Tink
 
1 members found this post helpful.
Old 07-20-2011, 12:59 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
This should help

Code:
file=$(ls -lt|head -1)
cp $file /destn

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 ...

Last edited by chrism01; 07-20-2011 at 07:30 PM. Reason: Darn typo !! .. sigh
 
Old 07-20-2011, 02:19 AM   #4
ananthkadalur
Member
 
Registered: Mar 2011
Posts: 38

Original Poster
Rep: Reputation: 0
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:
Originally Posted by Tinkster View Post
Is there a reason why you're not simply using rsync?


Cheers,
Tink
 
Old 07-20-2011, 03:12 AM   #5
Leslie007
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Rep: Reputation: 2
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.
 
1 members found this post helpful.
Old 07-20-2011, 03:59 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by ananthkadalur View Post
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.
rsync works either from/to local and remote machines. You can do a local copy using rsync if the destination is a local path.

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
 
1 members found this post helpful.
Old 07-20-2011, 05:12 AM   #7
ananthkadalur
Member
 
Registered: Mar 2011
Posts: 38

Original Poster
Rep: Reputation: 0
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:
Originally Posted by colucix View Post
rsync works either from/to local and remote machines. You can do a local copy using rsync if the destination is a local path.

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
 
Old 07-20-2011, 07:29 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

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


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
[SOLVED] gcc recompiles c file (obj file in different location) freeindy Programming 2 07-11-2011 04:45 AM
scp truncate text file busy - copying file is not a running binary jetberrocal Linux - Server 3 06-24-2010 04:56 PM
strange (to me) behavior of file permissions when copying file warrob Linux - General 10 10-25-2009 06:13 AM
Copying the newest file in a directory to another location Transition Linux - General 2 01-14-2005 12:53 PM
Question on File System and File Copying hey_joe Linux - Newbie 2 11-05-2003 11:30 AM


All times are GMT -5. The time now is 02:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration