I am using xargs and rm with find in order to remove files that follow a specific pattern defined by find as follows.
touch file
find . -name file | xargs rm
Everything works fine with the above code, but if I enter the -i option to rm, then executing:
touch file
find . -name file | xargs rm -i
prints:
rm: remove regular empty file ‘./file7’? user@host:$
without letting me enter y or n ? Thus the file is not removed.
What's the problem in here ?
I also am aware of -p parameter of xargs which works but it is more general. That is, it prompts user to execute the specific command, which is not user-friendly.
EDIT: While using it in a shell script, I've found out that it executes the command I want but it also prints find's result. Something that it is not preferable.
Also when I pass the ls file2
command to cmd variable, it does not specifically print the file2's specs. Same thing happens when I type
rm -i file2
. It prompts me to delete every single file in the directory.
This is my script:
#!/bin/bash
touch file{1..9} # Create 9 files named file1,file2...file9
echo -n 'command: ' # Prompt user
read -e cmd # read command
find . -exec ${cmd} '{}' +
The only solution I can think of, is to prompt the user separately for his preferred pattern. Then store that pattern to a variable and pass it as an argument to find command's -name option.
Something like this:
#!/bin/bash
touch file{1..9} # Create 9 files named file1,file2...file9
echo -n 'command: ' # Prompt user for command
read -e cmd # read command
echo -n 'pattern: ' # Prompt user for pattern
read -e pattern
find . -name "$pattern" -exec ${cmd} '{}' +
But even with this solution, find's output is still printed and if the pattern field is left empty (i.e the user just does not want to use a pattern) then problems occur.
Any Ideas ?