Check if a file type exists repeatedly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if a file type exists repeatedly
# 1  
Old 06-19-2017
Hammer & Screwdriver Check if a file type exists repeatedly

Hi,

I have the below script that checks if the .tar file exists in my present directory. If so, extract it and then delete it except if the .tar filename is blent.tar

Code:
#!/bin/bash
while [ -f *.tar ] ; do
echo "TAR BALL EXISTS"

   for f in *.tar; do
   echo "File -> $f"
   tar -xvf $f
   rm -ir $f
   done

 done

The above extracts & deletes even the blent.tar which is something I do not want. My requirement is the keep doing untar of the <filename>.tar file till either no .tar file is left or this file [blent.tar] is left behind and the loop completes.

So, when the loop completes all the .tar files should be gone except the blent.tar.

Can you please suggest.

Last edited by mohtashims; 06-19-2017 at 10:00 PM..
This User Gave Thanks to mohtashims For This Post:
# 2  
Old 06-19-2017
Untested.

Code:
ls *.tar | while read f
do
    if [ -f "$f" ] && [ "$f" != blent.tar ]
    then
        echo "$file to extract and delete after"
        # uncomment the next line if you like what you see.
        #tar xvf "$f" && rm -i "$f"
    fi
done

# 3  
Old 06-20-2017
Hammer & Screwdriver

Quote:
Originally Posted by Aia
Untested.

Code:
ls *.tar | while read f
do
    if [ -f "$f" ] && [ "$f" != blent.tar ]
    then
        echo "$file to extract and delete after"
        # uncomment the next line if you like what you see.
        #tar xvf "$f" && rm -i "$f"
    fi
done

No... if the .tar contains .tar files which also contains .tar file this will not work as this will check for all .tars in the first run but it will not be able to check the new .tar generated as a result of tar xvf "$f"

I am basically looking for blent.tar exemption in this statement
Code:
while [ -f *.tar ] ;

# 4  
Old 06-20-2017
Quote:
Originally Posted by mohtashims
No... if the .tar contains .tar files which also contains .tar file this will not work as this will check for all .tars in the first run but it will not be able to check the new .tar generated as a result of tar xvf "$f"

I am basically looking for blent.tar exemption in this statement
Code:
while [ -f *.tar ] ;

Of course you haven't told us what operating system or shell you're using, and what you are basically looking for is a syntax error if there is ever more than one tar file in that directory...

Maybe the following (totally untested) code will come close to what you're tying to do:
Code:
#!/bin/ksh
found=1
while [ $found -eq 1 ]
do	found=0
	for tf in *.tar
	do	if [ -f "$tf" ] && [ "$tf" != blent.tar ]
		then	found=1
			tar xvf "$tf" && rm -i "$tf"
			ec=$?
			if [ $ec -ne 0 ]
			then	exit $ec
			fi
		fi
	done
done

Note that it will exit if extracting and removing an archive fails instead of going into an infinite loop or attempting to remove archives even if they couldn't be extracted as requested in your specifications. ('m not a big fan of throwing away data nor of infinite loops.

Note that the tar xvf "$tf" && rm -i "$tf" line has been updated to fix typos noted by RudiC and mohtashims.

Last edited by Don Cragun; 06-20-2017 at 05:13 PM.. Reason: Fix typos: s/tar xvf/tar xvf "$tf"/ && s/rm -i"$f"/rm -i "$tf"/
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 06-20-2017
Quote:
Originally Posted by Don Cragun
Of course you haven't told us what operating system or shell you're using, and what you are basically looking for is a syntax error if there is ever more than one tar file in that directory...

Maybe the following (totally untested) code will come close to what you're tying to do:
Code:
#!/bin/ksh
found=1
while [ $found -eq 1 ]
do	found=0
	for tf in *.tar
	do	if [ -f "$tf" ] && [ "$tf" != blent.tar ]
		then	found=1
			tar xvf "$tf" && rm -i "$f"
			ec=$?
			if [ $ec -ne 0 ]
			then	exit $ec
			fi
		fi
	done
done

