bash 如何按字母顺序合并bash中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7176572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge files in bash in alphabetical order
提问by javanix
I need to merge a bunch of mp3 files together. I know that simply doing
我需要将一堆 mp3 文件合并在一起。我知道只是做
cat file1.mp3 >> file2.mp3
seems to work fine (at least it plays back correctly on my Zune anyway).
似乎工作正常(至少它无论如何都能在我的 Zune 上正确播放)。
I'd like to run
我想跑
cat *.mp3 > merged.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).
但由于有大约 50 个单独的 mp3 文件,我不想在中途被错误位置的文件感到惊讶(这是一本我不想重新翻录的有声读物)。
I read through the cat man pages and couldn't find if the order of the wildcard operator is defined.
我通读了 cat 手册页,但找不到通配符运算符的顺序是否已定义。
If catdoesn't work for this, is there a simple way (perhaps using lsand xargs) that might be able to do this for me?
如果cat对此不起作用,是否有一种简单的方法(也许使用lsand xargs)可以为我做到这一点?
回答by Shawn Chin
Your version (cat *.mp3 > merged.mp3) should work as you'd expect. The *.mp3is expanded by the shell and will be in alphabetical order.
您的版本 ( cat *.mp3 > merged.mp3) 应该能如您所愿。在*.mp3由外壳扩展,并会按字母顺序排列。
From the Bash Reference Manual:
来自Bash 参考手册:
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 namesmatching the pattern.
分词后,除非设置了 -f 选项,否则 Bash 会扫描每个单词以查找字符 '*'、'?' 和 '['。如果出现这些字符中的一个,则将该单词视为一个模式,并替换为与该模式匹配的按字母顺序排序的文件名列表。
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 findinstead:
如果发生这种情况,请find改用:
find . -name "*.mp3" -maxdepth 0 -print0 | sort -z | xargs -0 cat > merged.mp3
The -print0option in finduses a null character as field separators (to properly handle filenames with spaces, as is common with MP3 files), while the -zin sortand -0in xargsinforms the programs of the alternative separator.
该-print0选件find使用一个空字符作为字段分隔符(妥善处理与空格的文件名,如与MP3文件的常见),而-z在sort和-0中xargs通知的替代分离的方案。
Bonus feature:leave out -maxdepth 0to also include files in sub directories.
额外功能:不-maxdepth 0包括子目录中的文件。
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 文件的方法会弄乱诸如 ID3 标题和持续时间信息之类的信息。这将影响更挑剔的播放器的可玩性,例如 iTunes(也许?)。
To do it properly, see "A better way to losslessly join MP3 files" or " What is the best way to merge mp3 files?"
要正确执行此操作,请参阅“无损合并 MP3 文件的更好方法”或“合并 mp3 文件的最佳方法是什么?”
回答by Heisenbug
try:
尝试:
ls | sort | xargs cat > merged.mp3
(Anyway I'm not sure that you can merge mp3 files that way)
(无论如何,我不确定您是否可以通过这种方式合并 mp3 文件)

