To remove files named core.xxxxxxx where xxxxxxx is exactly 4 or 5 digits, and all files are in the current folder, you should use the find command.
Important Safety Precaution:
Always test your find command with -print first to see which files it would delete, before actually running the delete command.
Step 1: Preview the files that will be deleted (Highly Recommended)
Navigate to the directory containing these files using cd.
bash Copy code
cd /path/to/your/folder
Then, run this command to list the files that match your criteria:
bash Copy code
find . -maxdepth 1 -regextype egrep -regex "^\./core\.[0-9]{4}$|^\./core\.[0-9]{5}$" -print
Let's break down this command:
find .: Start searching in the current directory (.).
-maxdepth 1: Only search in the current directory, do not go into subfolders. This is important since you specified "all file is in the same folder".
-regextype egrep: Tells find to use egrep (extended regular expression) syntax. This allows us to use {} for repeating characters and | for OR.
-regex "^\./core\.[0-9]{4}$|^\./core\.[0-9]{5}$": This is the core of the pattern matching:
^./: Matches the start of the path relative to the current directory (e.g., ./). find -regex matches the entire path, not just the filename.
core\.: Matches the literal string "core." (. needs to be escaped because it's a special regex character).
[0-9]{4}: Matches exactly four digits (0-9).
|: This is the OR operator.
[0-9]{5}: Matches exactly five digits (0-9).
$: Matches the end of the filename.
-print: Prints the names of the matched files.
Example output of the preview command:
code Copy code
./core.1234
./core.56789
./core.0000
(It will not show core.123 or core.123456 or core.abcde)
Step 2: Delete the files (After verifying Step 1)
If the list from Step 1 looks correct and contains only the files you want to delete, you can proceed with the deletion.
Option 1: Using find with -delete (Recommended)
This is generally the safest and most efficient way as find handles the deletion itself.
bash Copy code
find . -maxdepth 1 -regextype egrep -regex "^\./core\.[0-9]{4}$|^\./core\.[0-9]{5}$" -delete
-delete: Deletes the files found. This is a powerful action, so be sure of your regex!
Option 2: Using find with -exec rm {} \;
An alternative, if you prefer rm or need more control (though -delete is usually better for this specific case):
bash Copy code
find . -maxdepth 1 -regextype egrep -regex "^\./core\.[0-9]{4}$|^\./core\.[0-9]{5}$" -exec rm {} \;
-exec rm {} \;: Executes the rm command for each found file.
{}: A placeholder for the current file path found by find.
\;: Terminates the -exec command.
Summary of the recommended approach:
cd /path/to/your/folder
PREVIEW: find . -maxdepth 1 -regextype egrep -regex "^\./core\.[0-9]{4}$|^\./core\.[0-9]{5}$" -print
DELETE (if preview was correct): find . -maxdepth 1 -regextype egrep -regex "^\./core\.[0-9]{4}$|^\./core\.[0-9]{5}$" -delete