Grep word within a file then copy the file
I have a collection of files ( *.zip, *.txt, *.tar.gz, *.doc, ...etc ). These files reside within a path. I want to find all the files ( *.txt), then copy, only, the text files that contain specific words ( e.g LINUX/UNIX).
I ran the following:
find . -name "*.txt" | grep 'LINUX/UNIX'
This command was able to find all the text files, then "grep" filtered the resultant text files by listing only the text files that contain 'LINUX/UNIX'.
How can I copy these final files (i.e. the text files that contain 'LINUX/UNIX') to a specific path of choice?
I tried to apply xargs
find . -name "*.txt" | grep 'LINUX/UNIX' | xargs cp <to a path>
But it didn't work
text-processing grep find cp
add a comment |
I have a collection of files ( *.zip, *.txt, *.tar.gz, *.doc, ...etc ). These files reside within a path. I want to find all the files ( *.txt), then copy, only, the text files that contain specific words ( e.g LINUX/UNIX).
I ran the following:
find . -name "*.txt" | grep 'LINUX/UNIX'
This command was able to find all the text files, then "grep" filtered the resultant text files by listing only the text files that contain 'LINUX/UNIX'.
How can I copy these final files (i.e. the text files that contain 'LINUX/UNIX') to a specific path of choice?
I tried to apply xargs
find . -name "*.txt" | grep 'LINUX/UNIX' | xargs cp <to a path>
But it didn't work
text-processing grep find cp
Pretty much the same as Grep command to find files containing text string and move them
– don_crissti
Jul 20 '16 at 16:41
add a comment |
I have a collection of files ( *.zip, *.txt, *.tar.gz, *.doc, ...etc ). These files reside within a path. I want to find all the files ( *.txt), then copy, only, the text files that contain specific words ( e.g LINUX/UNIX).
I ran the following:
find . -name "*.txt" | grep 'LINUX/UNIX'
This command was able to find all the text files, then "grep" filtered the resultant text files by listing only the text files that contain 'LINUX/UNIX'.
How can I copy these final files (i.e. the text files that contain 'LINUX/UNIX') to a specific path of choice?
I tried to apply xargs
find . -name "*.txt" | grep 'LINUX/UNIX' | xargs cp <to a path>
But it didn't work
text-processing grep find cp
I have a collection of files ( *.zip, *.txt, *.tar.gz, *.doc, ...etc ). These files reside within a path. I want to find all the files ( *.txt), then copy, only, the text files that contain specific words ( e.g LINUX/UNIX).
I ran the following:
find . -name "*.txt" | grep 'LINUX/UNIX'
This command was able to find all the text files, then "grep" filtered the resultant text files by listing only the text files that contain 'LINUX/UNIX'.
How can I copy these final files (i.e. the text files that contain 'LINUX/UNIX') to a specific path of choice?
I tried to apply xargs
find . -name "*.txt" | grep 'LINUX/UNIX' | xargs cp <to a path>
But it didn't work
text-processing grep find cp
text-processing grep find cp
edited Sep 14 '18 at 16:36
asked Jul 20 '16 at 0:41
user88036
Pretty much the same as Grep command to find files containing text string and move them
– don_crissti
Jul 20 '16 at 16:41
add a comment |
Pretty much the same as Grep command to find files containing text string and move them
– don_crissti
Jul 20 '16 at 16:41
Pretty much the same as Grep command to find files containing text string and move them
– don_crissti
Jul 20 '16 at 16:41
Pretty much the same as Grep command to find files containing text string and move them
– don_crissti
Jul 20 '16 at 16:41
add a comment |
4 Answers
4
active
oldest
votes
Try:
grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0r cp -t /path/to/dest
Because this command uses NUL-separation, it is safe for all file names including those with difficult names that include blanks, tabs, or even newlines.
The above requires GNU cp
. For MacOS/FreeBSD, try:
grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0 sh -c 'cp "$@" /path/to/dest' sh
How it works:
grep
options and arguments
-r
tells grep to search recursively through the directory structure. (On FreeBSD,-r
will follow symlinks into directories. This is not true of either OS/X or recent versions of GNUgrep
.)--include '*.txt'
tells grep to only return files whose names match the glob*.txt
(including hidden ones like.foo.txt
or.txt
).-l
tells grep to only return the names of matching files, not the match itself.--null
tells grep to use NUL characters to separate the file names. (--null
is supported bygrep
under GNU/Linux, MacOS and FreeBSD but not OpenBSD.)LINUX/UNIX
tells grep to look only for files whose contents include the regexLINUX/UNIX
.
search in the current directory. You can omit it in recent versions of GNUgrep
, but then you'd need to pass a--
option terminator tocp
to guard against file names that start with-
.
xargs
options and arguments
-0
tells xargs to expect NUL-separated input.-r
tells xargs not to run the command unless at least one file was found. (This option is not needed on either BSD or OSX and is not compatible with OSX'sxargs
.)cp -t /path/to/dest
copies the directories to the target directory. (-t
requires GNUcp
.)
For Mac OS X, and probably BSD, you'll want to use --null instead of -Z. Also, I thinkcp -t
is Linux-only.
– Edward Falk
Jul 20 '16 at 2:18
1
@EdwardFalk Good point. Thanks. I updated to use--null
and added a version for BSD/OSX that does not usecp -t
.
– John1024
Jul 20 '16 at 2:27
@StéphaneChazelas Thank you for the improvements.
– John1024
Jul 20 '16 at 18:40
1
OpenBSDgrep
does not have--null
.
– Kusalananda
May 5 '18 at 20:30
@Kusalananda Thanks. Answer updated to note that OpenBSD does not support--null
.
– John1024
May 6 '18 at 1:48
|
show 2 more comments
More portably (POSIX features only):
find . -type f -name '*.txt' -exec grep -q LINUX/UNIX {} ; -exec cp {} /path/to/dest ;
add a comment |
The following sh/Bash one liner is another method, though will only work in the current directory, and doesn't recurse:
for f in ./*.txt; do if grep -l 'LINUX/UNIX' "$f"; then cp "$f" /path/to/dest/; fi; done
The -l
option to grep will print a list of the files which are being copied, though you could use -q
if you don't want to see anything on the screen.
add a comment |
I am not sure why the original string did not work. Following command works for me.
find / -name (filename*) | grep '(filename.extention)'| xargs cp -t ./
In my case filename* is collection of files with same name with different file types (txt, zip, etc). I do grep to find out only filename.txt and copy it to my destination directory (which is currently, ./).
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f297006%2fgrep-word-within-a-file-then-copy-the-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try:
grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0r cp -t /path/to/dest
Because this command uses NUL-separation, it is safe for all file names including those with difficult names that include blanks, tabs, or even newlines.
The above requires GNU cp
. For MacOS/FreeBSD, try:
grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0 sh -c 'cp "$@" /path/to/dest' sh
How it works:
grep
options and arguments
-r
tells grep to search recursively through the directory structure. (On FreeBSD,-r
will follow symlinks into directories. This is not true of either OS/X or recent versions of GNUgrep
.)--include '*.txt'
tells grep to only return files whose names match the glob*.txt
(including hidden ones like.foo.txt
or.txt
).-l
tells grep to only return the names of matching files, not the match itself.--null
tells grep to use NUL characters to separate the file names. (--null
is supported bygrep
under GNU/Linux, MacOS and FreeBSD but not OpenBSD.)LINUX/UNIX
tells grep to look only for files whose contents include the regexLINUX/UNIX
.
search in the current directory. You can omit it in recent versions of GNUgrep
, but then you'd need to pass a--
option terminator tocp
to guard against file names that start with-
.
xargs
options and arguments
-0
tells xargs to expect NUL-separated input.-r
tells xargs not to run the command unless at least one file was found. (This option is not needed on either BSD or OSX and is not compatible with OSX'sxargs
.)cp -t /path/to/dest
copies the directories to the target directory. (-t
requires GNUcp
.)
For Mac OS X, and probably BSD, you'll want to use --null instead of -Z. Also, I thinkcp -t
is Linux-only.
– Edward Falk
Jul 20 '16 at 2:18
1
@EdwardFalk Good point. Thanks. I updated to use--null
and added a version for BSD/OSX that does not usecp -t
.
– John1024
Jul 20 '16 at 2:27
@StéphaneChazelas Thank you for the improvements.
– John1024
Jul 20 '16 at 18:40
1
OpenBSDgrep
does not have--null
.
– Kusalananda
May 5 '18 at 20:30
@Kusalananda Thanks. Answer updated to note that OpenBSD does not support--null
.
– John1024
May 6 '18 at 1:48
|
show 2 more comments
Try:
grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0r cp -t /path/to/dest
Because this command uses NUL-separation, it is safe for all file names including those with difficult names that include blanks, tabs, or even newlines.
The above requires GNU cp
. For MacOS/FreeBSD, try:
grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0 sh -c 'cp "$@" /path/to/dest' sh
How it works:
grep
options and arguments
-r
tells grep to search recursively through the directory structure. (On FreeBSD,-r
will follow symlinks into directories. This is not true of either OS/X or recent versions of GNUgrep
.)--include '*.txt'
tells grep to only return files whose names match the glob*.txt
(including hidden ones like.foo.txt
or.txt
).-l
tells grep to only return the names of matching files, not the match itself.--null
tells grep to use NUL characters to separate the file names. (--null
is supported bygrep
under GNU/Linux, MacOS and FreeBSD but not OpenBSD.)LINUX/UNIX
tells grep to look only for files whose contents include the regexLINUX/UNIX
.
search in the current directory. You can omit it in recent versions of GNUgrep
, but then you'd need to pass a--
option terminator tocp
to guard against file names that start with-
.
xargs
options and arguments
-0
tells xargs to expect NUL-separated input.-r
tells xargs not to run the command unless at least one file was found. (This option is not needed on either BSD or OSX and is not compatible with OSX'sxargs
.)cp -t /path/to/dest
copies the directories to the target directory. (-t
requires GNUcp
.)
For Mac OS X, and probably BSD, you'll want to use --null instead of -Z. Also, I thinkcp -t
is Linux-only.
– Edward Falk
Jul 20 '16 at 2:18
1
@EdwardFalk Good point. Thanks. I updated to use--null
and added a version for BSD/OSX that does not usecp -t
.
– John1024
Jul 20 '16 at 2:27
@StéphaneChazelas Thank you for the improvements.
– John1024
Jul 20 '16 at 18:40
1
OpenBSDgrep
does not have--null
.
– Kusalananda
May 5 '18 at 20:30
@Kusalananda Thanks. Answer updated to note that OpenBSD does not support--null
.
– John1024
May 6 '18 at 1:48
|
show 2 more comments
Try:
grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0r cp -t /path/to/dest
Because this command uses NUL-separation, it is safe for all file names including those with difficult names that include blanks, tabs, or even newlines.
The above requires GNU cp
. For MacOS/FreeBSD, try:
grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0 sh -c 'cp "$@" /path/to/dest' sh
How it works:
grep
options and arguments
-r
tells grep to search recursively through the directory structure. (On FreeBSD,-r
will follow symlinks into directories. This is not true of either OS/X or recent versions of GNUgrep
.)--include '*.txt'
tells grep to only return files whose names match the glob*.txt
(including hidden ones like.foo.txt
or.txt
).-l
tells grep to only return the names of matching files, not the match itself.--null
tells grep to use NUL characters to separate the file names. (--null
is supported bygrep
under GNU/Linux, MacOS and FreeBSD but not OpenBSD.)LINUX/UNIX
tells grep to look only for files whose contents include the regexLINUX/UNIX
.
search in the current directory. You can omit it in recent versions of GNUgrep
, but then you'd need to pass a--
option terminator tocp
to guard against file names that start with-
.
xargs
options and arguments
-0
tells xargs to expect NUL-separated input.-r
tells xargs not to run the command unless at least one file was found. (This option is not needed on either BSD or OSX and is not compatible with OSX'sxargs
.)cp -t /path/to/dest
copies the directories to the target directory. (-t
requires GNUcp
.)
Try:
grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0r cp -t /path/to/dest
Because this command uses NUL-separation, it is safe for all file names including those with difficult names that include blanks, tabs, or even newlines.
The above requires GNU cp
. For MacOS/FreeBSD, try:
grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0 sh -c 'cp "$@" /path/to/dest' sh
How it works:
grep
options and arguments
-r
tells grep to search recursively through the directory structure. (On FreeBSD,-r
will follow symlinks into directories. This is not true of either OS/X or recent versions of GNUgrep
.)--include '*.txt'
tells grep to only return files whose names match the glob*.txt
(including hidden ones like.foo.txt
or.txt
).-l
tells grep to only return the names of matching files, not the match itself.--null
tells grep to use NUL characters to separate the file names. (--null
is supported bygrep
under GNU/Linux, MacOS and FreeBSD but not OpenBSD.)LINUX/UNIX
tells grep to look only for files whose contents include the regexLINUX/UNIX
.
search in the current directory. You can omit it in recent versions of GNUgrep
, but then you'd need to pass a--
option terminator tocp
to guard against file names that start with-
.
xargs
options and arguments
-0
tells xargs to expect NUL-separated input.-r
tells xargs not to run the command unless at least one file was found. (This option is not needed on either BSD or OSX and is not compatible with OSX'sxargs
.)cp -t /path/to/dest
copies the directories to the target directory. (-t
requires GNUcp
.)
edited May 6 '18 at 1:54
answered Jul 20 '16 at 0:59
John1024John1024
46k4107122
46k4107122
For Mac OS X, and probably BSD, you'll want to use --null instead of -Z. Also, I thinkcp -t
is Linux-only.
– Edward Falk
Jul 20 '16 at 2:18
1
@EdwardFalk Good point. Thanks. I updated to use--null
and added a version for BSD/OSX that does not usecp -t
.
– John1024
Jul 20 '16 at 2:27
@StéphaneChazelas Thank you for the improvements.
– John1024
Jul 20 '16 at 18:40
1
OpenBSDgrep
does not have--null
.
– Kusalananda
May 5 '18 at 20:30
@Kusalananda Thanks. Answer updated to note that OpenBSD does not support--null
.
– John1024
May 6 '18 at 1:48
|
show 2 more comments
For Mac OS X, and probably BSD, you'll want to use --null instead of -Z. Also, I thinkcp -t
is Linux-only.
– Edward Falk
Jul 20 '16 at 2:18
1
@EdwardFalk Good point. Thanks. I updated to use--null
and added a version for BSD/OSX that does not usecp -t
.
– John1024
Jul 20 '16 at 2:27
@StéphaneChazelas Thank you for the improvements.
– John1024
Jul 20 '16 at 18:40
1
OpenBSDgrep
does not have--null
.
– Kusalananda
May 5 '18 at 20:30
@Kusalananda Thanks. Answer updated to note that OpenBSD does not support--null
.
– John1024
May 6 '18 at 1:48
For Mac OS X, and probably BSD, you'll want to use --null instead of -Z. Also, I think
cp -t
is Linux-only.– Edward Falk
Jul 20 '16 at 2:18
For Mac OS X, and probably BSD, you'll want to use --null instead of -Z. Also, I think
cp -t
is Linux-only.– Edward Falk
Jul 20 '16 at 2:18
1
1
@EdwardFalk Good point. Thanks. I updated to use
--null
and added a version for BSD/OSX that does not use cp -t
.– John1024
Jul 20 '16 at 2:27
@EdwardFalk Good point. Thanks. I updated to use
--null
and added a version for BSD/OSX that does not use cp -t
.– John1024
Jul 20 '16 at 2:27
@StéphaneChazelas Thank you for the improvements.
– John1024
Jul 20 '16 at 18:40
@StéphaneChazelas Thank you for the improvements.
– John1024
Jul 20 '16 at 18:40
1
1
OpenBSD
grep
does not have --null
.– Kusalananda
May 5 '18 at 20:30
OpenBSD
grep
does not have --null
.– Kusalananda
May 5 '18 at 20:30
@Kusalananda Thanks. Answer updated to note that OpenBSD does not support
--null
.– John1024
May 6 '18 at 1:48
@Kusalananda Thanks. Answer updated to note that OpenBSD does not support
--null
.– John1024
May 6 '18 at 1:48
|
show 2 more comments
More portably (POSIX features only):
find . -type f -name '*.txt' -exec grep -q LINUX/UNIX {} ; -exec cp {} /path/to/dest ;
add a comment |
More portably (POSIX features only):
find . -type f -name '*.txt' -exec grep -q LINUX/UNIX {} ; -exec cp {} /path/to/dest ;
add a comment |
More portably (POSIX features only):
find . -type f -name '*.txt' -exec grep -q LINUX/UNIX {} ; -exec cp {} /path/to/dest ;
More portably (POSIX features only):
find . -type f -name '*.txt' -exec grep -q LINUX/UNIX {} ; -exec cp {} /path/to/dest ;
edited Nov 9 '16 at 9:28
answered Jul 20 '16 at 1:43
WildcardWildcard
22.7k962164
22.7k962164
add a comment |
add a comment |
The following sh/Bash one liner is another method, though will only work in the current directory, and doesn't recurse:
for f in ./*.txt; do if grep -l 'LINUX/UNIX' "$f"; then cp "$f" /path/to/dest/; fi; done
The -l
option to grep will print a list of the files which are being copied, though you could use -q
if you don't want to see anything on the screen.
add a comment |
The following sh/Bash one liner is another method, though will only work in the current directory, and doesn't recurse:
for f in ./*.txt; do if grep -l 'LINUX/UNIX' "$f"; then cp "$f" /path/to/dest/; fi; done
The -l
option to grep will print a list of the files which are being copied, though you could use -q
if you don't want to see anything on the screen.
add a comment |
The following sh/Bash one liner is another method, though will only work in the current directory, and doesn't recurse:
for f in ./*.txt; do if grep -l 'LINUX/UNIX' "$f"; then cp "$f" /path/to/dest/; fi; done
The -l
option to grep will print a list of the files which are being copied, though you could use -q
if you don't want to see anything on the screen.
The following sh/Bash one liner is another method, though will only work in the current directory, and doesn't recurse:
for f in ./*.txt; do if grep -l 'LINUX/UNIX' "$f"; then cp "$f" /path/to/dest/; fi; done
The -l
option to grep will print a list of the files which are being copied, though you could use -q
if you don't want to see anything on the screen.
edited Jul 20 '16 at 15:23
Stéphane Chazelas
301k55564916
301k55564916
answered Jul 20 '16 at 9:16
ArronicalArronical
235111
235111
add a comment |
add a comment |
I am not sure why the original string did not work. Following command works for me.
find / -name (filename*) | grep '(filename.extention)'| xargs cp -t ./
In my case filename* is collection of files with same name with different file types (txt, zip, etc). I do grep to find out only filename.txt and copy it to my destination directory (which is currently, ./).
New contributor
add a comment |
I am not sure why the original string did not work. Following command works for me.
find / -name (filename*) | grep '(filename.extention)'| xargs cp -t ./
In my case filename* is collection of files with same name with different file types (txt, zip, etc). I do grep to find out only filename.txt and copy it to my destination directory (which is currently, ./).
New contributor
add a comment |
I am not sure why the original string did not work. Following command works for me.
find / -name (filename*) | grep '(filename.extention)'| xargs cp -t ./
In my case filename* is collection of files with same name with different file types (txt, zip, etc). I do grep to find out only filename.txt and copy it to my destination directory (which is currently, ./).
New contributor
I am not sure why the original string did not work. Following command works for me.
find / -name (filename*) | grep '(filename.extention)'| xargs cp -t ./
In my case filename* is collection of files with same name with different file types (txt, zip, etc). I do grep to find out only filename.txt and copy it to my destination directory (which is currently, ./).
New contributor
New contributor
answered Jan 9 at 22:08
SamLSamL
1
1
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f297006%2fgrep-word-within-a-file-then-copy-the-file%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
Pretty much the same as Grep command to find files containing text string and move them
– don_crissti
Jul 20 '16 at 16:41