简体   繁体   English

Bz2 目录中的每个文件

[英]Bz2 every file in a dir

I am running centos and I have around 1,300 files in a folder that each need to be bzipped individually.我正在运行 centos,我在一个文件夹中有大约 1,300 个文件,每个文件都需要单独进行 bzip 压缩。 What would be the easiest way of handing this?处理这个问题的最简单方法是什么?

If all the files are in a single directory then:如果所有文件都在一个目录中,则:

bzip2 *

Is enough.足够的。 A more robust approach is:更稳健的方法是:

find . -type f -exec bzip2 {} +

Which will compress every file in the current directory and its sub-directories, and will work even if you have tens of thousands of files (using * will break if there are too many files in the directory).这将压缩当前目录及其子目录中的每个文件,即使您有数万个文件也能工作(如果目录中的文件太多,使用 * 会中断)。

If your computer has multiple cores, then you can improve this further by compressing multiple files at once.如果您的计算机有多个内核,那么您可以通过一次压缩多个文件来进一步改进这一点。 For example, if you would like to compress 4 files concurrently, use:例如,如果您想同时压缩 4 个文件,请使用:

find . -type f -print0 | xargs -0 -n1 -P4 bzip2

To bzip2 on a multi-core Mac, you can issue the following command (when you're inside the folder you want to bzip)要在多核 Mac 上使用 bzip2,您可以发出以下命令(当您在要 bzip 的文件夹中时)

find . -type f -print0 | xargs -0 -n1 -P14 /opt/local/bin/bzip2

This will bzip every file recursively inside the folder your terminal is in using 14 CPU cores simultaneously.这将在您的终端所在的文件夹中递归地 bzip 每个文件同时使用 14 个 CPU 内核。

You can adjust how many cores to use by editing您可以通过编辑来调整要使用的核心数

 -P14

If you don't know where the bzip2 binary is, you can issue the following command to figure it out如果你不知道 bzip2 二进制文件在哪里,你可以发出以下命令来弄清楚

which bzip2

The output of that command is what you can replace该命令的输出是您可以替换的内容

/opt/local/bin/bzip2

with

I have written below script to bzip2 files to another directory我已经将下面的脚本写到bzip2文件到另一个目录

#!/bin/bash
filedir=/home/vikrant_singh_rana/test/*

for filename in $filedir; do
name=$(basename "$filename" | sed -e 's/\.[^.]*$//')
bzip2 -dc $filename > /home/vikrant_singh_rana/unzipfiles/$name
done

my sample file name was like我的示例文件名就像

2001_xyz_30Sep2020_1400-30Sep2020_1500.csv.bz2

I was not able to get any direct command, hence made this.我无法获得任何直接命令,因此做了这个。 This is working fine as expected.这按预期工作正常。

从目录内部: bzip2 *

find PATH_TO_FOLDER -maxdepth 1 -type f -exec bzip2 -zk {} \;

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

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