BIOS
BIOS

Reputation: 1695

Unix Command to Delete all files in a directory but preserve the directory

I am looking for a unix command to delete all files within a directory without deleting the directory itself. (note the directory does not contain subdirectories).

Upvotes: 10

Views: 62802

Answers (8)

andy
andy

Reputation: 1

you can use rm -r /UrDir/*.* This would ignore the files in the sub-directories

Upvotes: 0

Amranur Rahman
Amranur Rahman

Reputation: 1123

If you want to Delete all file as well as all Directory that means all things then try this:

rm -rf *

Upvotes: 1

javaPlease42
javaPlease42

Reputation: 4963

If you are in the directory where you want to remove all the files then the following command works fine:

rm *

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318508

You can use find /path/to/your/folder/ -delete to delete everything within that folder.

While a wildcard rm would braek with too many files ("Argument list too long"), this works no matter how many files there are.

You can also make it delete only files but preserve any subdirectories:

find /path/to/your/folder/ -type f -delete

You could also specify any other criteria find supports to restrict the "results".

Upvotes: 4

Thiyagu ATR
Thiyagu ATR

Reputation: 2264

This will help you,

rm path/*

eg:

rm ../mydir/*

In this command,if mydir has any sub_directory! it'll raise error an message and skip that sub_directory and remove rest of the files in main directory.

Upvotes: 0

Radu Pluta
Radu Pluta

Reputation: 131

you can remove all the files form the current directory using rm * if you want to remove from a specific directory, type rm /path/*

Upvotes: 2

Amxx
Amxx

Reputation: 3070

try

rm -r yourDirectory/*

it deletes all file inside the "yourdirectory" directory

Upvotes: 7

BigMike
BigMike

Reputation: 6873

rm -i <directory>/*

this should do the trick

EDIT: added -i just in case (safety first). directory should be a full or relative path (e.g. /tmp/foo or ../trash/stuffs)

Upvotes: 10

Related Questions