Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge files in bash in alphabetical order

Tags:

bash

ls

cat

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

cat file1.mp3 >> file2.mp3

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

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).

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

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?

like image 617
javanix Avatar asked Aug 24 '11 13:08

javanix


People also ask

How do I merge two sorted files in Linux?

All you have to do is list all the files that you wish to merge into a single file along with the new file name you wish to create. If a file with the name of the final output already exists, then it will be overwritten by the one being created. Here is a very simple implementation of cat command.

How do I merge 3 files in Linux?

Type the cat command followed by the file or files you want to add to the end of an existing file. Then, type two output redirection symbols ( >> ) followed by the name of the existing file you want to add to.

How do I combine multiple text files into one in Linux?

If you want to combine multiple large files, then instead of specifying each file's name to be concatenated, use the wildcards to identify these files, followed by an output redirection symbol. Hence, it is possible to concatenate all the files in the current directory using an asterisk (*) symbol wildcard as: cat *.


2 Answers

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.

From the Bash Reference Manual:

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

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.

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


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?).

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

like image 152
Shawn Chin Avatar answered Sep 20 '22 08:09

Shawn Chin


try:

ls | sort | xargs cat > merged.mp3

(Anyway I'm not sure that you can merge mp3 files that way)

like image 29
Heisenbug Avatar answered Sep 20 '22 08:09

Heisenbug