Linux / UNIX: Delete a file

by Vivek Gite on January 27, 2006 · 60 comments

How do I delete a file under Linux / UNIX / BSD / AIX / HP-UX operating system?

To remove a file or directory in Linux, FreeBSD, Solaris or Unixish oses use the rm command.

rm command syntax

rm (short for remove) is a Unix / Linux command which is used to delete files from a filesystem. Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). The syntax is as follows:

rm -f -r {file-name}

Where,

  • -f: Forcefully remove file
  • -r: Remove the contents of directories recursively

Remove or Delete a File

To remove a file called abc.txt type the following command:
$ rm abc.txt

To remove all files & subdirectories from a directory (MS-DOS deltree like command), enter:
$ rm -rf mydir

To remove empty directory use rmdir and not rm command:
$ rmdir mydirectory

Read a List of All Files To Delete From a Text File

The rm command is often used in conjunction with xargs to supply a list of files to delete. Create a file called file.txt:
$ cat file.txt
List of to delete:

file1
/tmp/file2.txt
~/data.txt

Now delete all file listed in file.txt, enter:
$ xargs rm < file.txt

Never run rm -rf / as an administrator or normal UNIX / Linux user

WARNING! These examples will delete all files on your computer if executed.

$ rm -rf /
$ rm -rf *

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Unix disasters. The rm -rf / variant of the command, if run by an administrator, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands.

See also:

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 60 comments… read them below or add one }

1 abdul khader Naik February 21, 2008

Will you please tell me the command(in unux/linux) which delete the content of directory without deleteing the directory itself.
other than rm -rf(it will delete directory too) that i dont want.

Reply

2 user December 7, 2011

How to delete a file named -i in UNIX ?

Reply

3 iCyOMiK December 17, 2011

rm — -i
read the f*****g manual of rm, you can got it.

Reply

4 Vivek Gite December 17, 2011

See how to delete with file starting with a dash:

rm -- '-i'

Reply

5 Robert Dyke December 18, 2008

To remove all files in a directory:

