rm not deleting files completely


I have written an alias to create makefile (basically copying a template makefile and substituting the final exec name) in any project dir:
This is my cpmk command:

alias  cpmk='f() {                   \
if [ "$#" -eq 0 ] ;                  \
then                                 \ 
d="$(pwd)";                          \
else                                 \
d="$1";                              \
fi;                                  \
echo Trying to make a makefile in: $(readlink -f "$d") ;                \
if  [  -f $(readlink -f "$d")/makefile ] ;                                  \
then                                 \
echo  $(readlink -f "$d")/makefile already exists, but might be a different one, dont know;            \
return 1;                            \
fi;                                  \
read -p "Enter exec name:" execname ;\
echo This is the name of the executable: "$execname" ;          \
touch $(readlink -f "$d")/makefile;\
sed 's/hellomake/"$execname"/' ~/.makefileTemplate >$(readlink -f "$d")/makefile;                   \
if [ "$?" -eq 0 ] ;               \
then                              \
echo  $(readlink -f "$d")/makefile created successfully;             \ 
unset -f f;                       \
return 0;                         \
else                              \
echo $(readlink -f "$d")/makefile creation failed, couldnt write to file, by the way, there is no other makefile "in" this dir by that name, something "else" erred;                     \
unset -f f;                       \
return 0;                         \
fi;                               \
};                                \

Then I source .bashrc (because this alias is in the bashrc).

I create a makefile using cpmk. Then I run cpmk again. This time it doesn’t create a makefile because there’s already one. These makefiles are a copy of a template makefile, I have kept hidden. This is basically a copy-like command with variable replacement. Back to the problem in the dir where there’s already a makefile made by cpmk ran previously, I then delete this existing makefile by doing rm makefile, and rerun cpmk. This time too it asks me what to name the exec, I give it the name. It displays the name of the exec, and prints "created successfully" like message, but when I open it, I find the same deleted makefile again. How do I know? Because, It has the execname of the last one, the exec name given this time isn’t found in the makefile.
The same old makefile appears again with old exec name?

how do I completely delete any file so that when next time touch is run with the same filename as the deleted one, the old deleted file doesn’t reappear again?

Sometimes, the deletion of the previous makefile is successful and it doesn’t reappear on running touch with the same filename as the deleted one but sed fails to substitute execname in the generated makefile

sed 's/hellomake/"$execname"/' ~/.makefileTemplate >$(readlink -f "$d")/makefile;                   \

This line above is not substituting $execname in the makefile. I am getting empty space in place of the substituted text hellomake. But $execname has the name of the exec file as can be seen by the messages printed by cpmk.


The problem lies in the use of quotes. The function f() is already surrounded by single quotes and inside sed’ s regex, single quotes are being used again to enclose the regex ‘s/hellomake/"$execname"/’. That’ why getting undefined behaviour. But it’s surprising that because of this undefined behaviour, getting a deleted file back. Corrected it, now runs. Just substituted single quotes ‘ with double quotes ".


Replace the above line with this:


Answered By – Fitohe

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published