I'd like to run a command line:
cd ~/www/tmp/; rm -P 2*
But I get an error if there are no files starting with 2.
You would think I would want to use -f
, however:
-f Attempt to remove the files without prompting for confirmation, regardless of the file's permissions. If the file does not exist, do not display a diagnostic message or modify the exit status to reflect an error.
And I do care about file permissions- if there's something weird going on, I'd prefer the command abort. I only want to suppress 'do not exist' errors.
I've looked at some other questions with 'rm errors', and found these two which seem (kinda) relevant:
Delete files and directories by their names. No such file or directory
But, I don't understand everything going on in the answers to this question, and don't know that I need it?
The touch
hack suggested in this question:
Have rm not report when a file is missing?
Seems workable, as I don't think I mind the performance hit (but what type of performance hit would you take? – ie: is this a reasonably scalable technique, or am I just learning very bad habits?)
I'm not sure how I'd do an if-then
within the command line.
Are there any other options for a simple command line cut-n-paste cleanup?
Would find -delete
work better for my case? If so, why?
error in question:
rm: 2*: No such file or directory
Best Answer
To only call
rm -P
for existing regular files whose names match a pattern in a directory (but not below):E.g.,
To match only non-directories, use
! -type d
in place of-type f
. The difference is that! -type d
would be true for a wider range of file types (symbolic links, named pipes, sockets, etc.)Since you want to be prompted for some conditions on the files found, you should not use
-delete
instead of-exec rm
since that works asrm -f
.Remove
-maxdepth 1
to letfind
look in subdirectories recursively.