Solved

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

Posted on 2011-02-23
17
381 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
  • 6
  • 5
  • 3
  • +2
17 Comments
 
LVL 37

Expert Comment

by:Neil Russell
Comment Utility
rm-rf directoryname

BUT BE CAREFULL
0
 

Author Comment

by:jbaird123
Comment Utility
what if I am already in that directory?
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
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
 
LVL 17

Expert Comment

by:Sikhumbuzo Ntsada
Comment Utility
rm ./*
0
 
LVL 17

Expert Comment

by:Sikhumbuzo Ntsada
Comment Utility
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
Comment Utility
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
Comment Utility
See my comment!
0
 

Author Comment

by:jbaird123
Comment Utility
woolmilkporc:  Can you explain your command?  I don't understand it.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 37

Expert Comment

by:Neil Russell
Comment Utility
rm -rf Direectoryname
md Directoryname
:P
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
Comment Utility
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
Comment Utility
@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
Comment Utility
Edited virsion:

rm -rf ./*
0
 

Author Comment

by:jbaird123
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
Note that

rm -rf ./*

will not remove hidden files.
0
 

Author Comment

by:jbaird123
Comment Utility
Thanks, Tintin.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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 …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now