Remove
I am currently trying to delete some files. The files I want to delete contain the number 3 or 2, either both or one of them.
How do I delete files containing those numbers?
The files are named like so:
K-0_0_1_1_1_1_1_0.wav
K-0_0_1_1_1_1_1_1.wav
K-0_1_0_0_0_0_0_0.wav
K-0_1_0_0_0_0_0_1.wav
K-0_1_0_0_0_0_1_0.wav
K-0_0_0_0_0_0_0_2.wav
K-0_0_0_0_0_0_0_3.wav
K-0_0_0_0_0_0_1_2.wav
In this list the last three files should be deleted.
For the current directory, you can use:
rm -- *[23]*
If you want to restrict it, to match only files where the 2 or 3 must occur between K and .wav:
rm K*[23]*.wav
If you want to make this safer by forcing rm to ask for confirmation for every file, use the -i interactive flag:
rm -i K*[23]*.wav
Notes
the shell glob * matches any number (including zero) of any characters
to the shell [some chars] is a character class - anything inside can match
-- is recognised by rm as the end of options. This avoids errors if any filenames begin with -; otherwise the filename may be interpreted as an option
Well, if rm didn't accept multiple filenames, neither way would work when the shell expands the glob to more than one filename. The main differences between rm -- *[23]* and rm -- *2* *3* are (a) that *2* *3* can give the same filename twice and (b) the order. With the OP's files, echo *[23]* shows K-0_0_0_0_0_0_0_2.wav K-0_0_0_0_0_0_0_3.wav K-0_0_0_0_0_0_1_2.wav while echo *2* *3* shows K-0_0_0_0_0_0_0_2.wav K-0_0_0_0_0_0_1_2.wav K-0_0_0_0_0_0_0_3.wav. With rm this affects deletion order and thus the order errors (or messages due to -v, -i, or -I) are shown
rm *2* *3*
That will delete all files (not directories) inside the current working directory which get matched by either of the *2* or *3* globs.
In those globs, "* always means "any number (including zero) of any character".
Note however that IIRC Bash's globs don't match hidden files (filename starting with a .) by default.
Run this to see what is to be deleted:
find . -type f -iname "*[32]*.wav"
Then delete with:
find . -type f -iname "*[32]*.wav" -delete
More information:
. means current directory, could also be path to location of file, like /path/to/files
To prevent find from moving into any directory in the intended target use the option -maxdepth 1, hence:
find . -maxdepth 1 -type f -iname "*[32]*.wav" -delete
If in the target directory then use:
find . -maxdepth 1 -type f -iname "*[32]*.wav" -delete
CAUTION
Never do this find . -delete -type f -iname "*[32]*.wav" else all (your files) will be deleted!
CAUTION
Never do this find . -delete -type f -iname "*[32]*.wav" else all (your files) will be deleted!
xargs, which allows a preview before running The Command:
$ echo *2* *3*
(list of files with 2 or 3 in name)
That's only the shell glob test (works in bash), if the list is correct, pipe it to xargs:
$ echo *2* *3* | xargs rm -v
The -v option lists files rm is removing. You could be more paranoid and add the -i option with -v so it prompts for every deletion.
How do I delete files containing those numbers?
The files are named like so:
K-0_0_1_1_1_1_1_0.wav
K-0_0_1_1_1_1_1_1.wav
K-0_1_0_0_0_0_0_0.wav
K-0_1_0_0_0_0_0_1.wav
K-0_1_0_0_0_0_1_0.wav
K-0_0_0_0_0_0_0_2.wav
K-0_0_0_0_0_0_0_3.wav
K-0_0_0_0_0_0_1_2.wav
In this list the last three files should be deleted.
For the current directory, you can use:
rm -- *[23]*
If you want to restrict it, to match only files where the 2 or 3 must occur between K and .wav:
rm K*[23]*.wav
If you want to make this safer by forcing rm to ask for confirmation for every file, use the -i interactive flag:
rm -i K*[23]*.wav
Notes
the shell glob * matches any number (including zero) of any characters
to the shell [some chars] is a character class - anything inside can match
-- is recognised by rm as the end of options. This avoids errors if any filenames begin with -; otherwise the filename may be interpreted as an option
Well, if rm didn't accept multiple filenames, neither way would work when the shell expands the glob to more than one filename. The main differences between rm -- *[23]* and rm -- *2* *3* are (a) that *2* *3* can give the same filename twice and (b) the order. With the OP's files, echo *[23]* shows K-0_0_0_0_0_0_0_2.wav K-0_0_0_0_0_0_0_3.wav K-0_0_0_0_0_0_1_2.wav while echo *2* *3* shows K-0_0_0_0_0_0_0_2.wav K-0_0_0_0_0_0_1_2.wav K-0_0_0_0_0_0_0_3.wav. With rm this affects deletion order and thus the order errors (or messages due to -v, -i, or -I) are shown
rm *2* *3*
That will delete all files (not directories) inside the current working directory which get matched by either of the *2* or *3* globs.
In those globs, "* always means "any number (including zero) of any character".
Note however that IIRC Bash's globs don't match hidden files (filename starting with a .) by default.
Run this to see what is to be deleted:
find . -type f -iname "*[32]*.wav"
Then delete with:
find . -type f -iname "*[32]*.wav" -delete
More information:
. means current directory, could also be path to location of file, like /path/to/files
To prevent find from moving into any directory in the intended target use the option -maxdepth 1, hence:
find . -maxdepth 1 -type f -iname "*[32]*.wav" -delete
If in the target directory then use:
find . -maxdepth 1 -type f -iname "*[32]*.wav" -delete
CAUTION
Never do this find . -delete -type f -iname "*[32]*.wav" else all (your files) will be deleted!
CAUTION
Never do this find . -delete -type f -iname "*[32]*.wav" else all (your files) will be deleted!
xargs, which allows a preview before running The Command:
$ echo *2* *3*
(list of files with 2 or 3 in name)
That's only the shell glob test (works in bash), if the list is correct, pipe it to xargs:
$ echo *2* *3* | xargs rm -v
The -v option lists files rm is removing. You could be more paranoid and add the -i option with -v so it prompts for every deletion.
Comments
Post a Comment