Does 'rm files*' remove all matches from all sub-directories?

I want to remove any file that looks like wordpress-891.sql from the current directory (not inside sub-directories).

Will rm wordpress-*.sql do the trick or will it also remove matches from sub-directories?


Solution 1:

No. Normal globbing * is not recursive and neither is rm.

If a directory name matches, it won't be removed - you need the -r flag to delete a directory.

So it's safe to do that if you're sure you want to delete those files.

You can also make rm interactive

rm -i wordpress-*.sql

then it will ask for confirmation before deleting each file

Solution 2:

Yes it does the trick for you and removes all files with that schema in the current directory. And NO, it does not removes files within the sub-directories.

When ever you are not sure what happens when you run a command like:

rm wordpress-*.sql

then just run it using ls:

ls wordpress-*.sql

the files you see in output are the ones which will get removed.

When you are trying to get a list like: foo*, it is better to use -d switch with ls to prevent listing files withing a directory named foobar/ etc.

ls -d foo*

This trick works for commands which are not used to do the job recursively.

The other thing you can do is to type your desired input, e.g: wordpress-* then press Ctrl+Alt+*, and now all the matches are typed automatically in front of your command.