-f option and -rf in rm

Hi,

I'm new to shell scripting and I'm trying to read some code I need to use. Could you help me out on what it's doing? I read this as.... if the directory has files then rm all the files in that directory. I'm not sure....

if [ -f ${TSP_FILEPATH_PLUGIN_DIR} ]; then
  run "rm -rf ${TSP_FILEPATH_PLUGIN_DIR}";
fi

run "mkdir -p ${TSP_FILEPATH_PLUGIN_DIR}";

-f forces the delete without a Y/N question
-r recurses (goes down into) sub-directories.

rm -rf  directory_name

remove all of the files including subdirectories in directory_name, then deletes directory_name as well.

What the heck is run? It is not a keyword in any shell I know.

1 Like

I read ( but its the end of the day... and had very strange annoying support...) :
If there is a file callled {TSP_FILEPATH_PLUGIN_DIR} , then remove it with all if exist subdirectories and content (???) but could make sense if JAVA code ... (links to directory...even so, I doubt: why use -f rather than -e?) badly written to me...
run (?) mkdir -p {TSP_FILEPATH_PLUGIN_DIR} (create with correct path to subdirs where necessary...)

1 Like

run is a subroutine. Sorry about that.

run()
{
     echo "running: $*";
     eval $*;
     EXIT_CODE="$?";
}

Thanks for the help!

Also, to confirm. The -f in the if statement is similar to exists -e?

-f will return true if the specified file exist AND is an ordinary file ( ...)

The others conditional expression in ksh/posix are (I may be missing some...)
-a
-b
-c
-d
-e

If ${TSP_FILEPATH_PLUGIN_DIR} is a directory, you would need -d not -f . A directory will never match -f .

if [ -d ${TSP_FILEPATH_PLUGIN_DIR} ]; then

This will not tell you if there are files in the directory, it will just tell you if the directory exists.

Footnotes:
1) I have go through life without ever needing rm -rf in a script. As a minimum, when testing your script consider echoing the command rather than executing it.
2) In unix scripting it is never necessary to end a line with a single semi-colon character.

Hypothetically what would the -f option perform on a directory. When I replace -f with -e some internal files within the directory are deleted (or at least they aren't shown), but when I keep the -f option those files remain in the directory. I need to keep the files (the are part of the framework of the server I'm using) so I will have to use the -f option.

Nothing.

I suggest you try it and see what it does.

Re post #7. Are you talking about if or rm ?
Please give an example.
The rm -rf directory is pretty ruthless. It deletes the directory named and all the contents.