To delete a record, recordsdata, or a folder in Linux, the Linux command to make use of is both rm or 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.

The 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 rm and rmdir.

The best way to use rm

1. Delete a record
rm [file]

The rm command gets rid of a unmarried record. To do that, sort rm adopted through the identify of the record you need to delete.

Instance:

The next command is used to take away a particular record named instance.txt positioned inside of a listing named myfolder.

rm myfolder/instance.txt

Right here’s an instance of the way it works:

  1. Assume you have got a listing named myfolder and inside of that listing, there’s a record named instance.txt.
  2. You run the command rm myfolder/instance.txt.
  3. The record instance.txt inside of myfolder will likely be deleted, and there will likely be no output message displayed within the terminal through default.
  4. If you happen to attempt to get admission to the record instance.txt once 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:

myfolder/
2. Take away recordsdata with out affirmation
rm -f [file]

This feature lets in customers to take away write-protected recordsdata with out affirmation.

Instance:

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

Because the -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]

Use the rm command with filenames as arguments to take away more than one recordsdata immediately.

Instance:

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 file1.txt, file2.txt, and file3.txt are deleted, and your listing now seems like this:

otherfile.txt
4. Show output message
rm -v [filename]

The -v (verbose) possibility means that you can get details about what’s being got rid of.

Instance:

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.

Instance:

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

After this, instance.txt will likely be deleted when you showed with y.

The best way to use rmdir

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 command:

$ rmdir [OPTION...] [DIRECTORY...]
1. Take away a listing
rmdir [directory]

Use this command to take away a listing, however it’ll most effective be got rid of whether it is empty.

Instance:

Assume you have got a listing named myfolder and it’s empty. While you run the command:

rmdir myfolder

The listing 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

Or if 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.

Instance:

The rmdir folder1 folder2 folder3 command in Linux makes an attempt to take away the directories named folder1, folder2, and 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:

If folder1, folder2, and 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, folder1 and 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]

The -p possibility gets rid of the required listing and its guardian directories.

Instance:

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

If each folder_a and 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_a or 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:
Listing Operations rmdir · cd · pwd
Record Operations cat · cp · dd · much less · ls · mkdir · mv · tail · tar · zip
Record Gadget Operations chown · mkfs
Networking ping · curl · wget · iptables
Seek and Textual content Processing in finding · grep · sed · whatis
Gadget Data and Control env · historical past · best · who
Person and Consultation Control display screen · su · sudo

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/

[ continue ]