• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Delete Files In Linux

Sarah Majeed

Published: 5/7/2024

About Terminus

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:

$ 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:

$ 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:

$ 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:

$ 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:

$ rm *.pdf

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

$ rm h?llo.txt

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

$ 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:

$ 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:

$ ls <pattern>

Then replace ls with rm  to actually remove them:

$ 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:

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

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

$ 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:

$ 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:

$ 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:

$ 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:

$ 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:

$ > <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
$ cat hello.txt
Hello, World!
$ > hello.txt
$ ls
hello.txt
$ cat hello.txt
$

Written by

Sarah Majeed

Filed Under

Related Articles

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.

UnixLinux
Razvan Ludosanu

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.

LinuxUnix
Razvan Ludosanu

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.

LinuxUnix
Razvan Ludosanu

Linux Chmod Command

Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.

Linux
Razvan Ludosanu

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Neeran Gul

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.

Linux

Create Groups In Linux

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

Linux

Switch Users In Linux

Learn how to switch between users, log in as another user, and execute commands as another user in Linux.

Linux

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.

Linux

Find Files In Linux

Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.

Linux

Copy Files In Linux

Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.

Linux

Create Files In Linux

Learn how to create regular files in Linux using command line interface commands like touch, echo, cat, printf, and in-terminal text editors like Vim.

Linux

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac