rm stands for remove. It is a command that allows you to remove files, folders, symlinks from the system. Deleted files and folders will not be moved to the recycle bin, but the rm command will delete them immediately. You must be careful before using this command.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux system commands queries.
In this context, we shall look into how to check the manual by using the rm command in Linux.
Basically, the rm command removes (deletes) files.
rm removes each file specified on the command line. By default, it does not remove directories.
When rm is executed with the -r or -R options, it recursively deletes any matching directories, their subdirectories, and all files they contain.
The removal process unlinks a file name in a filesystem from its associated data, and marks that space on the storage device as usable by future writes. In other words, when you remove a file, the data in the file isn't changed, but it's no longer associated with a file name.
The data itself is not destroyed, but after being unlinked with rm, it becomes inaccessible. Remove your files wisely! It's not like putting something in the Windows Recycle Bin; once you rm a file or directory, there is no way to undo it.
rm command syntax is given below:
$ rm [option]... file...
$ rm [-f | --force] {[-i | --interactive[=always]] | [-I | --interactive=once] |
[--interactive=never]} [--one-file-system] [--no-preserve-root |
--preserve-root] [-r | -R | --recursive] [-d | --dir] [-v | --verbose]
FILE...
$ rm --help
$ rm --version
For example, I have 2 files: a.txt and b.txt:
Now, To remove file a.txt, run the below command:
$ rm a.txt
This will remove a.txt file from the system.
Or you can remove multiple files, execute:
$ rm b.txt datefile.txt
1. -i: must confirm before deleting
For example, We will remove file c.txt:
$ rm -i c.txt
You must press y to confirm.
2. -r: remove a directory
For example, We will remove directory snort3:
$ rm -r snort3
3. Remove all files in the working directory. If it is write-protected, you will be prompted before rm removes it:
$ rm -f *
4. Remove all files in the working directory. rm will not prompt you for any reason before deleting them:
$ rm -i *
5. Attempt to remove every file in the working directory, but prompt before each file to confirm:
$ rm -I *
6. Remove every file in the working directory; prompt for confirmation if more than three files are being deleted.
$ rm -r mydirectory
Remove the directory mydirectory, and any files and directories it contains. If a file or directory that rm tries to delete is write-protected, you are prompted to make sure you want it deleted.
$ rm -rf mydirectory
This article covers how to use the rm command in Linux. In fact, the rm command is used for removing/deleting files and directories.
How to remove directories using rm command?
If you are trying to remove a directory, then you need to use the -r command line option. Otherwise, rm will throw an error saying what you are trying to delete is a directory:
$ rm -r [dir name]
For example:
$ rm -r testdir
How to make rm prompt before every removal ?
If you want rm to prompt before each delete action it performs, then use the -i command line option:
$ rm -i [file or dir]
For example, suppose you want to delete a directory 'testdir' and all its contents, but want rm to prompt before every deletion, then here's how you can do that:
$ rm -r -i testdir