How to delete all files and folders in current directory using Linux commands?

wpspeedster

Well-known member
Registered
Joined
May 6, 2016
Messages
120
Points
18
I am trying to remove all files and sub directories in a directory but I only want to remove all files and sub folders, excluding the top directory itself.
For example, I have a top directory like cPanel and it contains the files as system.log, gmp.sys and five folders, in this five folders, there are many sub folders.
Now I want to delete all files in cPanel directory and all folders and sub folders in it.
I am at root folder. How can I do this in Linux and using commands only?
 

LJSHost

Well-known member
Hosting Provider
Registered
Joined
Jul 5, 2016
Messages
1,031
Points
63
If you want to remove the folder with all its contents (including all interior folders)

rm -rfi /root/directory/*

This will remove everything above /root/directory/ but leave the /root/directory/ empty but not deleted.

I added the -i to ask for approval on every delete for safety, if the directory contains hundreds of files you will need to remove the -i or it will take hours.
EXTERME WARNING: unless you understand this command completely and know exactly what you are doing do not run it !
 

GswHosting

Well-known member
Registered
Joined
Aug 23, 2016
Messages
233
Points
0
Great advice LJSHost, I never had need to delete all. I have some times need to delete something from some directory (leaving directory).
I usually go to that directory using cd and then use rm * -R
 

WPCycle

Well-known member
Hosting Provider
Registered
Joined
Dec 31, 2016
Messages
123
Points
18
The rm -rfi /folder/folder/* is very useful.

Now if you really dislike someone...just do rm -R in the root and wait for sparks to fly ;)
 

GswHosting

Well-known member
Registered
Joined
Aug 23, 2016
Messages
233
Points
0
That reminds of some old joke.

Your father have hidden games on computer to access them go to dos, enter format c: and hit enter and you will find all games.
 

WPCycle

Well-known member
Hosting Provider
Registered
Joined
Dec 31, 2016
Messages
123
Points
18
WPCycle
HAHAHA. You know the young cats will have to Google "What is DOS" and will be astounded by 16k RAM and saving data to cassettes...which they will then Google "What is a cassette" :hysterical:
 

LJSHost

Well-known member
Hosting Provider
Registered
Joined
Jul 5, 2016
Messages
1,031
Points
63
I will just add to my original comments that before deleting anything always backup first. Better safe than sorry.

Deleting files on the command line as root can go badly wrong the difference between deleting a few folders or the entire file system can be as simple as a / or a * in the wrong place.
 
Older Threads
Newer Threads
Replies
11
Views
9,404
Replies
7
Views
3,766
Replies
4
Views
2,491
Replies
32
Views
8,538
Recommended Threads
Replies
0
Views
1,911
Replies
3
Views
3,100
Replies
2
Views
2,257
Replies
5
Views
2,257

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top