LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-05-2009, 08:10 PM   #1
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Rep: Reputation: 32
How to copy/move files and skip...


How can I copy (would rather just move) files from one directory to another and NOT have it prompt me if I want to over write. I want the answer to always be no, so skip all files that already exist and only move those that do not.

I've tried the command

mv -u files destination

However, it STILL asks me if I want to overwrite the files or not. With 1000+ files that I move, I do not want to have to wait and answer "no" to over 90% of them.

However, I can't just overwrite the existing files.

The BEST way I would like to do this would be to;

if file exists in destination, delete source
if file doesn't exist in destination, move to destination

Last edited by Skillz; 07-05-2009 at 08:11 PM.
 
Old 07-05-2009, 08:16 PM   #2
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
Check out the yes command. It just pipes a series of strings (the same one). Use it like this: yes no | mv files destination
 
Old 07-05-2009, 08:19 PM   #3
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
yes no | mv files destination

will answer "no" to all the questions it ask?
 
Old 07-05-2009, 08:22 PM   #4
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by Skillz View Post
How can I copy (would rather just move) files from one directory to another and NOT have it prompt me if I want to over write. I want the answer to always be no, so skip all files that already exist and only move those that do not.

I've tried the command

mv -u files destination

However, it STILL asks me if I want to overwrite the files or not. With 1000+ files that I move, I do not want to have to wait and answer "no" to over 90% of them.

However, I can't just overwrite the existing files.

The BEST way I would like to do this would be to;

if file exists in destination, delete source
if file doesn't exist in destination, move to destination
You can use "rsync" to do this...It will only copy the files that don't exist on the destination...something like this...

Code:
root@host# rsync -av /path/to/source/ /path/to/destination/
OR you can do it via a script...you have to test to see if the file exists and only move if it dosen't...something like...

Code:
#!/bin/bash 
#
for file in $(ls -1 /path/to/source/)
do
  if [ ! -f /path/to/destination/${file} ] ; then
    mv /path/to/source/${file} /path/to/destination/      
  fi
done
But your results may vary depending what you want to do...

-C
 
Old 07-05-2009, 08:26 PM   #5
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
Yes. and that's all it does. The mv command will always get a no answer to a stdin request.
 
Old 07-05-2009, 08:26 PM   #6
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by custangro View Post
Code:
#!/bin/bash 
#
for file in $(ls -1 /path/to/source/)
do
  if [ ! -f /path/to/destination/${file} ] ; then
    mv /path/to/source/${file} /path/to/destination/      
  fi
else
  rm -Rf /path/to/source/${file}
done
-C
Would that work?
 
Old 07-05-2009, 08:32 PM   #7
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by Skillz View Post
Would that work?
Well the fact that you "moved" the file means that /path/to/source/${file} no longer exists...which means that you don't need to to rm it

FYI...the else needs to come before the fi

-C
 
Old 07-05-2009, 08:35 PM   #8
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by custangro View Post
Well the fact that you "moved" the file means that /path/to/source/${file} no longer exists...which means that you don't need to to rm it

FYI...the else needs to come before the fi

-C
Well what I mean is, if the file does already exist...what happens to the source?

I added that so that it would delete the file if it does exist on the destination.
 
Old 07-05-2009, 08:44 PM   #9
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by Skillz View Post
Well what I mean is, if the file does already exist...what happens to the source?

I added that so that it would delete the file if it does exist on the destination.
Then it would look something like this...

Code:
#!/bin/bash 
#
for file in $(ls -1 /path/to/source/)
do
  if [ ! -f /path/to/destination/${file} ] ; then
    mv /path/to/source/${file} /path/to/destination/
  fi   
  rm -Rf /path/to/source/${file}   
done
FYI, that's a number one in the ls command...NOT an l

-C

Last edited by custangro; 07-05-2009 at 08:46 PM.
 
Old 07-05-2009, 08:59 PM   #10
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
Ah ok, thanks I will try that out!!

I used the yes command just now, then just deleted the files when it was done. once more get uploaded, I'll test out the script.
 
Old 07-05-2009, 09:05 PM   #11
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
I just thought of something.

I know it's bad, very bad. However, I have to run this script as root. Therefore when it moves the files, it gives the ownership to root instead of the user that should own it.

Reason why this is, is because there are two different users that I am moving files between and can't get the groups to work because it messes up cPanel.

So how would I tell it to then chown user:user the files it moves?
 
Old 07-05-2009, 09:21 PM   #12
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
mv preserves the original gid and uid. Moving as root should NOT upset them.
 
Old 07-05-2009, 09:26 PM   #13
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
Ah ok I was thinking of the cp command, that actually makes root own the file it copied.
 
Old 07-05-2009, 09:30 PM   #14
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by Skillz View Post
I just thought of something.

I know it's bad, very bad. However, I have to run this script as root. Therefore when it moves the files, it gives the ownership to root instead of the user that should own it.

Reason why this is, is because there are two different users that I am moving files between and can't get the groups to work because it messes up cPanel.

So how would I tell it to then chown user:user the files it moves?
That gets a little tricky...You would have to do somthing like this...

Code:
#!/bin/bash
#
ls -l /path/to/source/ | awk '{print $3, $4, $NF}' | while read user group file
do
  if [ ! -f /path/to/destination/${file} ] ; then
    mv /path/to/source/${file} /path/to/destination/
    chown ${user}:${group} /path/to/destination/${file}
  fi   
  rm -Rf /path/to/source/${file}
done
 
Old 09-27-2009, 07:42 PM   #15
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
I tried the above script, but it deletes the directory.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete/move/copy files of specific date imsajjadali Red Hat 26 11-07-2013 11:34 PM
Cannot su to root successfully / Cannot copy/move *.bz2 files from Sosreport jbottiger Linux - Newbie 17 02-06-2009 09:28 AM
rsync syntax to skip directory, but copy select files.. tnicol Linux - Software 3 07-24-2007 05:40 AM
Command to copy files/folders but skip bad/corrupt files?? leemoreau Linux - Newbie 2 04-02-2007 02:27 PM
how to move and copy files.... JMK Linux - Newbie 3 01-22-2004 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:53 AM.

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