实现一个工具:通过符号链接减少重复文件,从而控制文件夹大小
How to do it...
#include <iostream> #include <fstream> #include <unordered_map> #include <filesystem> using namespace std; using namespace filesystem;static size_t hash_from_path(const path &p) { ifstream is {p.c_str(), ios::in | ios::binary}; if (!is) { throw errno; } string s; is.seekg(0, ios::end); s.reserve(is.tellg()); is.seekg(0, ios::beg); s.assign(istreambuf_iterator<char>{is}, {}); return hash<string>{}(s); }static size_t reduce_dupes(const path &dir) { unordered_map<size_t, path> m; size_t count {0}; for (const auto &entry : recursive_directory_iterator{dir}) {const path p {entry.path()}; if (is_directory(p)) { continue; } const auto &[it, success] = m.try_emplace(hash_from_path(p), p);if (!success) { cout << "Removed " << p.c_str() << " because it is a duplicate of " << it->second.c_str() << '\n'; remove(p); create_symlink(absolute(it->second), p); ++count; }} return count; }int main(int argc, char *argv[]) { if (argc != 2) { cout << "Usage: " << argv[0] << " <path>\n"; return 1; } path dir {argv[1]}; if (!exists(dir)) { cout << "Path " << dir << " does not exist.\n"; return 1; }const size_t dupes {reduce_dupes(dir)}; cout << "Removed " << dupes << " duplicates.\n"; }$ du -sh dupe_dir 1.1Mdupe_dir $ ./dupe_compress dupe_dir Removed dupe_dir/dir2/bar.jpg because it is a duplicate of dupe_dir/dir1/bar.jpg Removed dupe_dir/dir2/base10.png because it is a duplicate of dupe_dir/dir1/base10.png Removed dupe_dir/dir2/baz.jpeg because it is a duplicate of dupe_dir/dir1/baz.jpeg Removed dupe_dir/dir2/feed_fish.jpg because it is a duplicate of dupe_dir/dir1/feed_fish.jpg Removed dupe_dir/dir2/foo.jpg because it is a duplicate of dupe_dir/dir1/foo.jpg Removed dupe_dir/dir2/fox.jpg because it is a duplicate of dupe_dir/dir1/fox.jpg Removed 6 duplicates. $ du -sh dupe_dir 584Kdupe_dir
How it works...
There's more...
Last updated