To delete a record, recordsdata, or a folder in Linux, the Linux command to make use of is both
rmdir. The rm command stands for “take away” and is used to delete recordsdata and directories. Via the use of more than a few choices, you’ll take away recordsdata, directories, or even the contents of directories. As an example,
rm filename will delete a record, whilst
rm -r directoryname will delete a listing and all its contents.
rmdir command stands for “take away listing” and is in particular used to delete empty directories. If a listing accommodates any recordsdata or subdirectories, the
rmdir command won’t take away it, and an error message will likely be displayed. Against this, the
rm command with the
-r possibility can delete non-empty directories. Necessarily,
rm is extra flexible, whilst
rmdir is extra specialised for casting off empty directories.
On this submit, we take a look at alternative ways to make use of
The best way to use
1. Delete a record
rm command gets rid of a unmarried record. To do that, sort
rm adopted through the identify of the record you need to delete.
The next command is used to take away a particular record named
instance.txt positioned inside of a listing named
Right here’s an instance of the way it works:
- Assume you have got a listing named
myfolderand inside of that listing, there’s a record named
- You run the command
- The record
myfolderwill likely be deleted, and there will likely be no output message displayed within the terminal through default.
- If you happen to attempt to get admission to the record
instance.txtonce more, you’ll get an error message like
No such record or listing.
Prior to working the command, when you had:
myfolder/ └── instance.txt
After working the command, the construction could be:
2. Take away recordsdata with out affirmation
rm -f [file]
This feature lets in customers to take away write-protected recordsdata with out affirmation.
Assume you have got a record named
file1.txt on your present listing, and you need to delete it. You’ll run the next command:
rm -f file1.txt
-f possibility is used, there will likely be no affirmation recommended, and the record will likely be deleted right away. There will likely be no output displayed within the terminal if the operation is a success. If you happen to attempt to view the contents of the listing in a while, you’ll in finding that
file1.txt is now not there.
3. Delete more than one recordsdata
rm [file1] [file2] [file3]
rm command with filenames as arguments to take away more than one recordsdata immediately.
Prior to working the command, let’s say you have got the next recordsdata on your listing:
file1.txt file2.txt file3.txt otherfile.txt
You run the command
rm file1.txt file2.txt file3.txt.
After working the command, the recordsdata
file3.txt are deleted, and your listing now seems like this:
4. Show output message
rm -v [filename]
-v (verbose) possibility means that you can get details about what’s being got rid of.
While you execute the command
rm -v instance.txt, right here’s the output you’ll get:
got rid of 'instance.txt'
On this instance, the record
instance.txt is deleted, and the gadget prints a message confirming that the record has been got rid of. If the record does now not exist, an error message like
rm: can not take away 'instance.txt': No such record or listing could be displayed as a substitute.
5. Instructed for affirmation prior to deleting a record
rm -i [filename]
This feature is used to request affirmation prior to deleting a record. Typing
y (sure) confirms, typing
n (no) stops.
Let’s say you have got a record named
instance.txt and also you run the command:
rm -i instance.txt
The gadget will recommended you with a message like:
take away common record 'instance.txt'?
You’ll then want to sort
y (for sure) or
n (for no) to substantiate or deny the deletion. If you happen to sort
y and press Input, the record
instance.txt will likely be deleted. If you happen to sort
n, the record will stay untouched.
take away common record 'instance.txt'? y
instance.txt will likely be deleted when you showed with
The best way to use
This command gets rid of listing in addition to recordsdata throughout the listing. There isn’t important distinction with the
rm -r command except for that it cannot be used to take away a record.
Normal syntax for
$ rmdir [OPTION...] [DIRECTORY...]
1. Take away a listing
Use this command to take away a listing, however it’ll most effective be got rid of whether it is empty.
Assume you have got a listing named
myfolder and it’s empty. While you run the command:
myfolder will likely be deleted, and there will likely be no output message if the operation is a success.
Then again, if
myfolder isn’t empty or does now not exist, you’ll obtain an error message. As an example, if
myfolder accommodates recordsdata or subdirectories, chances are you’ll see:
rmdir: failed to take away 'myfolder': Listing now not empty
myfolder does now not exist:
rmdir: failed to take away 'myfolder': No such record or listing
2. Delete more than one directories
rmdir [folder1] [folder2] [folder3]
This command means that you can delete a number of directories immediately, however all of them will have to be empty.
rmdir folder1 folder2 folder3 command in Linux makes an attempt to take away the directories named
folder3. This command will most effective be triumphant if all 3 of those directories are empty; in a different way, an error message will likely be displayed.
Right here’s an instance of the way this may paintings:
folder3 are all empty directories, the command will take away them, and there will likely be no output message.
If any of those directories aren’t empty or don’t exist, an error message will likely be displayed for every problematic listing.
Instance of the output if
folder2 isn’t empty:
rmdir: failed to take away 'folder2': Listing now not empty
On this case,
folder3 would nonetheless be got rid of in the event that they have been empty, however
folder2 would stay untouched.
3. Take away guardian directories
rmdir -p [directory-path]
-p possibility gets rid of the required listing and its guardian directories.
Right here’s an instance of the way the command
rmdir -p folder_a/folder_b would paintings:
Assume you have got a listing construction like this:
folder_a └── folder_b
folder_b are empty, working the command
rmdir -p folder_a/folder_b will take away
folder_b first, after which, since
folder_a turns into empty, it’ll take away
folder_a as smartly.
If there have been any recordsdata or subdirectories inside of
folder_b, the command would now not take away them, and you could possibly obtain an error message like:
rmdir: failed to take away 'folder_a/folder_b': Listing now not empty
Within the a success case, there could be no output, and each directories could be deleted.
Extra Linux instructions:
|Record Gadget Operations||
|Seek and Textual content Processing||
|Gadget Data and Control||
|Person and Consultation Control||
The submit The best way to Delete Information and Folers in Linux gave the impression first on Hongkiat.WordPress Website Development Source: https://www.hongkiat.com/blog/linux-command-rm-rmdir/