简体   繁体   English

如何查找两台计算机上的文件/文件夹?

[英]How to find which files / folders are on both computers?

I have a folder called documentaries on my Linux computer. 我的Linux计算机上有一个名为documentaries的文件夹。
I have SSH access to seedbox (also Linux). 我有访问seedbox(也是Linux)的SSH访问权限。

How do I find out which documentaries I have in both computers? 如何找出我在两台计算机上有哪些纪录片?

On seedbox it's a flat file structure. 在seedbox上,它是一个平面文件结构。 Some documentaries are files, some are folders which contain many files, but all in same folder For example: 有些纪录片是文件,有些是包含许多文件的文件夹,但都位于同一文件夹中例如:

data/lions_botswana.mp4
data/lions serengeti/S01E01.mkv
data/lions serengeti/S01E02.mkv
data/strosek_on_capitalism.mp4
data/something_random.mp4

Locally structure is more organized 本地结构更有条理

documentaries/animals/lions_botswana.mp4
documentaries/animals/lions serengeti/S01E01.mkv
documentaries/animals/lions serengeti/S01E02.mkv
documentaries/economy/strosek_on_capitalism.mp4
documentaries/something_random.mp4

I am not looking for command like diff , I am looking for command like same (opposite of diff) if such command exists. 我不是在寻找像命令diff ,我正在寻找像命令same (DIFF的对面),如果这样的命令存在。

Based on the answer from Zumo de Vidrio, and my comment: 根据Zumo de Vidrio的回答和我的评论:

on one computer 在一台电脑上

  cd directory1/; find | sort > filelist1

on the other 在另一

  cd directory2/; find | sort > filelist2

copy them in one place an run: 将它们复制到一个地方运行:

  comm -12 filelist1 filelist2

or as a one liner: 或作为一个班轮:

ssh user@host 'cd remotedir/; find|sort' | comm -12 - <(cd localdir/; find|sort)

Edit: With multiple folders this would look as follows 编辑:对于多个文件夹,这将如下所示

on one computer 在一台电脑上

  cd remotedir/; find | sort > remotelist

on the other 在另一

  cd localdir/subdir1/; find > locallist1
  cd -;
  cd localdir/subdir2/; find > locallist2
  cd -;
  #... and so on
  sort locallist1 locallist2 > locallistall

copy them in one place an run: 将它们复制到一个地方运行:

  comm -12 remotelist locallistall

or as a (now very long) one liner: 或作为(现在很长)一个班轮:

ssh user@host 'cd remotedir/; find|sort' | comm -12 - <({cd localdir/subdir1/; find; cd -; cd localdir/subdir2/; find; cd -; cd localdir/subdir3/; find}|sort)

Export list of remote files to local file by: 将远程文件列表导出到本地文件:

ssh user@seedbox 'find /path/to/data -type f -execdir echo {} ";"' > remote.txt

Note: On Linux you've to use absolute path to avoid leading ./ or use with "$PWD"/data . 注意:在Linux上,您必须使用绝对路径来避免前导./或使用"$PWD"/data

Then grep the result of find command: 然后grep find命令的结果:

find documentaries/ -type f | grep -wFf remote.txt

This will display only these local files which also exist on remote. 这将仅显示远程上也存在的这些本地文件。

If you would like to generate similar list on local and compare two files, try: 如果您想在本地生成类似的列表并比较两个文件,请尝试:

find "$PWD"/documentaries/ -type f -execdir echo {} ';' > local.txt
grep -wFf remote.txt local.txt

However above methods aren't reliable, since one file could have a different size. 然而,上述方法不可靠,因为一个文件可以具有不同的大小。 If files would have the same structure, you could use rsync to keep your files up-to-date. 如果文件具有相同的结构,则可以使用rsync使文件保持最新。


For more reliable solution, you can use fdupes which can find all files which exist in both directories by comparing file sizes and MD5 signatures. 对于更可靠的解决方案,您可以使用fdupes ,通过比较文件大小和MD5签名,可以找到两个目录中存在的所有文件。

Sample syntax: 示例语法:

fdupes -r documentaries/ data/

However both directories needs to be accessible locally, so you can always use sshfs tool to mount the remote directory locally. 但是,这两个目录都需要在本地访问,因此您始终可以使用sshfs工具在本地安装远程目录。 Then you can use fdupes to find all duplicate files. 然后,您可以使用fdupes查找所有重复的文件。 It has also option to remove the other duplicates ( -d ). 它还可以选择删除其他重复项( -d )。

Copy the ls output of each Computer to a same folder and then apply diff over them: 将每台计算机的ls输出复制到同一文件夹,然后对它们应用diff

In your computer: 在您的计算机中:

ls -R documentaries/ > documentaries_computer.txt

In seedbox: 在种子箱中:

ls -R documentaries/ > documentaries_seedbox.txt

Copy both files to a same location and execute: 将两个文件复制到同一位置并执行:

diff documentaries_computer.txt documentaries_seedbox.txt

You can mount remote folder using sshfs , then you can use diff -r to find the differences between them. 您可以使用sshfs挂载远程文件夹,然后可以使用diff -r查找它们之间的差异。

Eg 例如

sshfs user@seedbox-host:/path/to/documentaries documentaries/
diff -rs /local/path/documentaries/animals documentaries/ | grep identical
diff -rs /local/path/documentaries/economy documentaries/ | grep identical

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM