A neat little bash trick

Discussion in 'Linux / Unix Discussion' started by ffreeloader, Nov 1, 2005.

  1. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    Have you ever wanted to copy all the files from one directory to another from the bash prompt but ended up copying the accompanying directory over to the destination directory too? The following script is a neat little trick for copying all the files from directory A to directory B but not placing directory B inside directory A as a subdirectory. You must have the necessary write permissions in the destination directory.

    There probably are other ways to do this but I haven't seen them so far. The "cp -r" command will copy the entire directory as a subdirectory to the destination directory.

    Here is how to run the script after you have used chmod to make it executable. "./whatever_you_named_the_script.sh /path/to/source_directory /path/to/destination_directory" (This is run without the quote marks of course). It will do all the rest for you. You must use the absolute path, i.e. the path must start with /.

    Hope you find it useful. I do.

     
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1
  2. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    Ooops. It appears I gave you the wrong copy of the script. I had played with it after I had it working and had a few of copies of it. Some worked and some didn't, and I gave you the wrong copy. What follows is the correct script. You will error out of the previous one. :oops: :oops: :oops:

     
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1
  3. moominboy

    moominboy Gigabyte Poster

    keep 'em coming freddy! i'm still bookmarking them!
     
    Certifications: ECDL
    WIP: A+
  4. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Er - surely a simpler way is to do this:

    cd A
    cp -r . B

    (This assumes you want to copy the *contents* of A to B.)

    Harry
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  5. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    Harry,

    Well, as I said, there was most likely another way to do it, and yours is better. I'm still learning my way around the bash shell and I have a lot to learn about how the "." works in a lot of situations.

    Thanks.
     
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1
  6. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    <grin>

    Wonderfully confusing isn't it!

    Treat '.' as a directory specification. Then in man cp the 'source file' is a directory, as mentioned under -R.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  7. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    I use man pages on a regular basis, but had only been introduced to the "." recently in bash scripting. It can be used in so many ways that it's confusing as to when to use it and what exact responses you'll get. To tell the truth I hadn't even considered it in this instance.

    The tar thing came from the Advanced Bash Scripting Guide, and was more an exercise in scripting than anything else. I figured there most likely was a way that had to be faster than that, as it was really resource intensive, but I hadn't put a much thought into it. I've just been focusing on the concepts in the guide for now as I figure once I get used to playing with them the real possibilities will begin to open up to me.
     
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.