How to write content of several files into a single one in Unix?
up vote
0
down vote
favorite
Suppose in a directory there are some files like:
file1.txt
file2.txt
file3.txt
file4
file5
fab
text1
I need to eliminate the files with the .txt
extension and append the content of the remaining files having a file name started with file(file4
, file5
) to a single file.
I tried the below command, but it appends all 5 file content to a single file.
ls -ltr file*|grep -vE ".txt" | cat * > final
shell
add a comment |
up vote
0
down vote
favorite
Suppose in a directory there are some files like:
file1.txt
file2.txt
file3.txt
file4
file5
fab
text1
I need to eliminate the files with the .txt
extension and append the content of the remaining files having a file name started with file(file4
, file5
) to a single file.
I tried the below command, but it appends all 5 file content to a single file.
ls -ltr file*|grep -vE ".txt" | cat * > final
shell
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Suppose in a directory there are some files like:
file1.txt
file2.txt
file3.txt
file4
file5
fab
text1
I need to eliminate the files with the .txt
extension and append the content of the remaining files having a file name started with file(file4
, file5
) to a single file.
I tried the below command, but it appends all 5 file content to a single file.
ls -ltr file*|grep -vE ".txt" | cat * > final
shell
Suppose in a directory there are some files like:
file1.txt
file2.txt
file3.txt
file4
file5
fab
text1
I need to eliminate the files with the .txt
extension and append the content of the remaining files having a file name started with file(file4
, file5
) to a single file.
I tried the below command, but it appends all 5 file content to a single file.
ls -ltr file*|grep -vE ".txt" | cat * > final
shell
shell
edited Nov 21 at 21:36
Rui F Ribeiro
38.2k1475125
38.2k1475125
asked Mar 2 '16 at 14:05
user159194
1
1
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
If you have bash
available, you can use the following:
shopt -s extglob
cat !(*.txt) > final
Or using zsh
:
setopt extended_glob
cat ^*.txt > final
add a comment |
up vote
0
down vote
find . ! -name '*.txt' | xargs cat >> final
This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
– jordanm
Mar 2 '16 at 14:16
you can use find with -maxdepth 1 -type f parameters
– mazs
Mar 2 '16 at 14:22
That works, but not all versions offind
support-maxdepth
and OP did not specify his OS.
– jordanm
Mar 2 '16 at 14:23
The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
– mazs
Mar 2 '16 at 14:40
add a comment |
up vote
0
down vote
Try this
#!/bin/bash
find . -name '*.txt' -exec rm {} +
for f in file*
do
cat $f >> final_file
done
In one line without removing .txt
files but ignoring them for the cat
$ find . -name 'file*' ! -name '*.txt' -exec cat {} ; > final
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
If you have bash
available, you can use the following:
shopt -s extglob
cat !(*.txt) > final
Or using zsh
:
setopt extended_glob
cat ^*.txt > final
add a comment |
up vote
2
down vote
If you have bash
available, you can use the following:
shopt -s extglob
cat !(*.txt) > final
Or using zsh
:
setopt extended_glob
cat ^*.txt > final
add a comment |
up vote
2
down vote
up vote
2
down vote
If you have bash
available, you can use the following:
shopt -s extglob
cat !(*.txt) > final
Or using zsh
:
setopt extended_glob
cat ^*.txt > final
If you have bash
available, you can use the following:
shopt -s extglob
cat !(*.txt) > final
Or using zsh
:
setopt extended_glob
cat ^*.txt > final
answered Mar 2 '16 at 14:13
jordanm
29.8k28192
29.8k28192
add a comment |
add a comment |
up vote
0
down vote
find . ! -name '*.txt' | xargs cat >> final
This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
– jordanm
Mar 2 '16 at 14:16
you can use find with -maxdepth 1 -type f parameters
– mazs
Mar 2 '16 at 14:22
That works, but not all versions offind
support-maxdepth
and OP did not specify his OS.
– jordanm
Mar 2 '16 at 14:23
The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
– mazs
Mar 2 '16 at 14:40
add a comment |
up vote
0
down vote
find . ! -name '*.txt' | xargs cat >> final
This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
– jordanm
Mar 2 '16 at 14:16
you can use find with -maxdepth 1 -type f parameters
– mazs
Mar 2 '16 at 14:22
That works, but not all versions offind
support-maxdepth
and OP did not specify his OS.
– jordanm
Mar 2 '16 at 14:23
The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
– mazs
Mar 2 '16 at 14:40
add a comment |
up vote
0
down vote
up vote
0
down vote
find . ! -name '*.txt' | xargs cat >> final
find . ! -name '*.txt' | xargs cat >> final
answered Mar 2 '16 at 14:14
mazs
2,5521622
2,5521622
This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
– jordanm
Mar 2 '16 at 14:16
you can use find with -maxdepth 1 -type f parameters
– mazs
Mar 2 '16 at 14:22
That works, but not all versions offind
support-maxdepth
and OP did not specify his OS.
– jordanm
Mar 2 '16 at 14:23
The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
– mazs
Mar 2 '16 at 14:40
add a comment |
This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
– jordanm
Mar 2 '16 at 14:16
you can use find with -maxdepth 1 -type f parameters
– mazs
Mar 2 '16 at 14:22
That works, but not all versions offind
support-maxdepth
and OP did not specify his OS.
– jordanm
Mar 2 '16 at 14:23
The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
– mazs
Mar 2 '16 at 14:40
This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
– jordanm
Mar 2 '16 at 14:16
This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
– jordanm
Mar 2 '16 at 14:16
you can use find with -maxdepth 1 -type f parameters
– mazs
Mar 2 '16 at 14:22
you can use find with -maxdepth 1 -type f parameters
– mazs
Mar 2 '16 at 14:22
That works, but not all versions of
find
support -maxdepth
and OP did not specify his OS.– jordanm
Mar 2 '16 at 14:23
That works, but not all versions of
find
support -maxdepth
and OP did not specify his OS.– jordanm
Mar 2 '16 at 14:23
The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
– mazs
Mar 2 '16 at 14:40
The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
– mazs
Mar 2 '16 at 14:40
add a comment |
up vote
0
down vote
Try this
#!/bin/bash
find . -name '*.txt' -exec rm {} +
for f in file*
do
cat $f >> final_file
done
In one line without removing .txt
files but ignoring them for the cat
$ find . -name 'file*' ! -name '*.txt' -exec cat {} ; > final
add a comment |
up vote
0
down vote
Try this
#!/bin/bash
find . -name '*.txt' -exec rm {} +
for f in file*
do
cat $f >> final_file
done
In one line without removing .txt
files but ignoring them for the cat
$ find . -name 'file*' ! -name '*.txt' -exec cat {} ; > final
add a comment |
up vote
0
down vote
up vote
0
down vote
Try this
#!/bin/bash
find . -name '*.txt' -exec rm {} +
for f in file*
do
cat $f >> final_file
done
In one line without removing .txt
files but ignoring them for the cat
$ find . -name 'file*' ! -name '*.txt' -exec cat {} ; > final
Try this
#!/bin/bash
find . -name '*.txt' -exec rm {} +
for f in file*
do
cat $f >> final_file
done
In one line without removing .txt
files but ignoring them for the cat
$ find . -name 'file*' ! -name '*.txt' -exec cat {} ; > final
answered Mar 2 '16 at 15:00
tachomi
3,52731134
3,52731134
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f267083%2fhow-to-write-content-of-several-files-into-a-single-one-in-unix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown