Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

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

What is the Linux command to delete all files and directories (including hidden files and directories) from a directory?
0
jbaird123
Asked:
jbaird123
  • 6
  • 5
  • 3
  • +2
3 Solutions
 
Neil RussellTechnical Development LeadCommented:
rm-rf directoryname

BUT BE CAREFULL
0
 
jbaird123Author Commented:
what if I am already in that directory?
0
 
woolmilkporcCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Sikhumbuzo NtsadaSenior IT TechnicianCommented:
rm ./*
0
 
Sikhumbuzo NtsadaSenior IT TechnicianCommented:
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
 
jbaird123Author Commented:
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
 
woolmilkporcCommented:
See my comment!
0
 
jbaird123Author Commented:
woolmilkporc:  Can you explain your command?  I don't understand it.
0
 
Neil RussellTechnical Development LeadCommented:
rm -rf Direectoryname
md Directoryname
:P
0
 
woolmilkporcCommented:
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
 
woolmilkporcCommented:
@Neilsr: Take care to record ownership and permissions beforehand, so you'll be able to recreate the directory as it was before!
0
 
Sikhumbuzo NtsadaSenior IT TechnicianCommented:
Edited virsion:

rm -rf ./*
0
 
jbaird123Author Commented:
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
 
woolmilkporcCommented:
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
 
jbaird123Author Commented:
Thank you.
0
 
TintinCommented:
Note that

rm -rf ./*

will not remove hidden files.
0
 
jbaird123Author Commented:
Thanks, Tintin.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now