Ubuntu – How to remove all files from a directory

command linerm

The closest I've gotten is

# rm /path/to/directory/*.*

but that doesn't work for files that don't have an extension…

Best Answer

Linux does not use extensions. It is up to the creator of the file to decide whether the name should have an extension. Linux looks at the first few bytes to figure out what kind of file it is dealing with.

  • To remove all non-hidden files* in a directory use:

    rm /path/to/directory/*

    However, this will show an error for each sub-directory, because in this mode it is only allowed to delete files.

  • To remove all non-hidden files and sub-directories (along with all of their contents) in a directory use:

    rm -r /path/to/directory/*

* Hidden files and directories are those whose names start with . (dot) character, e.g.: .hidden-file or .hidden-directory/. Note that, in Bash, if the dotglob option (which is off by default) is set, rm will act on hidden files too, because they will be included when * is expanded by the shell to provide the list of filename arguments.