Note that it will exit if extracting and removing an archive fails instead of going into an infinite loop or attempting to remove archives even if they couldn't be extracted as requested in your specifications. ('m not a big fan of throwing away data nor of infinite loops.
This needs to run on both Linux and Solaris as part of the automation.
# 6  
Old 06-20-2017
And in what way does the code I suggested fail if you run it on a Solaris system in the directory that contains the tar files you want to extract and remove and on a Linux system in the directory that contains the tar files you want to extract and remove?
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 06-20-2017
Hammer & Screwdriver

Quote:
Originally Posted by Don Cragun
And in what way does the code I suggested fail if you run it on a Solaris system in the directory that contains the tar files you want to extract and remove and on a Linux system in the directory that contains the tar files you want to extract and remove?

I think you had a typo here tar xvf "$tf" && rm -i "$tf"

Thank you so much Don !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if file exists

I need to check whether a file exists and has been changed. The file should contain a specific string. The file should also have been changed within the last ten seconds. How do I do that? (3 Replies)
Discussion started by: locoroco
3 Replies

2. Shell Programming and Scripting

To check if file exists

Hi, I have the below code written. However I am not getting the desired output I am checking if the particular path has file in it. #!/bin/bash ls -l /IRS2/IRS2_ODI/INFILE/*LS* 1>/dev/null 2>/dev/null if then echo $? echo "File Exists" fi ... (3 Replies)
Discussion started by: Shanmugapriya D
3 Replies

3. Shell Programming and Scripting

Check if file exists or not

Hi, I want to check if the file exists or not in the directory. i am trying below code but not working. File="/home/va59657/Account_20090213*.dat" echo "$File" if ]; then echo "file found" else echo "file not found" fi However i am getting file not found even if file exits as... (5 Replies)
Discussion started by: Vivekit82
5 Replies

4. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

5. Shell Programming and Scripting

how to check to see if a file exists?

I want to write a script to see if various files exist. What I want to do is have the script search in various directories if a file exist, and if not, then output something like "/path/file does not exist". I don't actually know of how to check and see if a file exists or not. What I have in mind... (2 Replies)
Discussion started by: astropi
2 Replies

6. Shell Programming and Scripting

Check to see if a file exists?

Hi. I'd like to have an IF-Then-Else statement where I can check to see if a file exists? We have the Bourne Shell by default. I'm looking for the syntax to do something like this: if myfile.txt exists then ...my code else ...my code end if Any help would be greatly... (5 Replies)
Discussion started by: buechler66
5 Replies

7. Shell Programming and Scripting

check if directory and file exists

cp $PATHLOGS/$DATE/*.* $TMP/logs_tmp/ cp $PATHLOGS/$DATE1/*.* $TMP/logs_tmp/ Before copying the files I have to check if the directory $DATE1 and $DATE2 exists. If directory exists then, check if the folder contains some files. if the file exists then, check if the file size is greater... (3 Replies)
Discussion started by: sandy1028
3 Replies

8. Shell Programming and Scripting

Script to check if file exists

guys, I am trying to write a script that does the following: it looks for a file in a specific directory and if the file is not there (NOT), it emails me. I have tried the following but its not working. It simply hangs up. Please help. if then mail -s 'blah blah blah' my email... (4 Replies)
Discussion started by: basisvasis
4 Replies

9. Programming

Check if file exists + ulp :S

Hi all! I know that this may not be the best forum to ask ulp and eagle related question. But the guys from eagle didn't know the answer and this is the BEST programming forum I know :D How can I check if a file exists while programming eagle's ulp's? There is not much information on-line.... (2 Replies)
Discussion started by: ruben.rodrigues
2 Replies

10. Shell Programming and Scripting

Check if a file exists with certain prefix

Hi guys, How would i check a file exists with certainprefix? i have a directory with some files: ABC1 ABC2 ABC3 etc.. and want to do: please note i am using the korn shell environment.As when i gone through some stuff on then net i came to know some of the options will work... (11 Replies)
Discussion started by: raoscb
11 Replies
Login or Register to Ask a Question