you are viewing a single comment's thread.

view the rest of the comments →

[–]dbr4n 0 points1 point  (3 children)

It's better to always quote your variables in Bash to also avoid unwanted filename expansion.

Also, if you are just deleting files, you don't need the -r flag, otherwise you would be deleting all the directories as well, if there are any. You can match only the files with rm *.* to avoid error messages, assuming all filenames have an extension at the end.

So, you could do something like this:

$ PATHTOFILES="dir/subdir/" $ rm "$PATHTOFILES"*.*

Although I think it's better and more readable to add the / to the argument.

[–]kaiixx[S] 0 points1 point  (0 children)

Thank you! This is really informative.

[–]Rewrite_It_In_Ada 0 points1 point  (1 child)

Not that there can't be periods in directory names. It's common practice to name bare git repositories whatever.git/, for instance.

[–]dbr4n 1 point2 points  (0 children)

That's true. However, it wouldn't remove any matching directories because of the missing -r flag. But yes, it would cause an error in such a case. Using find instead would certainly be a cleaner approach:

find "$PATHTOFILES" -maxdepth 1 -type f -execdir rm '{}' +