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?

  • 1
    As an aside, while all the other answers end up correct, there is one more twist: it is actually bash, not the rm command, that expands the wildcard. If you have ten matching files in your directory, rm will not see the wildcard, but rather ten individual arguments. If you want rm to do the expansion, you have to enclose your argument in single quotes. For rm, it doesn't make a difference, but in some other contexts it may. Usually, when the wildcard refers to something other than files, such as apt-get packages. – Kevin Keane May 29 '17 at 19:24
  • 3
    @KevinKeane, rm doesn't do filename expansion... rm "foo*" would try to remove a file with a literal asterisk in the name. – ilkkachu May 30 '17 at 8:36

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


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.

  • 4
    My favourite method: printf "%s\n" wordpress-*.sql – wjandrea May 28 '17 at 20:41
  • 1
    You can also pretend Echo like echo rm file*. Especially look for hostile filenames like -rf – eckes May 29 '17 at 6:41
  • @eckes: You can cover those more safely with either rm ./* or rm -- *. – David Foerster May 29 '17 at 17:59
  • Yes, or use xargs -0 @DavidFoerster – eckes May 29 '17 at 20:33
  • if you go ls foo* and you have a directory called foobar, you also get a listing of the contents of that directory, so plain ls isn't really the best way to what the rm will hit – ilkkachu May 30 '17 at 8:32

No, rm does not recurse through subdirectories.

See Delete matching files in all subdirectories - SuperUser for methods for deleting files in subdirectories.

If you're ever concerned about accidentally deleting something important, use gvfs-trash (which sends files to the trash) instead of rm (which permanently deletes files).

  • Thanks for the gvfs-trash mention learnt something today hardly use it... – George Udosen May 28 '17 at 19:36

If you would like to find and delete all matching files such as wordpress-*.sql, you can use find command. :)

For instance, you would like to remove all matching files with wordpress-*.sql under test_dir, do like the following.

cd test_dir
find . -name "wordpress-*.sql" -exec rm -f {} \;

Whenever find program really finds a matching file, it tries to execute a command following after -exec option. In this case rm -f {} will be executed and {} will be changed to the match file name. E.g.

rm -f wordpress-169.sql

You can also set the depth of sub-directories with -maxdepth option.

cd test_dir
find . -maxdepth 3 -name "wordpress-*.sql" -exec rm -f {} \;

Please note that you must specify -maxdepth option before other options. Otherwise you may meet the results what you really don't want to see.

  • 1
    OP wants the opposite: not to recurse into subdirectories. – Melebius May 30 '17 at 6:10
  • 2
    GNU find: "the options affect all tests, including tests specified before the option", the list of options below includes -maxdepth. Use -exec rm -f {} + rather than -exec \;, or better yet, just -delete – ilkkachu May 30 '17 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.