Introduction
On macOS and Linux, files can be removed from the command line using tools such as rm, find, and grep.
Table of contents
- Introduction
- Remove a single file
- Remove multiple files
- Remove files by extension
- Remove files whose filename contains specific text
- Case-insensitive filename search
- Remove files whose content contains specific text
- Portable alternative for files containing text
- Important difference between filename search and content search
- Remove files from a specific folder
- Remove files with spaces in their names
- Remove a file whose name starts with a dash
- Remove empty directories
- Safer workflow before deleting files
- Practical examples
- Example 1: Remove one file
- Example 2: Remove multiple files
- Example 3: Remove all CSV files in the current folder
- Example 4: Remove files whose filename contains a date
- Example 5: Remove files whose filename contains a satellite name and date
- Example 6: Remove files whose content contains specific text
- Example 7: Preview files before deleting them
- Example 8: Remove matching files interactively
- Conclusion
- References
The most common cases are:
- remove one file
- remove several files
- remove files with filenames that contain a specific substring
- remove files whose content contains a specific text pattern
This tutorial shows practical examples that work on both macOS and Linux.
Before deleting files, it is strongly recommended to run a dry check first, especially when using wildcards such as *.
Remove a single file
To remove one file, use:
1 | rm filename.txt |
Example:
1 | rm data.csv |
If the filename contains spaces, wrap it in quotes:
1 | rm "my file.txt" |
You can also use the interactive option -i to ask for confirmation before deleting:
1 | rm -i data.csv |
Example output:
1 | remove data.csv? |
Type y to confirm.
Remove multiple files
To remove several files at once, list them after rm:
1 | rm file1.txt file2.txt file3.txt |
Example:
1 | rm data_01.csv data_02.csv data_03.csv |
With confirmation:
1 | rm -i data_01.csv data_02.csv data_03.csv |
Remove files by extension
To remove all files with a given extension in the current folder, use a wildcard.
For example, to remove all CSV files:
1 | rm *.csv |
Before deleting, it is safer to list the files first:
1 | ls *.csv |
Then remove them:
1 | rm *.csv |
With confirmation:
1 | rm -i *.csv |
Another example, remove all .txt files:
1 | rm *.txt |
Remove files whose filename contains specific text
If you want to remove files because their filename contains a specific substring, use find.
For example, suppose you have files like:
1 2 3 | viirs_efire_fire_pixels_NOAA21_20250321.csv.gz viirs_efire_skipped_fp_variables_NOAA21_20250321.csv viirs_efire_tile_statistics_NOAA21_20250321.csv |
and you want to remove all files whose filename contains:
1 | NOAA21_20250321
|
First, do a dry run:
1 | find . -type f -name "*NOAA21_20250321*" |
Then remove the matching files:
1 | find . -type f -name "*NOAA21_20250321*" -exec rm {} + |
A safer interactive version is:
1 | find . -type f -name "*NOAA21_20250321*" -exec rm -i {} + |
Here:
.means search from the current directory-type fmeans search only files-name "*NOAA21_20250321*"means the filename must contain that substring-exec rm {} +removes the matching files
Case-insensitive filename search
To remove files whose filename contains a substring, ignoring uppercase/lowercase differences, use -iname:
1 | find . -type f -iname "*noaa21_20250321*" |
Then remove them:
1 | find . -type f -iname "*noaa21_20250321*" -exec rm {} + |
Interactive version:
1 | find . -type f -iname "*noaa21_20250321*" -exec rm -i {} + |
Remove files whose content contains specific text
If you want to remove files because the inside content contains specific text, use grep first to find the files.
For example, to find files that contain:
1 | NOAA21_20250321
|
run:
1 | grep -rl "NOAA21_20250321" . |
Here:
grepsearches text-rsearches recursively in subfolders-lprints only the filenames that contain the matching text.means search from the current directory
After checking the list carefully, remove those files with:
1 | grep -rl "NOAA21_20250321" . | xargs rm |
However, this simple version can have problems with filenames containing spaces.
A safer version is:
1 | grep -rlZ "NOAA21_20250321" . | xargs -0 rm |
Interactive version:
1 | grep -rlZ "NOAA21_20250321" . | xargs -0 rm -i |
Portable alternative for files containing text
Another portable approach is to combine find, grep, and rm.
First, preview the files:
1 | find . -type f -exec grep -l "NOAA21_20250321" {} + |
Then remove them:
1 | find . -type f -exec grep -l "NOAA21_20250321" {} + | xargs rm |
For filenames with spaces, use:
1 2 3 4 | find . -type f -exec grep -l "NOAA21_20250321" {} + | while IFS= read -r file do rm "$file" done |
Interactive version:
1 2 3 4 | find . -type f -exec grep -l "NOAA21_20250321" {} + | while IFS= read -r file do rm -i "$file" done |
Important difference between filename search and content search
A common mistake is to use grep when searching for filenames.
For example:
1 | grep -rl "NOAA21_20250321" . |
searches inside the files.
It does not search filenames.
To search filenames, use:
1 | find . -type f -name "*NOAA21_20250321*" |
Summary:
1 2 3 | Search in filenames -> use find Search inside files -> use grep Remove files -> use rm |
Remove files from a specific folder
To search inside a specific folder instead of the current directory, replace . with the folder path.
Example on macOS external drive:
1 | find /Volumes/HD15TB/Datasets -type f -name "*NOAA21_20250321*" |
Then remove matching files:
1 | find /Volumes/HD15TB/Datasets -type f -name "*NOAA21_20250321*" -exec rm {} + |
Example on Linux:
1 | find /home/user/Datasets -type f -name "*NOAA21_20250321*" |
Then remove matching files:
1 | find /home/user/Datasets -type f -name "*NOAA21_20250321*" -exec rm {} + |
Remove files with spaces in their names
If a file has spaces in its name, use quotes:
1 | rm "my file.txt" |
For find, the following syntax safely handles filenames with spaces:
1 | find . -type f -name "*my text*" -exec rm {} + |
Interactive version:
1 | find . -type f -name "*my text*" -exec rm -i {} + |
Remove a file whose name starts with a dash
If a filename starts with -, rm may interpret it as an option.
For example, a file named:
1 | -test.txt |
can be removed using:
1 | rm -- -test.txt |
or:
1 | rm ./-test.txt |
Remove empty directories
To remove an empty directory, use:
1 | rmdir folder_name |
or:
1 | rm -d folder_name |
To remove a directory and everything inside it, use:
1 | rm -r folder_name |
Be careful with recursive removal. Always check the folder path before running rm -r.
Interactive version:
1 | rm -ri folder_name |
Safer workflow before deleting files
A good workflow is:
Step 1: Preview matching files
1 | find . -type f -name "*NOAA21_20250321*" |
Step 2: Remove interactively
1 | find . -type f -name "*NOAA21_20250321*" -exec rm -i {} + |
Step 3: Remove directly only when confident
1 | find . -type f -name "*NOAA21_20250321*" -exec rm {} + |
Practical examples
Example 1: Remove one file
1 | rm output.csv |
Example 2: Remove multiple files
1 | rm output_01.csv output_02.csv output_03.csv |
Example 3: Remove all CSV files in the current folder
1 | rm *.csv |
Example 4: Remove files whose filename contains a date
1 | find . -type f -name "*20250321*" -exec rm {} + |
Example 5: Remove files whose filename contains a satellite name and date
1 | find . -type f -name "*NOAA21_20250321*" -exec rm {} + |
Example 6: Remove files whose content contains specific text
1 | grep -rlZ "NOAA21_20250321" . | xargs -0 rm |
Example 7: Preview files before deleting them
1 | find . -type f -name "*NOAA21_20250321*" |
Example 8: Remove matching files interactively
1 | find . -type f -name "*NOAA21_20250321*" -exec rm -i {} + |
Conclusion
On macOS and Linux, the most useful commands for removing files are rm, find, and grep.
Use rm when you already know the filename:
1 | rm file.txt |
Use find when the filename contains a specific substring:
1 | find . -type f -name "*substring*" -exec rm {} + |
Use grep when the file content contains specific text:
1 | grep -rlZ "text" . | xargs -0 rm |
The safest habit is to always preview the matching files first, then delete them.
References
| Links | Site |
|---|---|
| GNU Coreutils Manual: rm invocation | GNU |
| GNU Findutils Manual | GNU |
| GNU Grep Manual | GNU |
| POSIX rm Utility | The Open Group |
