Loop through found files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through found files
# 1  
Old 04-21-2009
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

Code:
#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

Code:
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
# 2  
Old 04-21-2009
Try:

Code:
find "$dir" -name '*.txt' -exec rm -i {} \;

# 3  
Old 04-21-2009
Code:
for file in *.txt
do
   printf "%s: delete (Y/N): " "$file"
   read x
   case $x in
      [yY]) rm "$file" ;;
   esac 
   echo
done

# 4  
Old 04-22-2009
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

Code:
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.

Code:
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
# 5  
Old 04-22-2009
Hi,

Check the below code

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

# 6  
Old 04-22-2009
Quote:
Originally Posted by Calypso
Code:
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?

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

# 7  
Old 04-22-2009
@cfajohnson

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

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Continue the loop if the value is not found

Dear Help, Is it possible to continue the loop by going to the next available value, if the 'expected value' is not found. I have a list of values which might not get incremented by fixed value and hence the loop could break and the script could terminate. Any suggestion is appreciated. ... (1 Reply)
Discussion started by: Indra2011
1 Replies

2. Shell Programming and Scripting

While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found,... (5 Replies)
Discussion started by: laknar
5 Replies

3. Shell Programming and Scripting

Variable not found error in while loop

I am unable to use the value of a variable. while ] do LstFldDataPart =`head -n "$LstFldDataPartCntr" "${FeedFileDir}/${FeedFileBadRecs}" | tail -1 | tr -d '\n'` echo $LstFldDataPart JndLstFldDataPart="${JndLstFldDataPart}${LstFldDataPart}" LstFldDataPartCntr=... (3 Replies)
Discussion started by: TomG
3 Replies

4. Shell Programming and Scripting

while loop error. (command not found)

can any1 please tell me what is problem with following code: i=1; cat test| while read CMD; do Var$i=$CMD; or Var$i=$(echo $CMD) ; let i++ doneI keep getting error : line 4: Var1=sometext: command not found (2 Replies)
Discussion started by: kashif.live
2 Replies

5. Shell Programming and Scripting

For loop and file not found

Hi All, I have a simple script.sh file. It gives me " file.txt' for reading (no such file or directory)" Why can this happen? %ls ch1file.txt ch2file.txt ch3file.txt % chmod +x script.sh % bash % ./script.sh where script.sh is as follows for i in seq(3) do awk '{print $1}'... (1 Reply)
Discussion started by: senayasma
1 Replies

6. Shell Programming and Scripting

Renaming of files with different extensions on the same path to .found with the help of loop

hi , I have certain files at the same path with differeent extensions like .dat , .txt etc ...........i need to rename them with extension .found at the same path with the help of loop.... also the files names will be like this ; abc_2010_c1.dat abc_2010_c2.dat xyz_2010_c1.txt (2 Replies)
Discussion started by: amitpta
2 Replies

7. Shell Programming and Scripting

Loop through between 2 found lines

Hi frnd , hv nice week ahead, in shell script i found 2 words as below 1 ) targ_found=`sed -n \`echo $line_no\`p sesslog.txt | grep -ic 'Targ Rowid'` 2 ) bracket_found=`sed -n \`echo $line_no\`p sesslog.txt | grep -wc ')'` now we got he line number of 2 words. now i want to... (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

8. Shell Programming and Scripting

file not found error during loop

Hi, I have a strange problem and I'm sure its because I'm doing something stupid. When I loop through all of the files in a directory they cannot be found. Any help would be greatly appreciated. Thanks, -E $ touch a.txt $ touch b.txt $ ls a.txt b.txt $ for f in `ls` ; do... (3 Replies)
Discussion started by: earls
3 Replies

9. Shell Programming and Scripting

find command in while loop - how to get control when no files found?

I have the following statement in script: find ${LANDING_FILE_DIR}${BTIME_FILENAME_PATTERN2} -print | while read file; do ... done When there are no files located by the find comand it returns: "find: bad status-- /home/rnitcher/test/....." to the command line How do I get control in... (3 Replies)
Discussion started by: mavsman
3 Replies

10. Shell Programming and Scripting

variable not found with loop?

I cannot seem to pass the value of the variable assigned in the "else" statment below to an "if" statement outside of the loop. When I debug using "set -x", I find that the value of "$A" is empty. Is there something I'm missing here? Code is below: do if ] then echo "Good Match"... (2 Replies)
Discussion started by: douknownam
2 Replies
Login or Register to Ask a Question