user2805565
user2805565

Reputation: 51

Bash Wildcard use

I am trying to remove files in a directory using rm and without deleting the directory itself in a script. The examples I see only do this while in the directory itself, and I would like to do it without navigating there.

I tried

rm "$(dirname $1)/filetokeep/*"

but it is not working. Any help?

Upvotes: 5

Views: 207

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

Quoting the wildcard inhibits expansion.

rm -- "$(dirname -- "$1")/filetokeep"/*

Using -- ensures that values can't be interpreted as optional arguments rather than positional ones (so that things still work if the directory named in $1 starts with a -).

Upvotes: 6

Related Questions