Artificial intelligent assistant

rm -iR does not work inside a loop Here is my loop ls -ltrd * | head -n -3 | awk '{print $NF}' | while read y; do rm -iR $y done output: > rm: descend into directory `oct_temp17`? rm: descend into directory `oct_temp18`? rm: descend into directory `oct_temp19`? .... It does not prompt for user confirmation which rm -i should ideally when run manually. I m using bash shell on Linux 3.10 I am naive to Unix / Linux. Can you please suggest how can i make the script ask me for confirmation for every folder from the ls output ?

## 1\. Why you example did not work as expected

`rm`'s prompts need STDIN to receive your feedback. In your example you used STDIN to pipe a list to the `while` loop though, thus `rm` was getting the answers to it's prompts from your `ls`/`head`/`awk` commands, instead of the user.

The solution is to not use STDIN for providing the list to the loop - e.g.:


for y in $(ls -ltrd * | head -n -3 | awk '{print $NF}'); do
rm -iR $y
done


## 2\. Safer way to do this

Be ready for filenames containing spaces (you don't need `awk`, to get the filename, you can just tell `ls` to only print the filename in the 1st place):


IFS=
for y in $(ls -1qtrd * | head -n -3); do
rm -iR "$y"
done


## 3\. An easier way to do this

As Archemar pointed out: You don't even need a loop (as long as there are no spaces in filenames).


rm -iR $(ls -1qtrd * | head -n -3)

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 9ae5fb5811ffedd70c3635736f7a300c