Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux command to empty all files of a directory [closed]

Tags:

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?

like image 306
Luis Andrés García Avatar asked Jan 28 '13 15:01

Luis Andrés García


People also ask

Which command is used to delete all files in a directory?

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.

Is there a way to erase all files in the current 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.

How can I delete all files in a directory without prompt?

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.


1 Answers

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 '>"{}"' \;
like image 62
dogbane Avatar answered Oct 26 '22 12:10

dogbane