Updated: 11 October 2026 · Applies to: GNU rm 9.7 and find 4.10.0 on Ubuntu 26.04 LTS and Debian 13, GNU rm 9.5 on AlmaLinux 10 and Rocky Linux 9, BusyBox 1.37 on Alpine Linux and BSD rm on macOS 27

The Linux command to delete a file is rm: rm file.txt removes it at once, and rm file1.txt file2.txt removes several. To delete files older than 30 days, use find /path -type f -mtime +30 -delete. There is no recycle bin on the command line, so list what a command matches before you delete. The same commands work in Bash scripts, on other Unix systems and on macOS. We ran every example below in lab containers (Ubuntu 26.04 unless noted).

Delete one or more files with rm

rm file.txt                  # one file
rm file1.txt file2.txt       # several files
rm -v *.log                  # every .log file in this folder, and show what was removed
rm -i *.txt                  # ask before each file
rm -I *                      # ask once when deleting more than three files
rm -f file.txt               # no questions, no error if the file does not exist

With -v, rm confirms each file:

$ rm -v file1.txt file2.txt
removed 'file1.txt'
removed 'file2.txt'
  • Without -f, a missing file is an error (rm: cannot remove 'missing.txt': No such file or directory, exit code 1). With -f the exit code is 0, which is handy in scripts.
  • rm does not delete folders: rm: cannot remove 'somedir': Is a directory. Use rmdir or rm -r, as explained in how to remove a directory in Linux.
  • On AlmaLinux and Rocky Linux, root's ~/.bashrc contains alias rm='rm -i', so rm asks rm: remove regular empty file 'a.txt'? for every file. Type y, or run \rm a.txt to skip the alias once. Debian has the same line commented out, and Ubuntu has none.

Delete all files of one type, or everything in a folder

