I’m wanting to non-recursively delete all files in a directory where each filename contains only numbers, using only a single line of BASH.
I somehow accidentally ran a shell script with commented out file suffix variables, so I inundated my directory with over 1000 filenames that contain only numbers and no file extensions, with somewhere between 1 to 4 digits, like so:
1001 1010 1025 1039 1053 1064 1089 1107 1204 1300 1340 1420 1590 1701 1730 1806 302 406 480 604 704 820 19 1 73 8
Thankfully, my directory contains no valuable files that start with numbers, so I am okay to delete all files that start with numbers. I know that I could find all of these files using multiple BASH commands like are shown below (which I’ve proven does work), but I want a more elegant way to do so:
find . -name '[0-9]'
find . -name '[0-9][0-9]'
find . -name '[0-9][0-9][0-9]'
find . -name '[0-9][0-9][0-9][0-9]'
- Sample output of last command by itself:
./1019
./1340
./1710
./1046
The find
command is useful to me because I only have to append -delete
after the command to delete the found files.
I’m not strictly opposed to somehow including shopt -s extglob
(to enable extended globbing and maybe allow interpretation of a few more [idk?] characters by the shell) into the single line, but I don’t know how that would be done, what side effects it may have on further commands I run, or how to stop that property being applied to future commands and causing unexpected issues.
If what I’m looking for is impossible, then I’m open to hearing possibilities or references using sed
, awk
, or a single line for
/while
loop.
Here are the single-liners I’ve tried so far (none of which work correctly):
find . -name '[0-9]+'
*This didn’t throw an error, but it found nothing (it should’ve found lots of files in the current directory, as proven by ls
).
find . -name '[0-9]([0-9])?([0-9])?([0-9])?'
*This didn’t throw an error, but it found nothing in the current directory.
*I added the parentheses to make it more visually clear that the 2nd, 3rd, and/or 4th digits can be absent.
*SiegeX’s answer recommended to use -regex (with a special qualifier) instead of -name since -name uses the weaker glob formatting instead of regex.
find . -regex -regextype posix-extended '[0-9]([0-9])?([0-9])?([0-9])?'
*I got the error find: paths must precede expression: `posix-extended'
. Idk what the weird backtick followed by an apostrophe in the error is supposed to mean…
*John Kugelman’s answer says the parentheses are supposed to be “escaped” (have a backslash before each parenthesis), so I’ll just get rid of them since they were only there for visual-understanding reasons.
find . -regex -regextype posix-extended '[0-9][0-9]?[0-9]?[0-9]?'
*Same error as above
*According to John Kugelman’s same answer, this is missing a full file path
find . -regex -regextype posix-extended '$pwd/[0-9][0-9]?[0-9]?[0-9]?'
*Same error as above
find . -regex -regextype posix-extended '/home/currDir/[0-9][0-9]?[0-9]?[0-9]?'
*Same error as above
find /home/currDir/ -regex -regextype posix-extended '/home/currDir/[0-9][0-9]?[0-9]?[0-9]?'
*Same error as above
find . -regex -regextype posix-extended '[0-9]+'
*Same error as above
find . -regex -regextype '[0-9]+' posix-extended
*Different error! Error: find: paths must precede expression: `[0-9]+'
find . -regex '[0-9]+' -regextype posix-extended
*No error, but didn’t output any files it found either.
find . '[0-9]+' -regex -regextype posix-extended
*Error: find: paths must precede expression: `posix-extended'
find . -regex '[0-9]+'
*No error, but didn’t output any files it found either.
find . '{0,999}'
*This recursively outputs ALL files in the directory and subdirectories. I don’t want to delete any files in my subdirectories because those are the input files for my program, so this doesn’t work. Also, it includes files that don’t contain any numbers and numbers that are out of the specified range.
*Example output:
./1780
./407
./1800
./testfiles
./testfiles/simple.html
./testfiles/medium.html
./testfiles/hard.html
./1470
./1039
find: ‘{0,999}’: No such file or directory
find . '{0,9}{0,9}{0,9}$'
*Same output as above, except the last line is instead find: ‘{0,9}{0,9}{0,9}$’: No such file or directory
.
find . -name '*[0-9]'
*Doesn’t work because non-numeric characters are included. Example output:
./1107
./870
./1059
./testfiles3
./1023
./40