此命令:
find buffer/ | grep -i $varname
返回文件名列表。我想将它们每个都压缩成单独的zip文件,然后
output/
(但每个文件的名称均与原始文件相同,但扩展名应为.zip
)find buffer/ | grep -i $varname
的文件)我已经为此撞了20分钟,这是我能想到的最好的方法:
find buffer/ | grep -i $varname -exec zip "output/"'{}'.zip '{}' \;
答案 0 :(得分:1)
看起来像xargs的工作:
b0.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
File dest = Environment.getExternalStorageDirectory();
InputStream in = getApplicationContext().getResources().openRawResource(R.raw.teagachas);
try
{
OutputStream out = new FileOutputStream(new File(dest, "lastshared.mp3"));
byte[] buf = new byte[1024];
int len;
while ( (len = in.read(buf, 0, buf.length)) != -1)
{
out.write(buf, 0, len);
}
in.close();
out.close();
}
catch (Exception e) {}
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().toString() + "/lastshared.mp3"));
share.setType("audio/*");
getApplicationContext().startActivity(Intent.createChooser(share, "compartiendo \"" + b0.getText() + "\""));
return true;
}
});
对于find buffer/ | grep -i "$varname" | xargs -n1 -I{} -- zip "output/{}.zip" {}
和这样创建的文件夹varname=""
:
buffer
该命令将执行:
mkdir buffer
touch buffer/1 buffer/2
可能您想从文件名中删除zip output/buffer/1.zip buffer/1
zip output/buffer/1.zip buffer/2
部分,我们可以使用buffer
。我们可以指示find仅列出sed 's#^buffer/##'
的文件。因此,以下内容:
-type f
将执行:
find buffer/ -type f | grep -i "$varname" | sed 's#buffer/##' | xargs -n1 -I{} -- zip "output/{}.zip" buffer/{}
要删除原始文件,我们可以执行以下操作:
zip output/1.zip buffer/1
zip output/2.zip buffer/2
如果您要遍历文件并且做得更高级,可以使用while read line:
find buffer/ -type f | grep -i "$varname" | sed 's#buffer/##' | xargs -n1 -I{} -- bash -c 'zip "output/{}.zip" buffer/{}; rm "buffer/{}"'