Is there a way to use > operator in find -exec
up vote
2
down vote
favorite
I'm trying to empty lots of files under a certain folder.
>file
or cat /dev/null > file
or echo "" > file
can empty file.
find . -type f -exec blahblah {} ;
can find files and do something on them.
I tried to use the >
operator in find ... -exec
but the result is different to what I expected.
Is there a way to use >
operator in the find
command?
find exec
add a comment |
up vote
2
down vote
favorite
I'm trying to empty lots of files under a certain folder.
>file
or cat /dev/null > file
or echo "" > file
can empty file.
find . -type f -exec blahblah {} ;
can find files and do something on them.
I tried to use the >
operator in find ... -exec
but the result is different to what I expected.
Is there a way to use >
operator in the find
command?
find exec
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to empty lots of files under a certain folder.
>file
or cat /dev/null > file
or echo "" > file
can empty file.
find . -type f -exec blahblah {} ;
can find files and do something on them.
I tried to use the >
operator in find ... -exec
but the result is different to what I expected.
Is there a way to use >
operator in the find
command?
find exec
I'm trying to empty lots of files under a certain folder.
>file
or cat /dev/null > file
or echo "" > file
can empty file.
find . -type f -exec blahblah {} ;
can find files and do something on them.
I tried to use the >
operator in find ... -exec
but the result is different to what I expected.
Is there a way to use >
operator in the find
command?
find exec
find exec
edited Jul 22 '14 at 13:21
An Dorfer
1,2032713
1,2032713
asked Feb 11 '14 at 14:14
Sencer H.
796617
796617
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
4
down vote
accepted
You can't use it directly, since it will be interpreted as an actual redirection. You have to wrap the call in another shell:
find . -type f -exec sh -c 'cat /dev/null >| $0' {} ;
If sh
is Bash, you can also do:
find . -type f -exec sh -c '> $0' {} ;
1
Ouch! My head! How I can't remember that :) Thank you.
– Sencer H.
Feb 11 '14 at 14:42
add a comment |
up vote
2
down vote
Or you could redirect the output of the find command with process substitution:
while IFS= read -r -d '' file
do cat /dev/null > "$file"
done < <(find . type -f print0)
add a comment |
up vote
1
down vote
parallel allows escaping >
as >
:
find . -type f|parallel >{}
Or just use read
:
find . -type f|while read f;do >"$f";done
You don't need -r
, -d ''
, or IFS=
unless the paths contain backslashes or newlines or start or end with characters in IFS
.
+1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
– Hennes
Mar 8 '14 at 13:54
add a comment |
up vote
1
down vote
Alternatively, one could just use the appropriately named truncate
command.
Like this:
truncate -s 0 file.blob
The GNU coreutils version of truncate
also handles a lot of fascinating things:
SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.
An even simpler, although less appropriately “named” method would be
cp /dev/null file.blob
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You can't use it directly, since it will be interpreted as an actual redirection. You have to wrap the call in another shell:
find . -type f -exec sh -c 'cat /dev/null >| $0' {} ;
If sh
is Bash, you can also do:
find . -type f -exec sh -c '> $0' {} ;
1
Ouch! My head! How I can't remember that :) Thank you.
– Sencer H.
Feb 11 '14 at 14:42
add a comment |
up vote
4
down vote
accepted
You can't use it directly, since it will be interpreted as an actual redirection. You have to wrap the call in another shell:
find . -type f -exec sh -c 'cat /dev/null >| $0' {} ;
If sh
is Bash, you can also do:
find . -type f -exec sh -c '> $0' {} ;
1
Ouch! My head! How I can't remember that :) Thank you.
– Sencer H.
Feb 11 '14 at 14:42
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You can't use it directly, since it will be interpreted as an actual redirection. You have to wrap the call in another shell:
find . -type f -exec sh -c 'cat /dev/null >| $0' {} ;
If sh
is Bash, you can also do:
find . -type f -exec sh -c '> $0' {} ;
You can't use it directly, since it will be interpreted as an actual redirection. You have to wrap the call in another shell:
find . -type f -exec sh -c 'cat /dev/null >| $0' {} ;
If sh
is Bash, you can also do:
find . -type f -exec sh -c '> $0' {} ;
answered Feb 11 '14 at 14:21
slhck
158k47436461
158k47436461
1
Ouch! My head! How I can't remember that :) Thank you.
– Sencer H.
Feb 11 '14 at 14:42
add a comment |
1
Ouch! My head! How I can't remember that :) Thank you.
– Sencer H.
Feb 11 '14 at 14:42
1
1
Ouch! My head! How I can't remember that :) Thank you.
– Sencer H.
Feb 11 '14 at 14:42
Ouch! My head! How I can't remember that :) Thank you.
– Sencer H.
Feb 11 '14 at 14:42
add a comment |
up vote
2
down vote
Or you could redirect the output of the find command with process substitution:
while IFS= read -r -d '' file
do cat /dev/null > "$file"
done < <(find . type -f print0)
add a comment |
up vote
2
down vote
Or you could redirect the output of the find command with process substitution:
while IFS= read -r -d '' file
do cat /dev/null > "$file"
done < <(find . type -f print0)
add a comment |
up vote
2
down vote
up vote
2
down vote
Or you could redirect the output of the find command with process substitution:
while IFS= read -r -d '' file
do cat /dev/null > "$file"
done < <(find . type -f print0)
Or you could redirect the output of the find command with process substitution:
while IFS= read -r -d '' file
do cat /dev/null > "$file"
done < <(find . type -f print0)
edited Feb 13 '14 at 11:50
slhck
158k47436461
158k47436461
answered Feb 13 '14 at 11:31
stib
2,30341930
2,30341930
add a comment |
add a comment |
up vote
1
down vote
parallel allows escaping >
as >
:
find . -type f|parallel >{}
Or just use read
:
find . -type f|while read f;do >"$f";done
You don't need -r
, -d ''
, or IFS=
unless the paths contain backslashes or newlines or start or end with characters in IFS
.
+1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
– Hennes
Mar 8 '14 at 13:54
add a comment |
up vote
1
down vote
parallel allows escaping >
as >
:
find . -type f|parallel >{}
Or just use read
:
find . -type f|while read f;do >"$f";done
You don't need -r
, -d ''
, or IFS=
unless the paths contain backslashes or newlines or start or end with characters in IFS
.
+1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
– Hennes
Mar 8 '14 at 13:54
add a comment |
up vote
1
down vote
up vote
1
down vote
parallel allows escaping >
as >
:
find . -type f|parallel >{}
Or just use read
:
find . -type f|while read f;do >"$f";done
You don't need -r
, -d ''
, or IFS=
unless the paths contain backslashes or newlines or start or end with characters in IFS
.
parallel allows escaping >
as >
:
find . -type f|parallel >{}
Or just use read
:
find . -type f|while read f;do >"$f";done
You don't need -r
, -d ''
, or IFS=
unless the paths contain backslashes or newlines or start or end with characters in IFS
.
answered Mar 8 '14 at 13:43
user495470
30.8k586125
30.8k586125
+1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
– Hennes
Mar 8 '14 at 13:54
add a comment |
+1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
– Hennes
Mar 8 '14 at 13:54
+1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
– Hennes
Mar 8 '14 at 13:54
+1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
– Hennes
Mar 8 '14 at 13:54
add a comment |
up vote
1
down vote
Alternatively, one could just use the appropriately named truncate
command.
Like this:
truncate -s 0 file.blob
The GNU coreutils version of truncate
also handles a lot of fascinating things:
SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.
An even simpler, although less appropriately “named” method would be
cp /dev/null file.blob
add a comment |
up vote
1
down vote
Alternatively, one could just use the appropriately named truncate
command.
Like this:
truncate -s 0 file.blob
The GNU coreutils version of truncate
also handles a lot of fascinating things:
SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.
An even simpler, although less appropriately “named” method would be
cp /dev/null file.blob
add a comment |
up vote
1
down vote
up vote
1
down vote
Alternatively, one could just use the appropriately named truncate
command.
Like this:
truncate -s 0 file.blob
The GNU coreutils version of truncate
also handles a lot of fascinating things:
SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.
An even simpler, although less appropriately “named” method would be
cp /dev/null file.blob
Alternatively, one could just use the appropriately named truncate
command.
Like this:
truncate -s 0 file.blob
The GNU coreutils version of truncate
also handles a lot of fascinating things:
SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.
An even simpler, although less appropriately “named” method would be
cp /dev/null file.blob
answered Jul 22 '14 at 13:37
Daniel B
32.8k75986
32.8k75986
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fsuperuser.com%2fquestions%2f715386%2fis-there-a-way-to-use-operator-in-find-exec%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