I can do this:
find /home -name Trash -exec ls -al {} \;
and this:
find /home -name Trash -exec rm {} \;
but not this:
find /home -name Trash -exec cat /dev/null > {} \;
(!) [Neil]
It's the redirection that's the problem. If you quote the '>' thus:
find /hometest -name Trash -exec cat /dev/null '>' {} \;
it will work, with the caveat that you may still hit some "trash" files in
subdirectories.
Check where you ran the command. You will find an empty file called "{}",
created by your redirection. The command you ran was equivalent to
find /hometest -name Trash -exec cat /dev/null \; > {}
That will empty anything called Trash in subdirectories as well as in the
login directories. To only hit files in the login directories you should
use a for loop, e.g.
for file in /home/*/Trash
do
echo -n > $file
done
Before trying this put another echo in front of echo echo -n > $file, so
you can see the commands it will run and sanity check them before running
it for real.
What errors are you getting? Do you have permissions to write to these
files?
(?) or this:
find /home -name Trash | xargs cat > /dev/null
(!) [Neil] That wouldn't work. You're just listing the files and directing
the output to /dev/null, which won't achieve what you want.
(?) While root, when I do this:
find /hometest -name -Trash -exec cat /dev/null > {} \;
it runs and exists after a second giving me a new prompt (a carriage return)
and no errror messages.
When I run this:
find /hometest -name Trash -exec ls -s {} \;
I get this:
60 /hometest/accounting.test/Trash
264 /hometest/adam.test/Trash
3120 /hometest/agency.test/Trash
164 /hometest/joh.doe/Trash
4976 /hometest/alice.test/Trash
so obviously it didn't work but I didn't get any errors.
Your "for" script worked great and is short and sweet. I'm very greatful,
however, for my own information, I'd still like to understand what's wrong
with my find syntax/structure. If you guys post this solution on the website
you should put in the keywords "empty files". I've googled for all kinds of
crazy things and never found a solution.
(!) [Jason] Look carefully at your command.
find /hometest -name -Trash -exec cat /dev/null > {} \;
This runs "find /hometest -name -Trash -exec cat /dev/null" and redirects
the output to a file named "{}".
Quoting the '>' doesn't help since find doesn't use the shell to expand
commands given with -exec. (That is, if you quoted the ">", cat would be
run with three arguments. The first would be a file named "/dev/null". The
second would a file named ">", which cat would probably complain doesn't
exist. It is possible you might actually have a file named ">", but it's
such a weird and confusing name that you probably don't. And the third
would be the name of the file you're trying to truncate.)
If, for some reason, you needed to use "find" (perhaps to only truncate
files with a certain mtime, or whatever), you could use a script like
this:
#! /bin/sh
for file in "$@"; do
[ -f "$file" ] && echo -n > "$file"
done
name it truncate.sh or something, make it executable, and save it
somewhere. Then you could do:
find /path/to/files -exec truncate.sh {} \;
=6= |