I need to make a bash
script that takes two arguments, one is the string to search for in the filenames and the next one is the file path to search in. Next it needs to go through one matching file at a time and print it out, then prompt the user if they want to delete it or not.
An example of it would be:
./scriptName.sh foo /path/to/directory/
/path/to/directory/foo.txt
Delete this? (y/n)
user input
/path/to/directory/foop.txt
Delete this? (y/n)
user input
etc...
I originally tried
find $2 -type f -name "*$1*" -print
and
find $2 -type f -name "*$1*" -delete
Where $1
is the first argument and $2
are the second argument of the script.
This worked until I realized that it had to list each found file separately and prompt to delete them which is a bit of problem since the previous two lines of code just deletes all the matching files at once.
rm -i
command i.e., interactiverm
is there for this exact purpose. You do not need to do anything, unless you want a totally different verbiage. If this is the case, put your message beforerm
command and test if the user said yes or not using conditionals. Just create a dummy file and runrm -i dummyfile
command to see how it works – MelBurslan Apr 10 '16 at 04:57for file in $(find …); do …
; dofind "$dir" -type f -name "*${pattern}*" -exec rm -i {} +
. Note that$dir
should be quoted (in double quotes); putting it in braces accomplishes nothing. – G-Man Says 'Reinstate Monica' Apr 10 '16 at 05:31{ }
is the proper way to do, and at the most unexpected moment, they save you a lot of headache, because you did not think how a variable changes or not changes when it is not quoted and comes next to some other variable or keyword. You are entitled to your opinion how to handle a case like this but please do not spread wrong beliefs of your like braces accomplish nothing – MelBurslan Apr 10 '16 at 06:13for file in $(find ...)
. Usefind ... -exec rm -i {} +
as he suggested orfind ... | while IFS= read file ; do rm -i "$file" ; done
. The former (-exec rm
) works for all files, even those with newlines in the filename, whereas the second (while
loop) works for all filenames except those with newlines. These are not matters of opinion, they are facts. – cas Apr 10 '16 at 08:49for
– glenn jackman Apr 10 '16 at 11:56"*${pattern}*"
is an example (albeit a flawed one) of what it’s good for — and it’s not a bad habit to get into. But it doesn’t accomplish anything when used on a free-standing variable, like${file}
in your answer. … (Cont’d) – G-Man Says 'Reinstate Monica' Apr 11 '16 at 05:33$(…)
) should always be quoted (unless you have a good reason not to); and doing that is a *much* more important habit to get into. And thinking that enclosing variable names in braces gives you the same sort of protection that double quotes give you is wrong, and a very dangerous way of thinking. … (Cont’d) – G-Man Says 'Reinstate Monica' Apr 11 '16 at 05:34$foo_bar
(or even"$foo_bar"
) when you meant${foo}_bar
(or"${foo}_bar"
or"$foo"_bar
), i.e., you want the value of$foo
+ the literal string_bar
, and you do any testing, you will discover your error immediately. But if you use$foo
(or even${foo}
) when you meant"$foo"
(or"${foo}"
), that error will cause you a lot of headache at the most unexpected moment; when you encounter a filename with special character(s) in it. – G-Man Says 'Reinstate Monica' Apr 11 '16 at 05:35