Удаление файлов и каталогов

Рубрики Linux
  • To remove the folder with all its contents(including all interior folders):
    rm -rf /path/to/directory
    
  • To remove all the contents of the folder(including all interior folders) but not the folder itself:
    rm -rf /path/to/directory/*
    

    or

    rm -rf /path/to/directory/{*,.*}
    

    if you want to make sure that hidden files/directories are also removed.

  • To remove all the «files» from inside a folder(not removing interior folders):
    rm -f /path/to/directory/{*,.*}
    
  • Warning: if you have spaces in your path, make sure to always use quotes.
    rm -rf /path/to the/directory/*
    

    is equivalent to 2 separate rm -rf calls:

    rm -rf /path/to
    rm -rf the/directory/*
    

    To avoid this issue, you can use 'single-quotes'(does not expand shell variables) or "double-quotes"(expands shell variables):

    rm -rf "/path/to the/directory/"*
    

Where:

  • rm — stands for «remove«
  • -f — stands for «force» which is helpful when you don’t want to be asked/prompted if you want to remove an archive, for example.
  • -r — stands for «recursive» which means that you want to go recursively down every folder and remove everything.