Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix command deleted every directory even though not specified

I am very new to the unix. I ran the following command.

ls -l | xargs rm -rf bark.*

and above command removed every directory in the folder.

Can any one explained me why ?

like image 664
arpanoid Avatar asked May 10 '12 23:05

arpanoid


People also ask

How do I delete a directory in Unix with all files?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

Which command is used to remove the directory even if it is not empty?

To remove a directory that is not empty, use the rm command with the -r option for recursive deletion.

Which Unix command sequence deletes a directory and everything inside it?

Syntax: rm command to remove a file-r : Remove the contents of directories recursively.


1 Answers

The -r argument means "delete recursively" (ie descend into subdirectories). The -f command means "force" (in other words, don't ask for confirmation). -rf means "descend recursively into subdirectories without asking for confirmation"

ls -l lists all files in the directory. xargs takes the input from ls -l and appends it to the command you pass to xargs

The final command that got executed looked like this:

rm -rf bark.* <output of ls -l>

This essentially removed bark.* and all files in the current directory. Moral of the story: be very careful with rm -rf. (You can use rm -ri to ask before deleting files instead)

like image 87
dwurf Avatar answered Oct 10 '22 11:10

dwurf