how to delete a file in linux
How do I delete a file under a Linux / UNIX / *BSD / AIX / HP-UX operating system using command line options?
To remove or delete a file or directory in Linux, FreeBSD, Solaris, macOS, or Unix-like operating systems, use the rm command or unlink command. This page explains how to delete a given file on a Linux or Unix like system using the command line option.
| Tutorial details | |
|---|---|
| Difficulty level | Easy |
| Root privileges | No |
| Requirements | rm and unlink command on Linux or Unix |
| Est. reading time | 4 minutes |
Syntax: rm command to remove a file
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 to delete the specified files and directories:
rm {file-name}
rm [options] {file-name}
unlink {file-name}
rm -f -r {file-name}
Where,
- -f : Forcefully remove file
- -r : Remove the contents of directories recursively
When rm command used just with the file names, rm deletes all given files without confirmation by the user.
Warning : Be careful with filenames as Unix and Linux, by default, won't prompt for confirmation before deleting files. Always keep verified backups of all critical files and data.
Unix Remove or delete a file example
Say you have a file named abc.txt and you want to remove it:
$ rm abc.txt
Linux delete multiple files
Delete three files named foo.mp4, bar.doc, and demo.txt, run:
Patreon supporters only guides 🤓
rm foo.mp4 bar.doc demo.txt ls
Linux recursively delete all files
Remove all files and sub-directories from a directory (say deltree like command from MS-DOS world), enter:
$ rm -rf mydir
Linux delete a file and prompt before every removal
To request confirmation before attempting to remove each file pass the -i option to the rm command:
$ rm -i filename
Sample outputs:
Gif 01: rm command demo
Pass the -I option to prompt only once before removing more than three files but still providing protection against many mistakes at the cli:
$ rm -I foo.conf bar.conf resume.doc cakeday.png
$ rm -I -r -f ~/olddata/
Force rm command to explain what is being done with file
Pass the -v option as follows:
$ rm -v moiz.list.txt bios-updates.doc
removed 'moiz.list.txt'
removed 'bios-updates.doc'
How to delete empty directories
To remove empty directory use rmdir command and not the rm command:
$ rmdir mydirectory
$ rmdir dirNameHere
$ rmdir docs
How to 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
How do I delete a file named -foo.txt or a directory named -bar?
To delete a file called -foo.txt:
rm -- -foo.txt
OR
rm -- ./-foo.txt
To delete a directory called -bar:
rm -r -f -- -bar
The two -- dashes tells rm command the end of the options and rest of the part is nothing but a file or directory name begins with a dash.
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.
Using wildcards (globbing) with rm command
A wildcard is nothing but a symbol to match an unknown character or set of characters. For instance, the asterisk ( * ) and the question mark ( ? ) are examples of a wildcard. Long ago, in UNIX V6, there was a program /etc/glob that would expand wildcard patterns. Soon afterward, this became a shell built-in. Thus, we can use wildcard matching for removing files in bulk.
Say I can type the following rm command. It will remove all files starting with a character such as abc.txt aaa.doc amazing_pic.jpg, and so on:
rm a*
Want to delete all files with .pl extension? Try
rm *.pl
The asterisk ( * ) represents any number of unknown characters and is very powerful. On the other hand, the question mark ( ? ) represents only one unknown character. For instance, to remove all files pattern such as aa, ab, ac, and so on, you can type:
rm a?
Please note that these wildcards are used by nearly any Linux or Unix-like system commands and are not limited to the rm. Be careful with wildcards, as you might end up deleting all or unwanted files. Do check wildcards using the ls command before running the rm command. For example:
ls *.txt
Then if you see desired outputs, run:
rm *.txt
Conclusion
You learned how to delete files on Linux and Unix-like operating systems. Here are all important options for GNU rm command (read man page here)
| Option | Description |
|---|---|
| -f | Ignore nonexistent files and arguments, never prompt |
| -i | Prompt before every file removal |
| -I | Prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes --interactive[=WHEN] prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always |
| --one-file-system | when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument |
| --no-preserve-root | do not treat '/' specially |
| --preserve-root[=all] | do not remove '/' (default); with 'all', reject any command line argument on a separate device from its parent |
| -r | remove directories and their contents recursively |
| -R | same as above |
| -d | rmove empty directories |
| -v | Explain what is being done |
Do read the following man pages using the man command or help command:
man rm
man 7 glob
man 3 glob
See also:
- How to delete a file securely on a Linux
| Category | List of Unix and Linux commands |
|---|---|
| Documentation | help • mandb • man • pinfo |
| Disk space analyzers | df • duf • ncdu • pydf |
| File Management | cat • cp • less • mkdir • more • tree |
| Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
| Linux Desktop Apps | Skype • Spotify • VLC 3 |
| Modern utilities | bat • exa |
| Network Utilities | NetHogs • dig • host • ip • nmap |
| OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
| Package Manager | apk • apt |
| Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
| Searching | ag • grep • whereis • which |
| Shell builtins | compgen • echo • printf |
| Text processing | cut • rev |
| User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
| WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
how to delete a file in linux
Source: https://www.cyberciti.biz/faq/howto-linux-unix-delete-remove-file/
Posted by: griffininlyrib.blogspot.com

0 Response to "how to delete a file in linux"
Post a Comment