Terminus
Delete Files In Linux

Delete Files In Linux

The short answer

In Unix-like operating systems like Linux and macOS, to delete one or more files, you can use the [.inline-code] rm[.inline-code] command as follows:

$ rm <file ...>

Where:

  • [.inline-code] file[.inline-code] is a list of paths to the files you want to delete.

For example, this command will remove the [.inline-code] index.js[.inline-code] file located in the [.inline-code] app[.inline-code] directory:

$ rm ./app/index.js

It is important to note that the [.inline-code] rm[.inline-code]  command will not place the files you want to remove into the trash folder, but will immediately and irrevocably erase them from your system!

You should therefore use this command with extreme caution as there is no undelete command.

If you want to learn more about deleting entire directories, you can read our other article on how to delete directories in Linux.

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code] remove file[.inline-code]  in the AI Command Suggestions will prompt [.inline-code] rm[.inline-code] command that can then be quickly inserted in your shell by doing [.inline-code] CMD+ENTER[.inline-code] .

[#delete-files-with-force] Forcing the deletion of files [#delete-files-with-force]

To attempt to forcefully remove files without prompting for confirmation, like write-protected files for instance, you can use the [.inline-code] rm[.inline-code]  command with the [.inline-code] -f[.inline-code]  flag as follows:

$ rm -f <file ...>

[#delete-files-from-other-users] Deleting files belonging to another user [#delete-files-from-other-users]

To delete files that belong to other users, groups you’re not a member of, or you don’t have sufficient permissions over, you will need to combined the [.inline-code] rm[.inline-code]  command with the [.inline-code] sudo[.inline-code]  command to gain superuser privileges as follows:

$ sudo rm -f <file ...>

You can learn more about this command by reading our other article on how to safely use the rm command with sudo.

[#delete-files-with-patterns] Deleting files based on patterns [#delete-files-with-patterns]

To remove multiples files at once based on patterns, you can use shell features such as wildcards and brace expansions, where:

  • [.inline-code] *[.inline-code] will match one or more characters.
  • [.inline-code] ?[.inline-code] will match a single character.
  • [.inline-code]{}[.inline-code] will match the patterns enclosed in braces.

For example, this command will remove all the files with a [.inline-code] .pdf[.inline-code] file extension in the current working directory:

$ rm *.pdf

This command will remove all the files like [.inline-code] hallo.txt[.inline-code] , [.inline-code] hello.txt[.inline-code] , etc from the current working directory:

$ rm h?llo.txt

This command will remove all the files with either a [.inline-code] .js[.inline-code] or [.inline-code] .json[.inline-code] file extension in the [.inline-code] app[.inline-code] directory:

$ rm app/*.{js,json}

[#safely-delete-files] Safely removing files [#safely-delete-files]

As the [.inline-code] rm[.inline-code] command is quite dangerous and potentially harmful to the operating system, there are essentially two ways you can use it in a safer way.

[#delete-files-in-interactive-mode] Deleting files in interactive mode[#delete-files-in-interactive-mode]

One option for safely removing files, is to use the [.inline-code] rm[.inline-code]  command with the [.inline-code] -i[.inline-code] flag (short for interactive), which will prompt you for confirmation before attempting to remove any of them:

$ rm -i <file ...>

You can then either type [.inline-code] y[.inline-code] to confirm or [.inline-code] n[.inline-code]  to skip the file, followed by [.inline-code] ENTER[.inline-code]  to confirm your choice.

[#test-patterns-before-deletion] Testing a wildcard pattern with [.inline-code]ls[.inline-code] [#test-patterns-before-deletion]

Another option for safely removing files when using wildcards is to first test your pattern using the [.inline-code] ls[.inline-code]  command, which will display the list of matched entries:

$ ls <pattern>

Then replace [.inline-code] ls[.inline-code] with [.inline-code] rm[.inline-code]  to actually remove them:

$ rm <pattern>

[#delete-files-with-spaces-in-name] Deleting files with a space character in the name [#delete-files-with-spaces-in-name]

To delete files containing one or more space characters in their name, you can enclose their path in single ([.inline-code] '[.inline-code] ) or double quotes ([.inline-code] "[.inline-code] ) as follows:

$ rm "./Documents/my cv.pdf"

Alternatively, you can escape each space character with a backslash character ([.inline-code] \[.inline-code] ) as follows:

$ rm ./Documents/my\ cv.pdf

[#delete-files-by-date]Deleting files older than a specified time [#delete-files-by-date]

To delete files that are older than a specified time, you can execute the [.inline-code] rm[.inline-code] command through the [.inline-code] find[.inline-code] command as follows:

$ find <directory> -type f -mmin +<time> -exec rm {} \;

Where:

  • [.inline-code] directory[.inline-code] is the path to the directory you want to perform a file search in.
  • [.inline-code] -type f[.inline-code] is used to specify that the [.inline-code] find[.inline-code] command should only search for regular files.
  • [.inline-code] -mmin +<time>[.inline-code] is used to specify the time expressed in minutes.
  • [.inline-code] -exec rm {} \;[.inline-code] is used to execute the [.inline-code] rm[.inline-code]  command on each file found, where [.inline-code] {}[.inline-code]  is a placeholder that represents the filename, and [.inline-code] \;[.inline-code] marks the end of the command.

For example, this command will remove all the files created or modified 15 minutes ago or earlier in the [.inline-code] /home/app[.inline-code] directory:

$ find /home/app -type f -mmin +15 -exec rm {} \;

Note that to search for files in terms of days and not minutes, you can use the [.inline-code] -mtime[.inline-code] flag instead.

[#delete-files-by-size] Deleting files with a size greater than a specific value [#delete-files-by-size]

To delete files with a size greater than a specified value, you can execute the [.inline-code] rm[.inline-code] command through the [.inline-code] find[.inline-code]  command as follows:

$ find <directory> -type f -size <size> -exec rm {} \;

Where:

  • [.inline-code] -size <size>[.inline-code]  is used to specify the file size suffixed with a size indicator, such as [.inline-code] K[.inline-code]  for kilobytes, [.inline-code] M[.inline-code] for megabytes,[.inline-code] G[.inline-code] for gigabytes, and so on. By default, the size is assumed in bytes.

For example, this command will remove all the files with a size greater than 1 gigabytes in the [.inline-code] /tmp/data/logs[.inline-code] directory:

$ find /tmp/data/logs -type f -size +1G -exec rm {} \;

[#delete-files-by-contents] Deleting the contents of a file [#delete-files-by-contents]

To erase the contents of a file without actually deleting the file, you can use the output redirection operator ([.inline-code] >[.inline-code])as follows:

$ > <file>

Where:

  • [.inline-code] file[.inline-code] is the path to the file.

For example, this command will erase the contents of the file named [.inline-code] hello.txt[.inline-code],but preserve the file in the filesystem:

bash
$ cat hello.txt
Hello, World!
$ > hello.txt
$ ls
hello.txt
$ cat hello.txt
$