Delete Files In Linux

Sarah Majeed
Published: May 7, 2024

The short answer

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

Bash
$ rm <file ...>

Where:

  • file is a list of paths to the files you want to delete.

For example, this command will remove the index.js file located in the app directory:

Bash
$ rm ./app/index.js

It is important to note that the rm  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 retrieve this command using Warp’s AI Command Suggestions

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

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

Forcing the deletion of files

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

Bash
$ rm -f <file ...>

Deleting files belonging to another user

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 rm  command with the sudo  command to gain superuser privileges as follows:

Bash
$ 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.

Deleting files based on patterns

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

  • \ will match one or more characters.
  • ? will match a single character.
  • {} will match the patterns enclosed in braces.

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

Bash
$ rm *.pdf

This command will remove all the files like hallo.txt , hello.txt , etc from the current working directory:

Bash
$ rm h?llo.txt

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

Bash
$ rm app/*.{js,json}

Safely removing files

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

Deleting files in interactive mode

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

Bash
$ rm -i <file ...>

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

Testing a wildcard pattern with ls

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

Bash
$ ls <pattern>

Then replace ls with rm  to actually remove them:

Bash
$ rm <pattern>

Deleting files with a space character in the name

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

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

Alternatively, you can escape each space character with a backslash character ( \ ) as follows:

Bash
$ rm ./Documents/my\ cv.pdf

Deleting files older than a specified time

To delete files that are older than a specified time, you can execute the rm command through the find command as follows:

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

Where:

  • directory is the path to the directory you want to perform a file search in.
  • -type f is used to specify that the find command should only search for regular files.
  • -mmin +<time> is used to specify the time expressed in minutes.
  • -exec rm {} \; is used to execute the rm  command on each file found, where {}  is a placeholder that represents the filename, and \; 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 /home/app directory:

Bash
$ 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 -mtime flag instead.

Deleting files with a size greater than a specific value

To delete files with a size greater than a specified value, you can execute the rm command through the find  command as follows:

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

Where:

  • -size <size>  is used to specify the file size suffixed with a size indicator, such as K  for kilobytes, M for megabytes, G 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 /tmp/data/logs directory:

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

Deleting the contents of a file

To erase the contents of a file without actually deleting the file, you can use the output redirection operator ( >)as follows:

Bash
$ > <file>

Where:

  • file is the path to the file.

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

Bash
bash
$ cat hello.txt
Hello, World!
$ > hello.txt
$ ls
hello.txt
$ cat hello.txt
$
Written by
Sarah Majeed
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.