Find the answer to your Linux question:
Results 1 to 7 of 7
Hi there, I need to merge the contents of two folders. Each folder contains about 650 files, with duplicate names. I was thinking my scritp should look somehting like this: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    6

    merging two files


    Hi there,
    I need to merge the contents of two folders. Each folder contains about 650 files, with duplicate names.

    I was thinking my scritp should look somehting like this:

    Code:
    #!/bin/bash
    cd /dir1  #source
    for name in `ls`
    do
    
    cat $name >> /dir2/$name
    What do you think?
    Thanks,
    Nett

  2. #2
    Linux Engineer
    Join Date
    Apr 2005
    Location
    Buenos Aires, Argentina
    Posts
    908
    cp -R directory1/ directory2/

    Isn't that easier?

    serzsite.com.ar
    "All the drugs in this world won\'t save you from yourself"

  3. #3
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    So if some_file exists both in dir1 and dir2 you want to append
    the contents of dir1/some_file to the end of dir2/some_file ,
    is that right ?
    In that case , yes it looks like your script should work.But if some
    of the file names start with a dot , then ls won't list them.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer
    Join Date
    Apr 2005
    Location
    Buenos Aires, Argentina
    Posts
    908
    If you have files that start with dots, just use ls -a instead.

    serzsite.com.ar
    "All the drugs in this world won\'t save you from yourself"

  6. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by serz
    If you have files that start with dots, just use ls -a instead.

    No, use "ls -A" otherwise you'll be copying your current and parent directories.

  7. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Why do people use
    Code:
    for name in `ls`
    when they can use the shell's filename matching by
    Code:
    for name in *
    As noted elsewhere, neither version will match dotted file names.

  8. #7
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Quote Originally Posted by scm
    Why do people use
    Code:
    for name in `ls`
    when they can use the shell's filename matching by
    Code:
    for name in *
    Perhaps you should start a thread on this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •