How to delete a file under Linux / UNIX / *BSD / AIX / HP-UX using command line options?
To remove or delete a file or directory on Linux, FreeBSD, Solaris, MacOS, or Unix-like operating systems, use the rm command or the unlink command. This page explains how to delete this file on a system such as Linux or Unix using the command line option.
Syntax: rm Removal command for file
rm (abbreviation for delete) is a Unix/Linux command for deleting files from the file system. Normally, most file systems require permission to write to the parent directory to delete a file (and permission to enter the directory first). The syntax consists of deleting the files and folders specified as follows
rm {File name}
rm [Options] {File name}
unlock {File name}
rm -f -r {File name}
Wo,
- -f : Obligation to delete a file
- -r : Recursively deleting the contents of the folder
If rm is used with file names only, rm will delete all specified files without confirmation from the user.
Look out! Look out! Note file names such as Unix and Linux. By default, no confirmation is requested for deleting files. Always keep validated backups of all your important files and data.
UnixDelete or delete an example file
Suppose you have a file named abc.txt and you want to delete it:
$ rm abc.txt
Linux removes multiple files
Delete the three files called foo.mp4, bar.doc and demo.txt and start :
rm foo.mp4 bar.doc demo.txt ls |
rm foo.mp4 bar.doc demo.txt
ls
Linux recursively removes allfiles.
Remove all files and subdirectories from the directory (e.g. the deltree command as in the MS-DOS world), type:
$ rm -rf mydir
Linux deletes the file and the request for each deletion
To request confirmation before attempting to delete each file, give the rm command the option -i:
$ rm -i filename
Example output :
Gif 01: rm command demo
Transmit -I Option to ask only once for deleting more than three files, but providing protection against many errors in the clip at the same time:
$ rm -I foo.conf bar.conf resume.doc cakeday.png
$ rm -I -r -f ~/olddata/ rm command.
Forces the rm command to explain what to do with file.
Transfer of the -v option as follows
$ rm -v moiz.list.txt bios-updates.doc
‘moiz.list.txt’ deleted
‘bios-updates.doc’ deleted
How do I delete emptyfolders?
To delete an empty directory, use the rmdir command, not rm:
$ rmdir my directory
$ rmdir dirNameHere
$ rmdir docs
To read the list of all files deleted from the text file
The rm command is often used in combination with xargs to create a list of files to be deleted. Create a file named file.txt:
$ cat file.txt
list to remove:
file1
/tmp/file2.txt
~/data.txt
Now delete all files in the .txt file, type:
$ xargs rm
How do I delete a file named -foo.txt or a folder named -bar?
To delete a file named -foo.txt:
rm — -foo.txt
or
rm — ./-foo.txt
To delete a directory named -bar:
rm -r -f — -bar
The — dashes command tells rm the end of options and the rest is nothing more than the name of the file or directory beginning with a hyphen.
Never run rm -rf / as administrator or normal user of UNIX / Linux.
ATTENTION! These examples delete all files on your computer when they are executed.
$ rm -rf /
$ rm -rf *
rm -rf (otherwise, rm -rf /, rm -rf * etc.) is often used in jokes and anecdotes about Unix disasters. A variant of the rm -rf / command, when run by the administrator, removes the contents of any writable mount file system on the computer. Don’t try to carry out those orders.
Conclusion
You have learned how to delete files under Linux and Unix-like operating systems. Here are all the important options of the GNU rm command (read the manual here)
Option | Name |
-f | Ignore non-existent records and arguments, never demand |
-i | Request before each file deletion |
-I | index once for deleting more than three files or recursive; less intrusive than -i, but protects against most errors – interactive index [=When] after WHEN: never, once (-I) or always (-i); no index WHEN always |
-a file system | if you delete the hierarchy recursively, you skip all folders in the file system, except for the corresponding command line argument |
— …a carrot without reservation. | dissociate oneself from / in particular |
-Root reserve [=all] | not clear / (default); use anything to reject a command line argument on a device separated from the motherboard. |
-r | Removing directories and their content recursively |
-R | for the purposes of the foregoing |
-d | browse empty catalogues |
-v | Explain what you’re doing. |
RECOMMENDATIONS
Published: Vivek gears
The author is the creator of nixCraft and an experienced system administrator, DevOps engineer and Linux/Unix shell script trainer. Receive the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly by email.how to delete a file in linux terminal,linux delete all files in current directory,remove all files in a directory linux recursively,shell script to delete files in a directory,how to delete binary file in linux,copy file linux,how to delete a file system in linux,how to find and delete files in linux