Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

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

Writer Matthew Barrera

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?

2

4 Answers

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.

6

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).

1

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.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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