Using find - Deleting all files/directories (in Linux ) except any one

后端 未结 11 1060
野趣味
野趣味 2021-02-02 15:12

If we want to delete all files and directories we use, rm -rf *.

But what if i want all files and directories be deleted at a shot, except one particular fi

11条回答
  •  醉话见心
    2021-02-02 15:59

    mv subdir/preciousfile  ./
    rm -rf subdir
    mkdir subdir
    mv preciousfile subdir/
    

    This looks tedious, but it is rather safe

    • avoids complex logic
    • never use rm -rf *, its results depend on your current directory (which could be / ;-)
    • never use a globbing *: its expansion is limited by ARGV_MAX.
    • allows you to check the error after each command, and maybe avoid the disaster caused by the next command.
    • avoids nasty problems caused by space or NL in the filenames.

提交回复
热议问题