Solved

How to delete all files, directories, and hidden files and directories in Linux

Posted on 2011-02-23
17
395 Views
Last Modified: 2012-08-13
What is the Linux command to delete all files and directories (including hidden files and directories) from a directory?
0
Comment
Question by:jbaird123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +2
17 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 34960694
rm-rf directoryname

BUT BE CAREFULL
0
 

Author Comment

by:jbaird123
ID: 34960707
what if I am already in that directory?
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 34960745
cd directory
rm -rf * .?*

Nothing will happen to "directory"

Ignore the warning of being unable to remove "." or ".." That's expected (and intended)!

wmp
0
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
LVL 17

Expert Comment

by:Sikhumbuzo Ntsada
ID: 34960753
rm ./*
0
 
LVL 17

Expert Comment

by:Sikhumbuzo Ntsada
ID: 34960766
Use that command carefully, if you are ins a wrong folder hell will brake lose.

And all files must be closed and not be executed by any program.
0
 

Author Comment

by:jbaird123
ID: 34960840
Neilsr:  I tried your command, and it deleted "directoryname".

I don't want to delete "directoryname".  I want to delete all contents of that directory, but not the folder itself.  Please advise.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34960847
See my comment!
0
 

Author Comment

by:jbaird123
ID: 34960878
woolmilkporc:  Can you explain your command?  I don't understand it.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 34960926
rm -rf Direectoryname
md Directoryname
:P
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 34960943
remove ("rm") recursively ("-r") without asking questions ("-f") all files except hidden files ("*")  plus all files starting with a dot followed by at least one character(".?*"), which is equivalent to "hidden files", from the current directory.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 34960963
@Neilsr: Take care to record ownership and permissions beforehand, so you'll be able to recreate the directory as it was before!
0
 
LVL 17

Expert Comment

by:Sikhumbuzo Ntsada
ID: 34960966
Edited virsion:

rm -rf ./*
0
 

Author Comment

by:jbaird123
ID: 34961049
woolmilkporc and Santasi24:

Can you both confirm that your commands will delete only the contents of the directory that I am in?  

The reason I ask is because rm -rf <directoryname> means to delete a specific directory.  Your commands do not contain a directory name, so I need to be sure that it means "whatever directory i am currently residing in"

What does the ./ mean?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34961139
Please note that I suggested

cd directory
rm -rf * .?*

So directory will be your current directory. It will not be deleted, because "rm" does not delete "." or ".."

wmp

0
 

Author Closing Comment

by:jbaird123
ID: 34961319
Thank you.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 34965756
Note that

rm -rf ./*

will not remove hidden files.
0
 

Author Comment

by:jbaird123
ID: 34965852
Thanks, Tintin.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question