LINUX CLASSES - FILES

Delete Linux Files and Directories

How Do I Delete Files and Directories?

Deleting Files

Ready to do a little damage? The rm command removes a file (assuming you have permission to do so) without even saying hasta la vista. Be sure you really want to delete your files before you use rm, because once the files are gone, they're not coming back. For example, $ rm wallet

immediately deletes the file named wallet in the current directory without prompting. If you want to be prompted before the deletion, use the -i flag.

Note: There is one little safety feature in rm--it won't delete a directory (unless you use the -r flag).

One other rm flag is -f, which translates roughly to "Don't ask me any questions--just delete the files." While rm normally asks for confirmation before deleting a write-protected file, the -f (force) flag overrides this prompt.

Be careful with the rm command, since the multiuser nature of Linux does not allow for undelete as in DOS. As soon as you let go of the space occupied by a file, the operating system is likely to use it for something else.

Deleting Directories

If you created a directory named spelunking six months ago, and now you're not so keen on crawling through slimy caves, the rmdir command may be able to help:

$ rmdir spelunking

This command removes the specified directory, but only if it's empty. If the directory contains files or subdirectories, you'll have to delete them using rm first.

One alternative to painstakingly removing all the contents of a directory that you just want to make disappear is to use the rm -r command. The -r flag gives rm license to kill directories, their files, and even their subdirectories. Be very sure you understand what's about to happen before using a command like this:

$ rm -r spelunking

Let's just say that careless use of the -r flag might end your spelunking hobby prematurely.

Previous Lesson: Creating Files and Directories
Next Lesson: Linux Files - Wildcards

[ RETURN TO INDEX ]


   

Comments - most recent first
(Please feel free to answer questions posted by others!)

naresh     (11 Sep 2014, 12:10)
in Linux
$rm -rf <directory name>
this command will remove all the files and folder present in that directory by forcefully and recursively.
naresh.pirati     (11 Sep 2014, 12:08)
in LINUX
$rm -rf <directory name>
this removes all files present in the directory forcefully and recursively
Faheem     (01 Jul 2014, 11:01)
anyone can help me..??
i have some shell files
$RECYCLE.BIN
like this
i can not delete i tried many times.
please help me to remove
i am using HP DV-5laptop.
rani     (03 Jun 2014, 09:51)
i had 1000 files nd directories in a directory...i wannt to delete all except .tmp files in that directory...is there any command pls tell me?????
Alistair     (03 Mar 2014, 22:44)
I have been looking for code to make the rm -r command run only when the Mac has been shutdown or logged off. Would you know of a command?
shahid     (22 Jan 2014, 04:14)
Pls give suggestion, I could not remove files from a folder with command: rm -rf *
all files storage about 200GB. give a direction how to delete the files
Amy     (17 Feb 2013, 02:10)
Please help, purchased HP nc6320 for $99 Linux Mint 14 already installed. Please help me uninstall, I'm trying to install download version of windows 7. none of my school work is compatible with linux mint!!
Aram     (04 Nov 2012, 06:21)
Hi
I want to delete File but getting bash, or file not exist
Question is what should i put in terminals aster $?
-$ rm (File Name).txt
Or should i put address -directory also?
thanks
asha N     (18 Apr 2012, 05:35)
Send me rm program
example file
hanmath pradeep     (15 Mar 2012, 07:59)
Thank you this was very helpful! i had deleted 10000 duplicate records and saved my hard drive......

am leaning linux from last 15days so still am persuing linux and i hav a doubt it may b silly for you all but i would like to know all your opinion on my doubt.....

my doubt is that ............ in oracle it is possible to delete particular records from the table and in the same way is it possible to remove particular data from the specified file..........thanks in advance................

regards,
thanmathpradeep@gmail.com
Ivan Romero     (27 Dec 2011, 11:02)
Thanks for supporting new commers in Linux. I just wanted to subscribe my e-mail. By
Neeharika     (27 Dec 2011, 07:00)
can any one help me in deleting contents of folder but not the folder.
Syeed     (06 Dec 2011, 00:53)
Hello,

Could any one help me in deleting a file with sapces in the name Eg: abc dec.text
lover of man kind     (29 Nov 2011, 13:39)
indeed by deleting files using command line it worked for me. Thanks
Daniel     (26 Nov 2011, 14:27)
Thanks for helping others
junk     (15 Oct 2011, 03:02)
how to show and delete .file (hiddenfile)??
srikanth     (08 Sep 2011, 04:41)
Hi

