Thread: How to copy directories?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    How to copy directories?

    Im trying to copy a ton of files in one directory to another and im using linux-2.6.30
    i was wondering if there was a command or recursive way to say {yes} when it askes if you want to overwrite the existing file? i looked cp online and it says there should be a command --reply but im not really sure how to override the prompt that asking if u

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    uhmm.. and this is related to C how?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This will find its way to Tech Board soon enough.

    You should type "man cp" at your terminal and read the options. I don't have a "reply", but I have an "update".

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tabstop
    This will find its way to Tech Board soon enough.
    Right.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by kiros88 View Post
    i was wondering if there was a command or recursive way to say {yes} when it askes if you want to overwrite the existing file?
    I dunno...maybe the yes command?

    Edit:

    The yes man page is almost worthless. Have a look at this wiki for an idea of how it can be used.
    Last edited by kermit; 04-16-2010 at 03:45 PM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is one reason I recommend using mc as a file browser on linux. It's terminal based, which turns some people off, but

    1) it actually makes way more sense to have a file browser with shell/command line integration, rather than a GUI file browser with icon/drag and drop integration. Which the later remind me more of crib toys you would hang to help an infant learn to recognize objects rather than a tool a self-respecting programmer would want.

    2) GNU mc is still being developed, meaning it's like 20 years mature, and has way more to it than Nautilus, Konqueror, etc.

    Anyway, here's a screenshot of me copying directories from ~/C into a tmp directory to create an iso for a dvd backup. The view is split paned between the two directories. The bold white items are directories, you click on one, press F5, it is copied into the other panel. Recursively, all attributes preserved (if the filesystem type is the same, which it is), done.

    All distros have an mc package, either "yum search midnight" or "apt-cache search midnight" will find it (mc = midnight commander ).

    If you want to download and build:
    Midnight Commander

    Anyway, that's why I dunno how to do what you are saying off the top of my head, because I have been spoiled by mc and the fact that it's always there on any linux system and even works over ssh because it is terminal based.

    I've done shell scripts for this (actually I think it is doable just with cp switches), but unless you need to automate (which you could do in C anyway) it will only take 2 minutes to install mc, type "mc" in a terminal, and another 2 min to find <F5>, so that's the easy way. It asks you once if you want to overwrite, you click "all" and that's it.
    Last edited by MK27; 04-16-2010 at 02:02 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Supply the "-f" switch
    Code:
    cp -f <src> <dest>

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    cp -fvR /path/to/src /path/to/dst
    If you want to preserve file ownership, mode, and timestamp, give -p:

    Code:
    cp -fpvR /path/to/src /path/to/dst
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I am thinking kiros88's real problem is this:

    alias cp='cp -i'

    Most distros put this in ~/.bashrc. So your "cp" is by default a "cp -i", which means it asks for confirmation.

    Ie, it's not a switch you're missing, it's a switch you're (inadvertantly) using.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Actually in this case I would use rsync:

    Code:
    rsync -av /srcdir/foo /destdir/bar/
    If you want to see whats going on, add --progress. In any event, not only does this handle the recursion for you, it also handles file permissions and so on. Also if there is /src/foo.txt and /dest/foo.txt, it will only copy foo.txt if the src is newer than the destination. As an added bonus, if you are going over a network it can apply compression to the files as well as make use of ssh. Oh yeah and it also confirms the copy was good before moving on. rsync - Wikipedia, the free encyclopedia
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gcc can't find obvious copy constructor
    By SevenThunders in forum C++ Programming
    Replies: 13
    Last Post: 03-19-2009, 02:41 PM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Copy Constructor crashing program
    By bob2509 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 04:21 PM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM