Issue
I have some folders and subfoldes with .txt and other extensions (like .py, .html) and I want to concatenate all to one .txt file
I try this:
find . -type f -exec cat {} + > test.txt
Input:
txt1.txt:
aaaaa
test.py
print("a")
htmltest1.html:
<head></head>
Output:
aaaaaprint("a")<head></head>
Desired outup:
aaaaa
print("a")
<head></head>
So, how to modify this bash-command to get my desired output? I want to paste newline after each printed file
Solution
The problem is that the last lines of your files are not terminated with the newline character, which means they don't fulfill the POSIX definition of a text file, which may yield weird results like this.
Probably all graphical text editors I've used allow you not to put a terminating newline, and a lot of people won't put it, presumably because the editor makes it look like there's a redundant empty line at the end.
This may be the reason why some people couldn't reproduce your issue - presumably they created the sample files with well-behaving tools such as cat
or vim
or nano
, or they did put the newline characters at the end.
So here's the issue:
user@host:~$ find . -type f -exec cat {} \;
aaaaaprint("a")<head></head>user@host:~$
To avoid these sorts of problems in the future, you should always hit <enter>
after the last line of text in your file when using a graphical text editor. However, sometimes you have to work with files produced by other users, which might not know this sort of stuff, so:
here is a quick and dirty workaround (concatenating with an additional file which only contains the newline character):
user@host:~$ echo '' > /tmp/newline.txt
user@host:~$ find . -type f -exec cat {} /tmp/newline.txt \;
aaaaa
print("a")
<head></head>
user@host:~$
Answered By - Czaporka