i am not able to delete more than 25000 xml files at a time using rm -f command in unix so can any help me out to delete the 50000 files at a time in unix .
khotso     (17 Jun 2011, 08:46)
how to remove zPharaoh.exe on linux sever??
zuran     (14 Apr 2011, 01:51)
I can't delete a simple file in the dekstop.
In properties it says that the file type some kind of application (octet/streaming). I did run some iso mounting and after i unmount it the file does not wanna goes away. restart my pc a few times, still it were there. At first i run command rm -fr filename, it says access denied.Then i run chmod -R 775 filename, it still says access denied. so how do i delete this file .. cause when it says streaming .. scared it will streams something else. :)
judge virk     (05 Apr 2011, 02:15)
how to delete a file exactly without using rm command ?
Bob2     (15 Mar 2011, 00:09)
Awesome site Bob. Thanks for all the free help!
mahendra singh rawat     (10 Mar 2011, 04:32)
how i create text file in linux
Paul     (04 Mar 2011, 09:41)
With regard to the question
aruna (01 Dec 2010, 02:18)
I am unable to delete this file 'Valvoline fluid analysis(2).pdf' kindly help me

if the title of your file enclose by apostrophies 'val..etc' in the first place then simple put rm 'the file name' in apostrophies. The command line can't read files with spaces

In your case it was looking for file valvoline
juned kapadia     (25 Feb 2011, 10:57)
hello expert,
How to remove symentac update file (20110213-003-unix.sh) from linux11 & linux10.3 please help me
Cody     (21 Feb 2011, 10:38)
I just wanted to point out that you may need to use sudo to delete files you don't have permission to delete because root does.
Dennis J     (03 Feb 2011, 09:45)
Jason - some information is missing from your post, such as the owner and group names of these "icon" files. However, here is a starter on the "find" command: "find / -name Icon\*". If this returns nothing then you either have no permission to view the file or something is wrong with the name I typed into the command. Note that the "\*" is a wildcard and needs to be escaped (thus the leading backslash) from the command line (shell) interpreter.
If this find command does return the desired list of icon files then run this command: "find / -name Icon\* -exec rm -f '{}' \;" Note that all spaces and punctuation are significant. To be sure you get it right copy all the text between double quote marks and paste them into a Non-Proportional font text editor to see just where the spaces lie.
Good Luck.
Jason     (27 Jan 2011, 18:13)
Here's My dilema. I've got this mac server running on a unix platform as they do and what I'm trying to do is move all the files from the mac server to a windows server. However, there are these "Icon?" files that are appearing all over folders on this server. What I'm looking for is a way to (1) find all these files. I think they are hidden to the system.
The ls output for the file is -rwxrwx---@

2nd. I would like a unix command to remove these files in all folders recursively. I haven't been very successful in finding one, or at least not being able to understand the syntax properly. Any help given would be nice on this.

Thanks in advance.
james     (18 Dec 2010, 08:29)
Hi Experts
I have a problem with my server space becoming to full and I need to delete files from
/var/lib/psa/dumps in order to get it back online, in I am totally new to unix so a bit of an explantion will be really helpful, thank you all very much
Bob Rankin     (01 Dec 2010, 10:11)
@Aruna - Try putting the filename in single quotes.
Pankaj     (01 Dec 2010, 05:22)
try this
rm -rf Valvoline\ fluid\ analysis\(2\).pdf
aruna     (01 Dec 2010, 02:18)
I am unable to delete this file 'Valvoline fluid analysis(2).pdf' kindly help me
Shameem     (30 Nov 2010, 11:25)
Hello Experts,

I have one file with permission stating as ??? and time stamp also ???

how can i remove this file.

I tried numerous steps including force remove etc.,

Any suggestion would be appreciated.

Regards,
Shameem.
Ashish Awasthi     (04 Nov 2010, 02:29)
Thanks for explanation really nice one.
Mark     (20 Sep 2010, 12:20)
Hello Bob thanks for the tutorial.it was very very useful to me. Bob do you have self explanatory tutorial on linux in general,just like this example u gave. This is because i will like to change my main os to Linux (Ubuntu).I would really appreciate it.thanks
deepu     (17 Aug 2010, 01:32)
this explanation is toooo good
very good.......
you done a great help....
Jayti     (16 Aug 2010, 07:06)
You are brilliant.
I love the way you have designed the course material!
Gub     (28 Jul 2010, 04:02)
Dr. Bob thanks for this Site very helpful and you are good educator..i understand what i am doing right now,
Bob Rankin     (10 Jun 2010, 08:28)
@Deb - Sounds like you're low on RAM memory.
Deb     (10 Jun 2010, 08:14)
I am getting a "/bin/rm: Cannot allocate memory" message every time I use the rm -r command :(
Anon     (04 Jun 2010, 13:41)
Thank you this was very helpful! I just deleted 50,000 duplicated files and saved 100 megs of hard drive space. :D

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


Copyright by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.