如何按字母顺序排列的bash合并文件 [英] How to merge files in bash in alphabetical order

查看:194
本文介绍了如何按字母顺序排列的bash合并文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要合并一大堆的mp3文件在一起。我知道,简单地做

I need to merge a bunch of mp3 files together. I know that simply doing

cat file1.mp3 >> file2.mp3

似乎很好地工作(至少回放正确对我的Zune反正)。

seems to work fine (at least it plays back correctly on my Zune anyway).

我想运行

cat *.mp3 > merged.mp3

但由于周围有50个单独的MP3文件,我不希望感到惊讶的位置错误的文件一半(这是一个音频的书,我不觉得自己重新翻录)。

but since there are around 50 separate mp3 files I don't want to be surprised halfway through by a file in the wrong spot (this is an audio book that I don't feel like re-ripping).

我通过猫眼看人页阅读,如果通配符的顺序定义找不到。

I read through the cat man pages and couldn't find if the order of the wildcard operator is defined.

如果不适用于这方面的工作,有一个简单的方法(可能使用 LS 的xargs )也许能为我做到这一点?

If cat doesn't work for this, is there a simple way (perhaps using ls and xargs) that might be able to do this for me?

推荐答案

您的版本(猫* .MP3> merged.mp3 )应该工作如你所期望。在 * MP3 是由外壳扩展,并会按字母顺序排列。

Your version (cat *.mp3 > merged.mp3) should work as you'd expect. The *.mp3 is expanded by the shell and will be in alphabetical order.

猛砸参考手册

词的拆分之后,除非-f选项已定,猛砸扫描字符每个单词'*','?'和'['。如果出现这些字符之一,那么这个词被视为一种模式,和与文件名的字母顺序排序列表取代匹配模式。

After word splitting, unless the -f option has been set, Bash scans each word for the characters ‘*’, ‘?’, and ‘[’. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.

不过,千万要注意,如果你有很多文件(或长文件名),你会在参数列表受阻太长的错误。

However, do be aware that if you have many files (or long file names) you'll be hampered by the "argument list too long" error.

如果发生这种情况,使用找到而不是:

If that happens, use find instead:

find . -name "*.mp3" -maxdepth 0 -print0 | sort -z | xargs -0 cat > merged.mp3

-print0 选项找到使用一个空字符作为字段分隔符(妥善处理文件名包含空格,如与MP3文件常见),而 -z 排序 -0 的xargs 通知替代分离的方案。

The -print0 option in find uses a null character as field separators (to properly handle filenames with spaces, as is common with MP3 files), while the -z in sort and -0 in xargs informs the programs of the alternative separator.

奖金特点:的离开了 0 -maxdepth 也包括文件子目录

Bonus feature: leave out -maxdepth 0 to also include files in sub directories.

然而,合并的MP3文件会弄乱的信息,如ID3标题和时间信息的这种方法。这将影响到更多的挑剔玩家的可玩性如iTunes(也许?)。

However, that method of merging MP3 files would mess up information such as your ID3 headers and duration info. That will affect playability on more picky players such as iTunes (maybe?).

要做到正确,请参阅一个更好的方式来无损加入MP3文件或者 什么是合并MP3文件的最好方法?

To do it properly, see "A better way to losslessly join MP3 files" or " What is the best way to merge mp3 files?"

这篇关于如何按字母顺序排列的bash合并文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