How do I delete all the contents in a folder, but keep the folder itself?

How do I delete all files, folders, and links in a folder, but leave the folder itself? I have a temp folder and I put files in it, and after working with them I would like to delete all the contents. I have files like .zshrc temp.txt and also folders with child subfolders and hidden directories like . config and also there are regular files like info.txt temp.php, etc. And there is also a file in the name of which there are two dots like ..12

How do I clear all this from the temp folder ??

I have already tried the command sudo rm -rf /home/temp/.* /home/temp/* /home/temp/*.*

But it doesn't always work. If the folder temp will find all the types of files listed, then yes, it will work, but if, for example, only a folder and everything, and there are no other file types, then it outputs this

no matches found: /home/temp/*.*

Although the folder with subfolders exists even after the command is executed. If you manually delete each folder separately and for each to the file, then everything is normally deleted. What am I doing wrong?

Author: Ivan, 2019-10-17

2 answers

For example, you can instruct the shell (examples are given for the two most common programs that perform the role of a shell - bash and zsh) to include in the list of files/directories that fall under the * template and those whose names begin with a dot.

bash:

$ (shopt -s dotglob; ls -d /путь/*)
$ (GLOBIGNORE=.;     ls -d /путь/*)

zsh:

$ (setopt globdots; ls -d /путь/*)
$ (setopt dotglob;  ls -d /путь/*)
$ (set -o globdots; ls -d /путь/*)
$ (set -o dotglob;  ls -d /путь/*)

The parentheses in the examples are used so that the override does not affect the current shell process (what is in the parentheses will be executed by a child process).

 4
Author: aleksandr barakin, 2019-10-18 06:31:04

And in a simple way:

rm -rf ~/tmp
mkdir ~/tmp

Can I?: -)

If "the folder is not simple, but mounted" then there will be problems. Then, to avoid the fatal action of the error

No matches found:

I recommend breaking up your long command

sudo rm -rf /home/temp/.* /home/temp/* /home/temp/*.*

On the sequence of elementary actions:

sudo rm -rf /home/temp/.* 
sudo rm -rf /home/temp/* 
sudo rm -rf /home/temp/*.*

Then the absence of suitable files in one delete command will not affect the work of the other delete commands in any way.

 5
Author: Sergey, 2019-10-19 09:25:24