1

I am trying to compile CyanogenMod and in the vendor files I have to manually find same files and then copy them. In a folder, there are 400+ files and it is very hard to copy them one-by-one.

I was thinking if there is a way to select the common files in both folders? It'll make my life a lot easier.

4 Answers 4

1

What you're probably looking for is:

rsync -a --existing source/ destination

Here, the --existing option ensures that only files already present in the destination directory are copied from the source directory

-a makes the copying recursive and preserves symbolic links, special & device files, modification times, group, owner, and permissions

If you want to perform a dry-run to see what will be copied, use:

rsync -nav --existing source/ destination

In either case, notice there is no slash after specifying the destination directory.

0

On the command line you can use the MV command and some wildcards.

Move all the files in dirs subdirs to here

mv dir/*/* here   

Same but just files starting with prefix

mv dir/*/prefix* here
0

If you have to find the same files, you could also look at fdupes, and parse its output. It checks if two (or more) files are really, really the same, even if they do not have the same file name.

You can combine it with something that compares the filenames.

(Side note: Using fdupes to delete redundant files can be dangerous - e.g. with ripped DVDs. Each DVD track has a subtitle file and it's backup, which are identical but have a different filename suffix. VLC player won't play the DVDs if one of the files is missing. So I have to rebuild these files...)

2
  • But I want files which share the same name, not the same content. Is it possible in fdupes? May 30, 2016 at 12:58
  • No, fdupes only identifies identical files. There is no comparison of file names. In that case, I would go with TmTron's answer.
    – user258532
    May 31, 2016 at 11:56
0

solution:

diff -sqr dir1 dir2 | grep identical | awk '{print $2}'

explanation:

diffcan output differences by file names. Example

diff -sqr dir1 dir2

Files dir1/fileX.txt and dir2/fileY.txt differ
Only in dir1: griveTest.txt
Files dir1/Test.odt and dir2/Test.odt are identical

to get only the identical files use grep:

diff -sqr dir1 dir2 | grep identical 

Files dir1/Test.odt and dir2/Test.odt are identical

now to only get the filename use awk to get the 2nd column:

diff -sqr dir1 dir2 | grep identical | awk '{print $2}'

dir1/Test.odt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .