If you're thinking of something like ls foo*.txt
vs. rm foo*.txt
, then yes, they will show and remove the same files. The shell expands the glob, and passes it to the command in question, and the commands work on the listed files. One listing them, one removing them.
The obvious difference is that if any of those files happened to be a directory, then ls
would list its contents, but rm
would fail to remove it. That's usually not a problem, since rm
would remove less than what was shown by ls
.
The big issue here comes from running ls *
or rm *
in a directory containing filenames starting with a dash. They would expand to the command lines of the two programs as if you wrote them out yourself, and ls
would take -r
to mean "reverse sort order", while rm
would take -r
to mean a recursive removal. The difference matters if you have subdirectories at least two levels deep. (ls *
will show the contents of the first level directories, but rm -r *
will everything past the first sublevel, too.)
To avoid that, write permissive globs with a leading ./
to indicate the current directory, and/or put a --
to signal the end of option processing before the glob (i.e. rm ./*
or rm -- *
).
With a glob like *.txt
, that's actually not an issue since the dot is an invalid option character, and will cause an error (until someone expands the utilities to invent a meaning for it), but it's still safer to put the ./
there anyway.
Of course you could also get different results for the two commands if you changed the shell's globbing options, or created/moved/removed files in between the commands, but I doubt you meant any of those cases. (Dealing with new/moved files would be extremely messy to do safely.)
rm
does not have a--dry-run
flag...find -delete
be better thanrm
? You say "That is why", but it's completely unclear to me what that refers to. Also note that yourfind
invocation will delete all files recursively in the current directory, whererm
will just delete the files in the immediate directory. Also-name *
is a no-op. All in all, I'm quite puzzled by your advice...find
is because you can run it, see all the files, and then run the same command with-delete
. Since you already saw the results fromfind
, there should be no ambiguity to what will be removed (I'd actually like to hear more details about this in the form of an answer)-delete
" - But how is that better than runningls <filespec>
, followed byrm <filespec>
(which the OP already knows how to do)?find ... -print
first to confirm what files would be deleted, and thenfind ... -delete
, you'll still delete files created between the two commands. If you use both-print
and-delete
, you don't get confirmation, just an after-the-fact report of what has been deleted (and you might as well userm -v
).