How to clear all files within a directory under Linux

I need to clear and remove all files from a directory when using CentOS Linux. It worked:

cd dir1
rm *

But hidden files not cleared/deleted out. How do I do that?

Run the following to delete all files:

rm -r *

To remove and clear out hidden files run:

rm -rv {*,.*}
rm -rv /path/to/dir/{*,.*}

Another option set out dotglob option:

# set shell option
shopt -s dotglob
##################################################
# Remove all files including hidden . (dot) files   
##################################################
rm -v /path/to/folder/*
rm -vrf ~/dir1/*
 
# unset
shopt -u dotglob

Where,

  • rm means remove files
  • -v means verbose
  • -f means forceful removal of files
  • -i means get confirmation before removal of any files

See our tutorial page for more information:


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki