If I set the current/working directory (navigating to it using cd) to some particular directory and then type:

rm *.xvg

What will this command do? Is it true that the above command will only delete files with the extension .xvg only in the working directory?

I was nervous about trying this before asking, because I want to be absolutely sure that the above command will only delete .xvg files LOCATED IN THE WORKING DIRECTORY.


4 Answers 4


Yes, rm *.xvg will only delete the files with the specified extension in your current directory.

A good way to make sure you are indeed in the directory you want delete your files is to use the pwd command which will display your current directory and then do an ls to verify you find the files you are expecting.

If you are bit apprehensive about issuing the rm command, there are 2 things you can do:

  1. type ls *.xvg to see a list of what files would be affected by this command.

  2. Unless you have a lot of files, you could always also use the -i command line switch for rm (also exists for cp and mv). Using rm -i *.xvg would prompt you for each individual file if it was ok to delete it, so you could be sure nothing you didn't expect was getting deleted. (This will be tedious if you have a lot of files though :)

  • 4
    Also: rm -f {*.xvg,.*.xvg} to remove also hidden files. And rm -R -f {*.xvg,.*.xvg} to remove also directories.
    – Dor
    Commented Jan 24, 2014 at 10:03
  • echo *.xvg would be a better way of seeing what the glob expands to than ls *.xvg. If there is a directory ending in .xvg, ls will list its contents.
    – nog642
    Commented Apr 18 at 14:42

You don't need to navigate to the dir, just use

rm /some/dir/*.xvg 

In the case you have a typo or similar mistake in the path, where /som/dir does not exist:

cd /som/dir
rm *.xvg

will accidentally delete all .xvg-files in the current dir. The first command will not, and you don't need to cd back again.

An alternative way would be to use find:

find /some/dir/ -maxdepth 1 -type f -name "*.xvg" -delete 
  • find /some/dir/ -maxdepth 1 -type f -name "*.xvg" -delete worked for me! use -maxdepth "n" to delete matching files in sub directory if given path
    – Vinay
    Commented Jun 3, 2015 at 8:56

Yes, rm *.xvg will only delete files ending with .xvg in your current directory. Here's why.

When you type a command like this, work is split up between the shell you are using (let's assume bash) and the command binary.

You can locate the binary by typing which rm. This little program takes care of unlinking files. Programs like this can be started from the command line and can read a list of arguments prog arg1 arg2 arg3 when they start up. In the case of rm, they are interpreted as a list of fully qualified filenames to be deleted. So if you are in a directory containing the file foo.bar, typing delete 'foo.*' will result in rm: foo.*: No such file or directory. Note the single quotes around the file pattern, they tell the shell to pass the argument to the shell as it is.

However if you type rm *.bar in the same directory, it will delete the file. What's happening here is that your shell, which is the program you are using to type in commands, is performing some transformations before passing the arguments on to the command. One of these is called 'file name expansion', otherwise know as 'globbing'. You can see a list of bash file name expansions here. One of the most common expansions is *, which is expanded to filenames in the current directory.

A simple way to look at globs at work is to use echo, which prints back all arguments passed to it through the shell. So typing echo * in the same directory will output foo.bar. So when you type rm *.bar, what's actually happening is that the shell expands the argument list to foo.bar, then passes that to the rm command.

There are some ways of controlling globbing. In recent versions of bash, for example, you can turn on an option called globstar which will do recursive expansion. Typing echo **/*.bar will show a list of all files ending in .bar in all subfolders. So typing rm **/*.bar in globstar enabled bash will indeed recursively delete all matching files in subfolders.


Command :

  rm ./*.pgm

works for me


rm *.pgm

gives error command ( in Polish, translated ):

rm: wrong option - '1'
Use "rm ./-1000.pgm" to delete the file '-1000.pgm'.
Write "rm --help" for information.


bash --version
GNU bash, wersja 4.4.19(1)-release (x86_64-pc-linux-gnu)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .