Merge Files in the Linux Command Line
Learn various ways of merging multiple files into another file in the Linux command line.
Got two or more files and need to merge them in a single file? The simplest way would be to use the cat command. After all, the cat command’s original purpose is to concatenate files.
Use the cat command to merge files in Linux
Merging two files is simple. You just have to append the filename to the cat command and that’s it:
As you can see, I used the cat command to show the contents of a file and then merged them.
But it won’t save any changes. To save those changes, you have to redirect the file contents to another file.
Remember, the > will override the contents of the file so I would recommend using a new file as the cat will create the file if it doesn’t exist.
So how can you modify the editing file while keeping the previous content intact?
Append changes to the existing file
To modify the existing file, you just have to use >> instead of single > and you’d be good to go.
For example, I will make edit the existing file File_3.txt by appending Hello.txt and Sagar.txt :
As you can see, it added new text in the end line while keeping the old content intact.
Automate the merging files using the loop in Linux
Here, I will be using for loop (till three iterations as I want to merge three files) also you can use > or >> as per your needs.
Use the sed command to merge files in Linux (temporary)
There are many times when you want to apply changes only for a specific time and in those cases, you can use sed.
Being a non-interactive way of editing files, the sed utility can be tremendously useful when used in the right way.
And when used with the h flag, it will hold the buffer temporarily:
Wrapping Up
This was my take on how you can merge files using the sed and cat command. And if you have any queries, leave us a comment.
How to merge all (text) files in a directory into one?
I’ve got 14 files all being parts of one text. I’d like to merge them into one. How to do that?
6 Answers 6
This is technically what cat ("concatenate") is supposed to do, even though most people just use it for outputting files to stdout. If you give it multiple filenames it will output them all sequentially, and then you can redirect that into a new file; in the case of all files just use ./* (or /path/to/directory/* if you’re not in the directory already) and your shell will expand it to all the filenames (excluding hidden ones by default).
Make sure you don’t use the csh or tcsh shells for that which expand the glob after opening the merged-file for output, and that merged-file doesn’t exist before hand, or you’ll likely end up with an infinite loop that fills up the filesystem.
The list of files is sorted lexically. If using zsh , you can change the order (to numeric, or by age, size. ) with glob qualifiers.
To include files in sub-directories, use:
Though beware the list of files is not sorted and hidden files are included. -type f here restricts to regular files only as it’s unlikely you’ll want to include other types of files. With GNU find , you can change it to -xtype f to also include symlinks to regular files.
With the zsh shell,
Would do the same ( (-.) achieving the equivalent of -xtype f ) but give you a sorted list and exclude hidden files (add the D qualifier to bring them back). zargs can be used there to work around argument list too long errors.
Concatenating multiple text files into a single file in Bash
What is the quickest and most pragmatic way to combine all *.txt file in a directory into one large text file?
Currently I’m using windows with cygwin so I have access to BASH.
Windows shell command would be nice too but I doubt there is one.
12 Answers 12
This appends the output to all.txt
This overwrites all.txt
Just remember, for all the solutions given so far, the shell decides the order in which the files are concatenated. For Bash, IIRC, that’s alphabetical order. If the order is important, you should either name the files appropriately (01file.txt, 02file.txt, etc. ) or specify each file in the order you want it concatenated.
The Windows shell command type can do this:
Type type command also writes file names to stderr, which are not captured by the > redirect operator (but will show up on the console).
You can use Windows shell copy to concatenate files.
To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format).
Be careful, because none of these methods work with a large number of files. Personally, I used this line:
EDIT: As someone said in the comments, you can replace $(ls | grep «.txt») with $(ls *.txt)
EDIT: thanks to @gnourf_gnourf expertise, the use of glob is the correct way to iterate over files in a directory. Consequently, blasphemous expressions like $(ls | grep «.txt») must be replaced by *.txt (see the article here).
Простые советы в примерах по PHP, MySQL, HTML, CSS, JS и LINUX
Простые советы разработки сайтов и приложений. Вы найдете ответы на вопросы, которые нельзя найти где-то ещё.
Объединить несколько файлов в один в Linux
- Получить ссылку
- Электронная почта
- Другие приложения
Для того, чтобы объединить несколько файлов в Linux, нужно воспользоваться командой cat.