Skip to content

How to delete only the subfolder and keep the files

How to delete only the subfolder and keep the files by using the console

I want to refresh my WordPress project created by using my scripts.

I just edited the .env file and I need some docker refresh

In this case, I want just the folders to be removed. Here is the console command:

sudo find . -mindepth 1 -maxdepth 1 -type d -exec rm -rf {} +

Explanation:

  • sudo: Runs the command with superuser privileges, which may be necessary if you don’t have permission to delete some of the subfolders.
  • find .: Starts the search from the current directory (.).
  • -mindepth 1: Specifies to find files or directories starting from depth 1 (subdirectories), not including the current directory.
  • -maxdepth 1: Restricts the search to depth 1, i.e., only in the immediate subdirectories, not further down the directory tree.
  • -type d: Specifies to find only directories.
  • -exec rm -rf {} +: Executes the rm -rf command on each found directory ({} represents the found directory). The + at the end is used for efficiency, it tells find to run rm with as many arguments as possible at once, rather than once for each file.

This command will delete only the subfolders within the current directory, keeping the files intact.

echo "Caution: This command will delete all subfolders in the current directory. Make sure you have backed up any important data before proceeding."

Visits: 2

Leave a Reply

Your email address will not be published. Required fields are marked *