seth
seth

Reputation:

Script to delete all folders it is placed in

Can anyone here help me out with a script that will let me, when run, delete all the folders and their contents in whatever folder it is placed in.

Upvotes: 1

Views: 1662

Answers (2)

crb
crb

Reputation: 8178

What operating system? Do you want to remove files in the current directory also?

Under cmd.exe in Windows, for files, you can run

del /s /q *

or to remove just folders and their contents,

for /d %d in (*.*) do rmdir /s /q %d

Under most Linux/UNIX shells, to delete files and folders, you can run

rm -rf *

or as pointed out below by derobert (and tidied up a little), you can do just folders and their contents with

find . -maxdepth 1 -not -name '.' -type d -exec rm -rf \{\} \;

This will find all the directories in the current directory (maxdepth 1) excluding the current directory '.', and run rm -rf on each of them.

Upvotes: 5

derobert
derobert

Reputation: 51157

On Unix, you can do something like this:

find -type d -maxdepth 1 -not -name '.' -print0 | xargs -0 rm -Rf

This will get rid of all folders (and their contents) in the current working directory, leaving only the files not inside a folder. Given:

test/folder1
test/folder1/file1
test/file2

if you run it in test, then only file2 will be left.

Upvotes: 1

Related Questions