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:
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop












{ 88 comments… read them below or add one }
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.
How to delete a file named -i in UNIX ?
rm — -i
read the f*****g manual of rm, you can got it.
See how to delete with file starting with a dash:
To remove all files in a directory:
rm -f path/to/my/directory/*.*
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.
Hi,
How to remove all the files in a directory except a single file using rm command.
i m sure, u might have got the answer by now :)
ANS: rm !()
Giri,
You mean the command should be like this
rm !(myfilename) ?? Is it correct?
Thanks.
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.
How to remove all the files inside a directory but not remove directory using rm command?
rm -r dirname
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?
cd /usr/local/www/squid/myfolderrm -rf *
how can i delete file alot of file let say 1 million files
rm -f *
Out of memory.
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
Try with rm -rf filename
Thanks,
$ xargs rm < file.txtreturns multiple errors if there are spaces in your filenames in the file
all filenames that have spaces must be written like this /dir/”my file”/dir… :)
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
Use this:
find /var/www2/ -depth -name ‘CVS’ -exec rm -rf ‘{}’ \; -print
where /var/www2/ is the location you want to recursively go through…
Hi
i need one help. iam having one file in ftp server after processing the file will be chaged how to write that command….
how do i delete a number of txt files from a directory al at once?
rm *.txtI 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.
tnx mate
how to remove a read only file in unix?
chmod 755 filename
rm filename
Thanks
it is very helpful to me..
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.
That should be
echo “” > filename
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.
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
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
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
you can use rm *.txt
will you help me to remove files in a directiory created from sept 1 to sept 18 ..??
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.
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.
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,
Try a shell loop. In this example, cd to /data/files directory and run the command on all .txt files:
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
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
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.
Hi Aram ,
U r Great…Thanx alot for ur comment…it works…appriciated
Thanks for the kind words.
I am glad I was able to help. :-)
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
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.
How can I delete bunch of text file. I want to delete file for perticular range like
xxx24mar2010.txt to xxx30mar2010.txt. Please suggest.
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
how can i delete 5 user’s except admin in terminal?
Hello,
I want to display duplicate lines in a file, what command to display ?
1. Make a copy of the file.
2. Run the following.
$ sort myfile.txt | uniq -uc
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
Try changing user or group ownership using chown for the files in question and then delete them again.
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.
Sry the command is not working for me…
Satheesh,
See my post @ January 21, 2011 (scroll up to see it) and modify the startdate and startdate for your usage.
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 :)
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.
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?
Hi Friends,
I want to delete a particular line in a code in command mode
While this article discusses deleting files, to answer your question.
Deleting line(s) matching pattern
sed ‘/pattern/d’
If you know the line number already, or you can use cat -n to display it, then delete the lines based on the line numbers.
For example to delete lines 2 and 4 from file example.txt
sed -e ’2d;4d’ example.txt
HI : I need to delete all files named .0001 on my server ? Would this work rm *.0001 ???
thanks ..
Yes that should work, however, it will delete all *.0001 in the current directory.
If the command rm -rf couldn’t delete the file,then what is the other command????
If you can’t delete a file using rm -f (r not needed unless if you are deleting directories), then sudo rm or su – root and then delete the file.
If that does not work, or if you get Operation not permitted, try unsetting the immutable attribute.
chattr -i file.txt
And then try again.
How to install tar.bz2 and tar.gz2
How to uninstall tar.bz2 and tar.gz2 .
please help me…
I want to delete a user in solaris but when trying to delete it is showing that user is already logged in how to delete that user.
How to remove “delete” option for file ?
What i mean to say is, even though i am a root user, the file should not have permission even for me to delete it.
chattr +i myfile
Will prevent anyone including root from deleting the file.
chattr -i myfile
Will remove that restriction.
man chattr for more details.
I need to delete a directory with a $ symbol in front of it, using rm commands has not worked any suggestions?
Try:
To remove files without directory, try this.
find | xargs rm
It will remove only file not directory itself.
Aram Iskenderan @ Thanks a lot.. This is a new command I got to know..Thank u once again
@manoj
You’re always welcome.
How do u delete files with a ‘r-xr-xr-x’ attribute? Tks.
Nevermind, able to delete using Putty with root.
hey its works for me … thank you bro :)
Hi,
I want to schedule a cron job with file created by specific user be deleted every night.
Any ideas please?
@ Rick.
Here is a quick a dirty way to do it.
1. Get a bash script something along this.
#!/bin/sh
rm -f /path/to/file
2. Save the file as something like deletefile.sh
3. Make it executable.
chmod +x deletefile.sh
Make sure the path to file and the file name to be deleted are correct, test before you run this without the -f switch in the script. Run the script manually and see if it is working as expected.
4. Move it to a directory that only you or root has access to it.
4. Schedule it to run in Cron.
crontab -e
Add something like.
0 4 * * * /path/to/script/delefile.sh >/dev/null 2>&1
Remember, Cron syntax is as follows:
1 2 3 4 5
m h d month daypofweek
So the above means the script will run @ 4:00 AM everyday. Change the value accordingly.
Save the Cron tab fail.
Or course you can substitute the script by a oneliner in Cron, but the script gives you the ability to enhance this process and check for errors, ..etc.
@ Aram
Thanks for you reply
Can you please tell me what does 2>&1 signify in your cron job and like as I want only specific user files to be deleted like for example files created by user1 only
may be i was not able to get completely from this script.
@Rick
No problem.
>/dev/null 2>&1 redirects both stdout and stderr to /dev/null to prevent email/tty notification. You can remove that part but expect to see an email every Cron runs the script.
Did you try running the script first before adding it to your crontab?If so, what was the output?
Any errors you see?
If not, then check under what user you added the entry in crontab. If you added it as anyone other than that specific user, then either give write permission to the directory/file (bad idea), or simply add the crontab entry for that specific user.
Either use crontab -u user1 -e And then add the entry, or su user1 and then run crontab -e to add the entry.
Either way, make sure that the script is running under a user that has write access to that file/directory.
If you see errors, please post them here.
Aram
Thanks a lot for your help. It was tested successfully with your help!!!
I am a newbie, it was of great help to me
No problem. I am glad it is working for you. If I can be of help, feel free to ask.
Aram,
I have another question for you. I want to set a cronjob which should be doing following:
checking a error file if generated every night in a specific directory and if there is one then sending to 5 users email id?
I think this should be done by creating a shell script and scheduling it every night.
Thanks a lot
Rick,
Looks like my reply to you was held for moderation and was never released. Probably due to usage of example email addresses. I posted another answer. If you have any questions, please let me know.
Looks like the original reply was held for moderation, not sure why it was not released. Let me try again.
Rick,
Let’s say that your file is named errofile.txt
Your script would look something like the following:
#!/bin/sh
$FILE=”/path/to/errofile.txt” #Change the file name here
if [ -f $FILE ];
then
echo “Error file found. Please see attached” | mutt -a $FILE -s “Error File” — recipient1 recipient2 recipient3 recipient4 recipient5
fi
Replace each recipient with the full email for each one.
Save the above script in a safe directory and make sure the user that the script will run under will have access to it, name it something like errorfile.sh
Note: To avoid email being rejected, make sure to set .muttrc for that specific user to set envelope from address.
Add the following lines to .muttrc for the user the script will run under (change sender to the email address to be used.)
set from=”sender”
set envelope_from=yes
If .muttrc under the home directory for that user does not exist, just add it.
touch .muttrc
nano .muttrc
And then add the above two lines.
Save and close.
Now in your cron, add the entry.
crontab -e
0 1 * * * /path/to/script/errorfile.sh >/dev/null 2>&1
This means that errorfile.sh will run daily at 1:00 AM. Change the time to when you want the script to run.
Save and exit.
Before you do anything, manually test.
If you have any questions, let me know.