rm /path/to/folder/*.log                            # .log files in that folder only
rm /path/to/folder/*                                # all files, but not hidden files or subfolders
find /path/to/folder -maxdepth 1 -type f -delete    # all files including hidden ones, keep subfolders
find /path/to/folder -type f -name "*.log" -delete  # .log files in all subfolders too

A wildcard skips hidden files: our rm -v *.log left .hidden.log in place. More patterns are in how to delete all files in a directory.

Delete files older than 30 days

  1. List the files first:
    find /var/log/myapp -type f -name "*.log" -mtime +30 -print
  2. When the list is right, replace -print with -delete:
    find /var/log/myapp -type f -name "*.log" -mtime +30 -delete
    Use -exec rm -v {} + instead of -delete if you want a line for each file removed.
  3. To clean up every night, add the command with crontab -e (this line runs at 03:30):
    30 3 * * * find /var/log/myapp -type f -name "*.log" -mtime +30 -delete
    See crontab syntax, or cron jobs in cPanel on shared hosting.

What +30 really means: find counts whole days and drops the remainder, so -mtime +30 matches files at least 31 days old. In our lab, a file changed 31 days and 1 hour ago matched, and one changed 30 days and 12 hours ago did not:

$ find /root/logs -type f -name "*.log" -mtime +30 -print
/root/logs/old-31d.log
/root/logs/old-40d.log

For an exact cut-off, count minutes: -mmin +43200 (30 x 24 x 60) also matched the 30.5-day-old file. Other useful filters:

find /path -type f -mtime -7         # changed in the last 7 days
find /path -type f -mmin +60         # older than 60 minutes
find /path -type f -size +100M       # larger than 100 MiB
find /path -type f -empty -delete    # delete empty files

Delete files in a Bash script

A script deletes without asking, so a typo or an empty variable can remove the wrong files:

  • Quote variables and end the options with --: rm -- "$file". Without quotes, a name with a space becomes two names: rm my file.txt tries to delete my and file.txt.
  • Stop when a folder variable is empty. ${dir:?} aborts instead of expanding to /*:
    $ bash -c 'dir=""; rm -rf "${dir:?}/"*'
    bash: line 1: dir: parameter null or not set
    set -u at the top of a script does the same for variables that were never set (unbound variable).
  • Handle "no match". If nothing matches *.log, Bash passes the pattern itself and rm fails with rm: cannot remove '*.log': No such file or directory. Use find ... -delete, or nullglob and a check:
    shopt -s nullglob
    files=(/var/tmp/myapp/*.log)
    if (( ${#files[@]} )); then rm -- "${files[@]}"; fi
  • When you pipe names from find, use find . -name "*.tmp" -print0 | xargs -0 rm so names with spaces survive, or simply -delete.

File names that start with a dash

$ rm -weird.txt
rm: invalid option -- 'w'
Try 'rm ./-weird.txt' to remove the file '-weird.txt'.
Try 'rm --help' for more information.

Do what the hint says, rm ./-weird.txt, or use rm -- -weird.txt. For spaces, put the name in quotes: rm "my file.txt".

Argument list too long

A wildcard that matches a huge number of files can exceed the size limit for one command. In our lab, a folder with 200,000 files made rm *.tmp fail with /usr/bin/rm: Argument list too long. find has no such limit, because it deletes the files itself:

find /path/to/folder -maxdepth 1 -type f -name "*.tmp" -delete

Permission denied and write-protected files

  • rm: cannot remove '/srv/rootdir/owned.txt': Permission denied: deleting needs write permission on the folder that holds the file, not on the file itself. Our file had mode 777, but the folder belonged to root. Use sudo only if you are sure the file should go.
  • rm: remove write-protected regular empty file 'wp.txt'?: the file is read-only but you may write to its folder, so rm asks first. Answer y, or use rm -f.

Deleted a big file but the disk is still full?

If a program still has the file open, the space comes back only when that program closes it. lsof +L1 lists such files:

$ sudo lsof -nP +L1
COMMAND PID USER FD   TYPE DEVICE SIZE/OFF NLINK    NODE NAME
sleep   348 root 1w   REG   0,63 52428800     0 1579917 /root/lab/app.log (deleted)

Restart that program or service. For a log that is still in use, empty it with truncate -s 0 app.log instead of deleting it. More in how to delete huge files in Linux.

Delete a file securely with shred

$ shred -v -u secret.txt
shred: secret.txt: pass 1/3 (random)...
shred: secret.txt: pass 2/3 (random)...
shred: secret.txt: pass 3/3 (random)...
shred: secret.txt: removing
shred: secret.txt: renamed to 0000000000
(nine more renames shortened)
shred: secret.txt: removed

-u removes the file after overwriting it (Ubuntu 26.04 ships the uutils shred 0.10.0 with the same options). According to the GNU coreutils manual, shred assumes data is overwritten in place, which is not true on journaled or log-structured file systems (it names Btrfs, XFS, ZFS and ext4 with data=journal), RAID, snapshots or SSDs, and backups may keep a copy. For sensitive data, encrypt the disk instead.

Unix and macOS

rm and find are standard Unix commands. On macOS 27 (BSD rm and find), rm -- -weird.txt and find . -name "*.log" -mtime +30 -delete worked the same, as did the find command on Alpine Linux (BusyBox 1.37). On macOS, messages read rm: nothing.txt: No such file or directory, rm -v prints only the name, long options such as --version are rejected, and there is no shred (the man page says rm -P has no effect).

Without the command line

In cPanel, open File Manager, select the files and click Delete. According to cPanel's documentation, File Manager moves them to the Trash folder; View Trash and Restore bring them back, and Empty Trash deletes them for good.

Deleted the wrong file on the command line? How to recover a deleted file in Linux explains what can still be done. For a safety net, use a trash command such as trash-cli. What rm -rf does is covered in what the rm -rf command does.

Official documentation: GNU rm, GNU shred and cPanel File Manager.

Ucartz services for this topic

Was this answer helpful? 0 Users Found This Useful (0 Votes)