Search code examples
linuxbashunixxargsrm

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 ?


Solution

  • The ls -l command gave a list of all the subdirectories in your current present-working-directory (PWD).

    The rm command can delete multiple files/directories if you pass them to it as a list.

    eg: rm test1.txt test2.txt myApp will delete all three of the files with names:

    test1.txt
    test2.txt
    myApp
    

    Also, the flags for the rm command you used are common in many a folly.
    rm -f - Force deletion of files without asking or confirming
    rm -r - Recurse into all subdirectories and delete all their contents and subdirectories

    So, let's say you are in /home/user, and the directory structure looks like so:

     /home/user
    |->dir1
    |->dir2
    `->file1.txt
    

    the ls -l command will provide the list containing "dir1 dir2 file1.txt", and the result of the command ls -l | xargs rm -rf will look like this:
    rm -rf dir1 dir2 file1.txt

    If we expand your original question with the example above, the final command that gets passed to the system becomes: rm -rf di1 dir2 file1.txt bark.*

    So, everything in the current directory gets wiped out, so the bark.* is redundant (you effectively told the machine to destroy everything in the current directory anyway).

    I think what you meant to do was delete all files in the current directory and all subdirectories (recurse) that start with bark. To do that, you just have to do:
    find -iname bark.* | xargs rm

    The command above means "find all files in this directory and subdirectories, ignoring UPPERCASE/lowercase/mIxEdCaSe, that start with the characters "bark.", and delete them". This could still be a bad command if you have a typo, so to be sure, you should always test before you do a batch-deletion like this.

    In the future, first do the following to get a list of all the files you will be deleting first to confirm they are the ones you want deleted.
    find -iname bark.* | xargs echo

    Then if you are sure, delete them via
    find -iname bark.* | xargs rm

    Hope this helps.

    As a humorous note, one of the most famous instances of "rm -rf" can be found here: https://github.com/MrMEEE/bumblebee-Old-and-abbandoned/commit/a047be85247755cdbe0acce6f1dafc8beb84f2ac

    An automated script runs something like rm -rf /usr/local/........., but due to accidentally inserting a space, the command became rm -rf /usr /local/......, so this effectively means "delete all root folders that start with usr or local", effectively destroying the system of anyone who uses it. I feel bad for that developer.

    You can avoid these kinds of bugs by quoting your strings, ie: rm -rf "/usr/ local/...." would have provided an error message and avoided this bug, because the quotes mean that everything between them is the full path, NOT a list of separate paths/files (ie: you are telling rm that the file/folder has a SPACE character in its name).