Loop through found files

Hi I am trying to write a script which will loop through all files that end in ".txt" and ask user if they want to delete the file or not

#this print out all files
dir=/root/etc/
find $dir -name "*.txt"

output:
1.txt
2.txt
etc

but what i really want is

1.txt delete(Y/N):
2.txt delete(Y/N):
etc

I have tried loops for example

for file $dir in "*.txt"; do
   echo "$file    delete (y/n)"
done

but they dont seem to be working, any ideas are very much appreciated thanks

Try:

find "$dir" -name '*.txt' -exec rm -i {} \;
for file in *.txt
do
   printf "%s: delete (Y/N): " "$file"
   read x
   case $x in
      [yY]) rm "$file" ;;
   esac 
   echo
done

First of all thanks for the replies, the set of commands dont exactly do what i want though unfornutely
@radoulov
Is it possible to modify this slightly to ask the user first before deleting, I have been trying to play around with but cant figure out correct syntax

find $dir -name "*.txt" -exec echo "delete y/n" {} \; read x {} \;

@cfjohnson
this doesnt seem to search the correct directory, I found an example which will search the correct dir e.g.

for file in `find $dir -name "*.txt"`; do

The only problem is that some files have spaces in their names for example "/etc/space inFilename"

and then it prints the file out twice ie

/etc/space
inFilename

is there anyway to print it out as one?

Thanks again

Calypso

Hi,

Check the below code

 
#!/bin/sh
for file in *.txt
do
        echo " \"$file\" delete(Y/N) :"
        read opt
        case $opt in
        [yY])rm "$file"
                echo "$file removed........."
        ;;
        esac
done

find $dir -name "*.txt" |
  while IFS= read -r file; do

@cfajohnson

Im afraid it still doesnt seem to work, now the script takes out all spaces so when i do

find $dir -name "*.txt" | while IFS= read -r file; do
  mv -i $file `echo file | sed 's/ //g'`
done

i just get the following errors
/etc/file: No such file or directory
name: No such file or directory

hi,

did u try the code given by me ?

Quote variable references:

mv -i "$file" `echo "$file" | sed 's/ //g'`

@abinaya
sorry, yes i did try your solution and im afraid i couldnt get it to work either

These are two methods which are very close but still dont work, any other ideas

#method1
find $dir -name "* *" | while IFS= read -r file; do
echo "$file remove spaces (Y/N)"
read opt
case $opt in
[yY])  mv -i "$file" `echo "$file" | sed 's/ //g'`;;
[nN])  ;;
*) echo "input not regonised";;
  esac
done

this script does not wait for the user input example output looks like
spaceDir/ 234.txt remove space (Y/N)
input not regonised
spaceDir/12 34.txt remove space (Y/N)
input not regonised

method 2
for file in `find $dir -name "* *"`; do
 printf "%s: remove spaces(Y/N): " "$file"
  read opt
  case $opt in
    [yY])  mv -i "$file" `echo "$file" | sed 's/ //g'`;;
    [nN])  ;;
    *) echo "input not regonised";;
  esac
done

This method waits for the user input but does not print out the file correctly e.g
spaceDir/file remove spaces(Y/N): y
mv: cannot access spaceDir/file
spaceDir/name remove spaces(Y/N): y
mv: cannot access spaceDir/name

Thanks in advance

Hi,

I have created few files with spave in between.My code works as follows.

# more test1
#!/bin/sh
for file in *.txt
do
echo " \"$file\" delete(Y/N) :"
read opt
case $opt in
[yY])rm "$file"
echo "$file removed........."
;;
esac
done
# touch "1 .txt" "2 .txt"
# touch "4.txt" "_3.txt"
# ls *.txt
1 .txt 2 .txt _3.txt 4.txt
# ./test1
"1 .txt" delete(Y/N) :
y
1 .txt removed.........
"2 .txt" delete(Y/N) :
y
2 .txt removed.........
"4.txt" delete(Y/N) :
y
4.txt removed.........
"_3.txt" delete(Y/N) :
y
_3.txt removed.........
# ls -l *.txt
*.txt: No such file or directory
#

Thanks abinaya, very useful
Calypso