Safe rm command

I would like to do a safe file removal

Naturally, the command

rm -f /dir/*

creates problem.

I want to modify the command such that it does not delete files with extensions
.f, .c, .h, .par, .com

Backup your data first, with bash:

shopt -s extglob
shopt -s nullglob
rm -f /dir/*!(.f|.c|.h|.par|.com)

ksh:

rm -f ./*!(.f|.c|.h|.par|.com)

ksh93 has nullglobbing operator:

 ~(N)*!(.f|.c|.h|.par|.com)

zsh:

 rm -f *^(.f|.c|.h|.par|.com)(N)

If you want to remove the hidden files, you should add another option (for bash it's dotglob).

I need to put the command in a makefile

Currently I have

clean:
    rm -f $(OBJDIR)/*.o $(BINDIR)/nraypk

distclean:
    rm -f $(OBJDIR)/*.o $(BINDIR)/*

I'm not familiar with makefiles. It could be safer if you identify a pattern in the filenames to be removed.

On a side note: "rm" is not unsafe at all, just the use one makes of it. What you need is not a safer command but a safer attitude towards the modus operandi of commands.

bakunin

I agree but need something to stop removing source code if they exist.

---------- Post updated at 10:59 AM ---------- Previous update was at 09:51 AM ----------

I also have the following variable

RAYPK_LIBSRC = $(RAYPK_LIBDIR)/time.f \
    $(RAYPK_LIBDIR)/model.f \
    $(RAYPK_LIBDIR)/tomo.f \
    $(RAYPK_LIBDIR)/plt.f \
    $(RAYPK_LIBDIR)/blkdat.f

RAYPK_LIBSRC expands to

RAYPK_LIBOBJ = ./source/library/raypk/time.f ./source/library/raypk/model.f ./source/library/raypk/tomo.f ./source/library/raypk/plt.f ./source/library/raypk/blkdat.f

I want to replace ./source/library/raypk with $(OBJDIR) and .f to .o

Not sure what you mean: if you want to replace a certain expanded path with a variable then this is not possible: you either deal with the expanded values of variables or the variable (names themselves), but you can't have it mixed. (Actually you can have it mixed by introducing "escaping" and treating the variable name as literal string, but i do not want to delve into this - its obviously not what you want.)

If you mean by "$(OBJDIR)" you want to replace it with the content of this variable then just replace the one variable with the other: instead of $(RAYPK_LIBDIR)/some_file.f use $(OBJDIR)/some_file.f . To change ".f" to ".o" do likewise with the extensions.

But honestly, and without any intention to belittle you: given that you have troubles grasping the concept of having to change a value to change the outcome: are you sure programming is the right profession for you?

I hope this helps.

bakunin

RAYPK_LIBDIR = ./source/library/raypk

OBJDIR = ./BUILD_DIR/obj

RAYPK_LIBSRC = $(RAYPK_LIBDIR)/time.f \
    $(RAYPK_LIBDIR)/model.f \
    $(RAYPK_LIBDIR)/tomo.f \
    $(RAYPK_LIBDIR)/plt.f \
    $(RAYPK_LIBDIR)/blkdat.f

and want to get

RAYPK_LIBOBJ =  ./BUILD_DIR/obj/time.f \
    ./BUILD_DIR/obj/model.f \
    ./BUILD_DIR/obj/tomo.f \
    ./BUILD_DIR/obj/plt.f \
    ./BUILD_DIR/obj/blkdat.f

I was thinking of doing something along the lines of

SRCS = $(patsubst %.f, %.o, $(wildcard *.f))

Naturally I will be replacing both the path and .f to .o

---------- Post updated at 11:36 AM ---------- Previous update was at 11:31 AM ----------

Basically I would like to apply the changes directly to the variable
RAYPK_LIBSRC and output the resulting list to another variable.

Naturally doing things as you mention is easy. I know a bit of
programming and you might know a little bit more.

---------- Post updated at 11:39 AM ---------- Previous update was at 11:36 AM ----------

Programming is not my profession! However I got to program so I can
investigate what I am interested in.

You do this with a special class of make-variables. Have a look in your reference manual for "$@", "$<", etc..

I hope this helps.

bakunin

The problem is that not all the files in the directory should be used.

I came up with this one

RAYPK_LIBOBJ:= $(subst .f,.o,$(subst $(RAYPK_LIBDIR),$(OBJDIR),$(RAYPK_LIBSRC)))