Here is my loop

ls -ltrd * | head -n -3 | awk '{print $NF}' | while read y; do

rm -iR $y



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 ?

  • 2
    might have less to do it being a loop, but the fact you are using STDIN for piping in data instead of user interaciton.
    – sborsky
    Oct 18, 2017 at 14:10

1 Answer 1


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

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):

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

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)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .