0

Editing and changing the verbiage to make it more clear - I have .dm4 files in both dirA and dirB. My reference folder is dirA and I want to list all the .dm4 files which are missing in dirA which are there in dirB and then post all missing .dm4s and along with the existing .dm4s of dir A, into directory C, thus making sure that I do not miss out on any .dm4s which might exist in my dirA and dirB.

closed as unclear what you're asking by Prvt_Yadav, Sparhawk, Thomas, jimmij, msp9011 Jun 3 at 6:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

0

Let's lay down some assumptions:

  • Your shell is bash
  • Your Directory A is dirA
  • Your Directory B is dirB
  • Your Directory C does not exist yet, but will be named dirC
  • All the files you care about exist in dirA or dirB or are possibly duplicated in those directories

The simplest way I can think of is to:

rm -rf dirC; mkdir dirC
cp -p dirB/*.dm4 dirC/
cp -p dirA/*.dm4 dirC/
diff <(ls dirA) <(ls dirB)

That will put all .dm4 files into dirC and produce a diff listing of the two directories.

If you need a higher level of detail than that, please edit your question to be more specific.

UPDATE:

Suppose you have these files:

$ ls -1 dir{A,B}/*
dirA/apple.dm4
dirA/banana.dm4
dirA/date.dm4
dirA/fig.dm4
dirB/apple.dm4
dirB/elderberry.dm4
dirB/fig.dm4
dirB/grape.dm4
dirB/huckleberry.dm4

Notice that some files are duplicated, some are only in dirA, some are only in dirB. Neither directory has a cherry.dm4 file, for what it's worth.

And also suppose you have a script foo.sh:

$ cat << EOF > foo.sh; chmod 755 foo.sh
#!/usr/bin/env bash
rm -rf dirC; mkdir dirC
cp -p dirB/*.dm4 dirC/
cp -p dirA/*.dm4 dirC/
diff <(ls dirA) <(ls dirB)
EOF

Running foo.sh produces:

$ ./foo.sh
2,3c2
< banana.dm4
< date.dm4
---
> elderberry.dm4
4a4,5
> grape.dm4
> huckleberry.dm4
$ ls -1 dirC/*
dirC/apple.dm4
dirC/banana.dm4
dirC/date.dm4
dirC/elderberry.dm4
dirC/fig.dm4
dirC/grape.dm4
dirC/huckleberry.dm4

The diff output tells you that the .dm4 files missing from dirA are elderberry, grape, and huckleberry. All of the files found in dirA or dirB are present in dirC.

  • Your assumptions are correct. However, can you tell me what your first command - rm -rf dir; mkdir dirC is doing? From my understanding you are creating directory C, but I am not sure why you are using rm-rf dir C. Isn't that command used to delete a directory. Sorry I am new to linux so getting confused with rm. – mywayz May 29 at 18:39
  • It's there in case you need to run the script twice for whatever reason. It ensures that dirC will always be present and empty when the script starts. – Jim L. May 29 at 18:51
  • So as I follow your commands which you shared with me, am I fine in saying that cp -p dirB/*.dm4 dirC/ - Will copy all .dm4 to dirC cp -p dirA/*.dm4 dirC/ - Will copy all .dm4 to dirC diff <(ls dirA) <(ls dirB) - Will find diff between both and B. But my actuall intention is to find the difference first and then save all the different .dm4 to directory c. My reference folder is dir a. So if I want to see what .dm4s don't exist there which exist in dirB and then save all those to dirC, how should I go about it? – mywayz May 29 at 19:13
  • If you want to find the difference between the two directories first, then put the diff line first in the script. Otherwise, your post is somewhat unclear. Please see the last paragraph of my answer. I suspect that if you clarify your question enough, it may become a duplicate of a question already asked on StackExchange. – Jim L. May 29 at 19:21
  • Hello Jim, I have updated my question. You have still answered it pretty well however, may be now with better verbiage you might be able to totally understand wha I really want to achieve and might help me with it better. Thanks – mywayz May 29 at 19:44
0

Copy all files of dirA to dirC

cp dirA/*.dm4 dirC

Then copy all files from dirB, skipping exising files in dirC

cp -n dirB/*.dm4 dirC/

Man Page for cp:

  -n, --no-clobber
         do not overwrite an existing file (overrides a previous -i
          option)

Note: -n checks for file names only, to check for versions, the-update option might be interesting.

  • Can you explain what do you mean by skipping existing files in dirC? You mean to see when I use "cp -n dirB/*.dm4 dirC/" command, then all my .dm4 files will be copied to directory C and will overwrite the same name files which came from directory A or will it not overwrite those files and create a 2nd copy incase it already existed in A? – mywayz May 31 at 15:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.