在find中使用pax或cpio 命令来实现将找到的所有文件压缩到一个压缩包

 

 

uote:
Originally Posted by   alister   View Post
Code:
find path -type f '*.txt' | pax -w > txtarchive.tar

Many tar implementations have an option that allows them to read a file list from a file, allowing for find's output to be redirected and then read. In my opinion, not a very elegant solution. If I recall correctly, BSD tar uses -I  and GNU tar uses -T . Like ps , tar is a ubiquitous utility whose usage, for anything non-trivial, varies a great deal between platforms of different lineage.

Regards,
Alister
Thank you, it worked with   pax    and   cpio    after the proper fixes.


Code:
find path -name '*.txt' -type f  | pax -w > txtarchive.tar



 

Code:
find path -name '*.txt' -type f | cpio -ov -H ustar -F txtarchive.tar

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C++的tar库来压缩多个文件一个tar文件。以下是一个简单的示例代码,可以将多个文件打包一个tar文件: ```c++ #include <iostream> #include <fstream> #include <string> #include <vector> #include <archive.h> #include <archive_entry.h> int main(int argc, char** argv) { // 检查参数是否正确 if (argc < 3) { std::cerr << "Usage: " << argv[0] << " <output_tar_file> <input_file_1> [<input_file_2> ...]" << std::endl; return 1; } // 打开tar文件 std::string tar_filename(argv[1]); struct archive* tar = archive_write_new(); archive_write_set_format_pax_restricted(tar); archive_write_open_filename(tar, tar_filename.c_str()); // 添加输入文件到tar文件 for (int i = 2; i < argc; i++) { std::string input_filename(argv[i]); // 获取文件信息 struct archive_entry* entry = archive_entry_new(); archive_entry_clear(entry); archive_entry_set_filetype(entry, AE_IFREG); archive_entry_set_pathname(entry, input_filename.c_str()); archive_entry_set_size(entry, -1); // 打开输入文件 std::ifstream input_file(input_filename, std::ios::binary); if (!input_file.is_open()) { std::cerr << "Failed to open input file: " << input_filename << std::endl; return 1; } // 将文件写入tar文件 archive_write_header(tar, entry); char buffer[4096]; while (input_file.read(buffer, sizeof(buffer)).gcount() > 0) { archive_write_data(tar, buffer, input_file.gcount()); } // 关闭输入文件 input_file.close(); archive_entry_free(entry); } // 关闭tar文件 archive_write_close(tar); archive_write_free(tar); return 0; } ``` 你可以将多个输入文件作为参数传递给该程序,例如: ``` ./compress_file my_archive.tar file1.txt file2.txt file3.txt ``` 该程序将创建一个名为`my_archive.tar`的tar文件,并将`file1.txt`、`file2.txt`和`file3.txt`添加到其

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值