rm -f path/to/my/directory/*.*

Reply

6 Mad93 June 16, 2009

It should be:

rm -f path/to/my/directory/*

Since in linux files doesn’t need file extension. Also, if you want to remove folders inside you have to add the -r flag.

Reply

7 Suresh June 17, 2009

Hi,

How to remove all the files in a directory except a single file using rm command.

Reply

8 Giri April 21, 2010

i m sure, u might have got the answer by now :)
ANS: rm !()

Reply

9 ken July 29, 2009

Hi UNIX expert,

For the command referred to, can a user with world-writeable permission uses the command to remove directories in O/S? Can a user remove a whole directory even though he/she does not have world-writeable permission to the files under the directories?

Thank you so much.

Reply

10 Ramin September 11, 2009

How to remove all the files inside a directory but not remove directory using rm command?

Reply

11 biswajit August 27, 2010

rm -r dirname

Reply

12 Ramin September 11, 2009

How to remove all the files and directory inside a /usr/local/www/squid/myfolder directory but not remove directory /usr/local/www/squid/ using rm command?

Reply

13 Vivek Gite September 11, 2009

cd /usr/local/www/squid/myfolder
rm -rf *

Reply

14 romy September 24, 2009

how can i delete file alot of file let say 1 million files
rm -f *
Out of memory.

Reply

15 swati November 17, 2009

how to delete a file which is being generated on its own.
have deleted the source from which its generating but its still generating the file.
have tried all th commands like rm & rm -f still no effect. please help

Reply

16 poorna February 5, 2011

Try with rm -rf filename

Thanks,

Reply

17 Mimor February 27, 2010

$ xargs rm < file.txt
returns multiple errors if there are spaces in your filenames in the file

Reply

18 DevX March 25, 2011

all filenames that have spaces must be written like this /dir/”my file”/dir… :)

Reply

19 Bill March 4, 2010

Hi,
I’m a linux user, I have a directory called “projects” in which there are number of subdirectories, my problem is that I have a directory called “CVS” in every sub-directory (within “projects”) which I want to delete using CLI (command line interface). Is it possible to use one command (or may be very few commands) to find and delete the “CVS” directory?
Thanks

Reply

20 Charles Dupin de Saint Cyr May 9, 2010

Use this:

find /var/www2/ -depth -name ‘CVS’ -exec rm -rf ‘{}’ \; -print

where /var/www2/ is the location you want to recursively go through…

Reply

21 sreenivasulu kaipu March 18, 2010

Hi
i need one help. iam having one file in ftp server after processing the file will be chaged how to write that command….

Reply

22 linda April 27, 2010

how do i delete a number of txt files from a directory al at once?

Reply

23 Vivek Gite April 27, 2010

rm *.txt

Reply

24 Carol Pominville May 25, 2010

I have a folder, lets call it a. Inside of a, is another folder (b) and some files. What command would I use to delete all the files inside of the a folder, but not touch b?

I tried what I saw above, rm !() but was not sure..i put the name of the path of folder b inside the brackets. I then tried, just the letter b… all to no avail.

Thanks.

Reply

25 vf June 8, 2010

tnx mate

Reply

26 mitesh June 14, 2010

how to remove a read only file in unix?

Reply

27 biswajit August 27, 2010

chmod 755 filename
rm filename

Reply

28 lalith sanjaya July 13, 2010

Thanks

it is very helpful to me..

Reply

29 marzi July 15, 2010

How can I delete the content of the file without opening the file?
I used the “echo -n > YOURFILE” but another file with the same name created.

Reply

30 Aram Iskenderian October 23, 2010

That should be

echo “” > filename

Reply

31 Gautam August 19, 2010

Hi,
Could someone help me in preparing a script to delete a file from a directory after the completion of a process(Informatica job)?

Thank you.

Reply

32 rick September 20, 2010

I am getting an error that says root directory is full. How do I safely remove all the contents of this directory. Do I need to do a copy backup first???
Thanks
Rick

Reply

33 Amit October 4, 2010

I need to deleate some data from the UNIX file but am not able to do that.Can anyone please help me out on this?
Thanks
amit

Reply

34 james February 14, 2011

for deleting or writing data in files. follow this
1. type vi “yourfilename”
(now the content in your file will be disply as a word file)
2. now you can use two commands to delete data
a.place the curser in the line, press dd(d twice) to delete one line.
b.place curser in the character and press x to delete character.
nb : for moving (placeing) curser use h, j, k , l .. to go up,down, sides ..

hope this will help you … this is just a basic way
if u want to learn more command to edit files … try search for “unix vi” . there are lots of results avaible there

Reply

35 Neeraj November 22, 2010

you can use rm *.txt

Reply

36 jestin December 17, 2010

will you help me to remove files in a directiory created from sept 1 to sept 18 ..??

Reply

37 Aram Iskenderian January 21, 2011

Warning: Test this on a NON production computer/server or in a dummy directory before you run it.

This assumes that you want to start the delete process on files that had date
stamp starting 09-10-2010 @ 12:00 AM and ending 09-18-2010 23:59
If not, modify the startdate and enddate accordingly.

#!/bin/sh

startdate=”201009010000″
enddate=”201009182359″
touch -t $startdate ./startdatefiles
touch -t $enddate ./enddatefiles
find ./ -type f -newer ./startdatefiles ! -newer ./enddatefiles -ls

Once you test the above script and find it listing the files that needed to be deleted correctly, add -exec rm “{}” \; argument to complete the task.

You can also modify it to change the startdate and enddate variables programatically and even use this in your crontab to automate the process.

Hope this helps.

Reply

38 steven January 12, 2011

Hi,
I want to create script for the following scenario:

The requirements are as below:-

1.) Create a cash script to remove STDF files with invalid lot ID.
2.) Script to be created > /ebsxe/ebs25/users/test/script
3.) Directory of the files > /ebsxe/ebs25/users/test/STDF4

The examples of files in STDF4:
S_20101202044129_MQB021M_B11P_N.st4__ba5651-s_BTS721L1_BAT
S_20101202031130_MQB010J_Q11P_N.st4__ba5651-s_BTS721L1_BAT
S_20101213061928_2_B11P_N.st4__ba56513-s_S1098C3F_BAT

4.) If the character between 2nd underscore and 3rd underscore (eg:MQB021M, MQB010J, 2) less than 8 character, it must be delete automatically.

Kindly please help.

Reply

39 poorna February 5, 2011

Q:What is commnd to delete all the information in file’s with out deleting file in a directory.

I know only with echo command we can delete all the information in one file.
A directory contain 10 files. I need to delete all the information which is in those file and files should be there in that directory with ZERO memory Size.

Thanks & Reards,

Reply

40 Vivek Gite February 5, 2011

Try a shell loop. In this example, cd to /data/files directory and run the command on all .txt files:

cd /data/files
for f in *.txt
do
   >"$f"
done

Reply

41 james February 14, 2011

hi, i have a folder, where some production files are automatically creates and after a few time it will be automatically delets. what my job is, to copy down all the new files created before it been deleted … i am facing a problem sometimes i am in otherjobs so i cant copy files. i am losing files. so will anybody here please help me to write a script to copy all the files to another dictionary. which runs in every 15 min. all the files have a common extension “.ser”
thanks in advance

Reply

42 vickram March 24, 2011

Hi Experts ,

The comments are really helpful. may i know how to delete files by year?i have one dir containts 33million files. i want to delete it by year start from 2009…plz help

Reply

43 Aram Iskenderian March 25, 2011

Hi vickram,

Take a look at my response to another post here by jestin dated January 21, 2011.
All you have to do is just specify the dates.

Reply

44 vickram March 25, 2011

Hi Aram ,

U r Great…Thanx alot for ur comment…it works…appriciated

Reply

45 Aram Iskenderian March 25, 2011

Thanks for the kind words.
I am glad I was able to help. :-)

Reply

46 Phil Noutsis March 24, 2011

How can I remove a unix file of the type #payroll.txt# ? I tried : rm #payroll.txt#, but it tells me that I have to supply more arguments. Any suggestion?
Thanks,
Phil

Reply

47 Aram Iskenderian March 25, 2011

Phil,
The reason you are getting that error is because of the #.

Try the following.
rm \#payroll.txt\# (you can force it not to prompt you by specifying -f)
If you have files with numbers in them, you can always consider ?* wildcards.

Reply

48 Somnath March 28, 2011

How can I delete bunch of text file. I want to delete file for perticular range like
xxx24mar2010.txt to xxx30mar2010.txt. Please suggest.

Reply

49 Andrej April 5, 2011

Hi,

I have created a long list (>4.000 entries) of redundant files that need to be deleted. Typing the command:
xargs rm < iTunesDeleteList

produces an error message:
xargs: unterminated quote.

I tried to replace spaces with "\ " but it didn't work. What should I do to make it work?Typical entry:
/Users/me/Music/iTunes/iTunes Media/Music/B.B. King/King Of The Blues [Disc 1]/1-23 Don't Answer The Door, Parts One And Two 1.mp3

Thanks,
Andrej

Reply

50 roland May 21, 2011

how can i delete 5 user’s except admin in terminal?

Reply

51 araa June 1, 2011

Hello,
I want to display duplicate lines in a file, what command to display ?

Reply

52 Aram Iskenderian July 6, 2011

1. Make a copy of the file.
2. Run the following.

$ sort myfile.txt | uniq -uc

Reply

53 Rohit Kumar July 5, 2011

Hi
When i am trying to delete a files using rm,rm -f,rm -rf the files still exists in the directory.. I tried deleting it by changing permissions but not done.. Any help is appreciated

Reply

54 Aram Iskenderian July 6, 2011

Try changing user or group ownership using chown for the files in question and then delete them again.

Reply

55 satheesh July 11, 2011

How to delete few different files using unix rm command. I want to delte thousands of files which are older than 6 months.
Below i gave the command which i have been using the files. But it does work.
Command:
find /My Dir -name ’1309927_sat_0407.price.xls 131011719_sat_0807.price.xls’ -mtime +180 -exec rm -f {} \;

Please help me.

Reply

56 satheesh July 11, 2011

Sry the command is not working for me…

Reply

57 Aram Iskenderian August 11, 2011

Satheesh,

See my post @ January 21, 2011 (scroll up to see it) and modify the startdate and startdate for your usage.

Reply

58 Neelesh August 11, 2011

Hi ,

After ls -lrt in a directory , it will give around 300 files with the most recently created/used file at the bottom. I want to delete the top 120 files from the directory. Can you please help. Thanks :)

Reply

59 Ocean November 23, 2011

I have a problem that I need a solution to.

How can I find the largest file and remove it with naming the file in Linux/Unix?

Any help would be appreciated.

Reply

60 john January 17, 2012

is it possible for rm command to list what files are being deleted without being in interactive mode

for example if I want to delete contents of a directory

rm /directory/*

is there a way to list which files are being deleted WHILE they’re being deleted?

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 7 + 7 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: