开发者

Remove file from different directory

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. 开发者_Go百科

Am I forced to use this method:

oDir=$PWD
cd directorytoremovefiles
rm files
cd oDir


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


As pointed out by YYC,

rm $DIR/files 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