Linux Delete Files Bash Command

To delete files on Linux server, there are multi ways to achieve this.

rm bash command

1
rm [options] ...file...

I usually use rm -rf file folder_name to remove a non-empty filefolder.

Options:

  • -f, —force

    Ignore nonexistent files, never prompt

  • -r, -R, —recursive

    Remove directories and their contents recursively.

and some other options, see the reference 1 for the doc.

rsync

rsync is an alternative method which can be used in deleting files. It is suitable for the folder which contains a large amount of files, in which case rm -rf can’t behave well.

1
2
mkdir empty
rsync -a --delete empty/ your_folder/

Reference

  1. Bash man doc: rm
  2. Bash man doc: rsync
  3. StackExchange: Efficiently delete large directory containing thousands of files