I´d like to empty all files from a directory. I´d tried this:
find myFolderPath/* -exec cat /dev/null > {} ';'
but it does not work. How can I do it?
Use the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory.
Use rm * from within the specific directory. The * is a wildcard that matches all files. It will not remove subdirectories or files inside them. If you want that too, use rm -r * instead.
Using the -r flag to deleting a non-empty directory. If you do not want a prompt before deleting the directory and its contents, use the -rf flag. This will remove everything inside the directory, including the directory itself, without any confirmation.
You can't use redirection (>
) within find -exec
directly because it happens before the command runs and creates a file called {}
. To get around this you need to do it in a new shell by using sh -c
.
Also, note that you don't need to cat /dev/null > file
in order to clobber a file. You can simply use > file
.
Try this:
find . -type f -exec sh -c '>"{}"' \;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With