I would like to delete every file, but keep the folder structure. Is there a way?
NOTE: (I'm using GNU bash 4.1.5).
I would like to delete every file, but keep the folder structure. Is there a way?
NOTE: (I'm using GNU bash 4.1.5).
Try this:
find . ! -type d -exec rm '{}' \;
This will delete every single file, excluding directories, below the current working directory. Be extremely careful with this command.
If the version of find on your machine supports it, you can also use
find . ! -type d -delete
You can use the command find to locate every file but maintain the directory structure:
$ find /some/dir -type f -exec rm {} +
Per this Unix & Linux Q&A titled: gnu find and masking the {} for some shells - which?, escaping the {} with single ticks (') doesn't appear to be necessary anymore with modern day shells such as Bash.
The easy way to delete every regular file in the current directory and subdirectories recursively:
zsh -c 'rm **/*(.)'
Only zsh has globbing qualifiers to match files by type. However, the rm command doesn't work on directories, so in bash, you can use
shopt -s globstar
rm **/*
This doesn't work for commands other than rm though. In general, you can use find:
find . -type f -delete
or if your find doesn't support -delete:
find . -type f -exec rm {} +
I had similar requirement to delete files from a path and its sub directories (filtering by time ) with out deleting the directory structure .
And i have used the below format which worked for me .
find /test123/home/test_file_hip/data/nfs -mtime +6 -type f -exec rm {} \;
Syntex : find (path of file) -mtime (greater than or less than days) -type f -exec rm {} \;
-type : Mention the type of file "f" for "d" directory -exec : execute command rm : remove {} : output of find command
Note : Do test it before using it . Please feel free to correct or update if i missed anything .