views:

46

answers:

3

How do I remove certain files from a different directory than $PWD using the bash shell script.

Looking at the documentation for rm, it appears that rm only works in $PWD.

Am I forced to use this method:

oDir=$PWD
cd directorytoremovefiles
rm files
cd oDir
+2  A: 

As pointed out by YYC,

rm $DIR/files 
Elpezmuerto
+2  A: 

rm certainly does work for deleting files in another directory.

Whatever gave you that idea from the man page, I certainly hope it's not this:

rm removes each specified file. By default, it does not remove directories.

The documentation you refer to, talks only about having write & execute permission to the directory you are deleting from.

So you only need:

rm directorytoremovefiles/files
Kedar Soparkar
A: 

rm will take any path, relative or absolute. If there is no slash at the beginning of directorytoremovefiles then it is a relative path and you may need to store PWD for later. However, you can do this with pushd and popd or a cd - once you're finished. Or, if you run the cd and rm commands in parens they will run in a subshell, like this: ( cd directory; rm files) then your working shell will not change directory.

Matt Kane