Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check a file name ?

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
CH
In the Korn Shell, how do I check if a filename contains a prefix ?<br>
eg.<br>
for file in /usr/files/*<br>
do<br>
# check if filename contains a &quot;.gz&quot;<br>
if ( grep &quot;.gz&quot; ${file} )<br>
then<br>
echo &quot;file is zipped&quot;<br>
else<br>
echo &quot;file is not zipped&quot;<br>
fi<br>
done<br>
<br>
The problem is that $file gets interpreted as the file content and I need it interpreted as the file name only ?<br>
<br>
Thanks<br>
Steve
 
try this<br>
<br>
--snip--<br>
for file in /usr/files/*<br>
do<br>
print $file ¦ grep '.gz$' 2&gt;&1 &gt; /dev/null<br>
if [[ $? = 0 ]]<br>
then<br>
print &quot;$file is gzipped&quot;<br>
else<br>
print &quot;$file is not gzipped&quot;<br>
fi<br>
done<br>
--snip--<br>
<br>
-ml<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Sorry Mike but your code does not work on my box.<br>
Using your idea I created the following ;<br>
for file in /usr/files/*<br>
do<br>
if ((echo $file ¦ egrep -c -i &quot;.gz&quot;) &gt; 0 )<br>
then<br>
print &quot;$file is gzipped&quot;<br>
else<br>
print &quot;$file is not gzipped&quot;<br>
fi<br>
done<br>
------------<br>
Thanks for the direction.<br>
Steve<br>

 
Another option would be to use ksh parameter substitution. Since you want to check a suffix, try removing that suffix and see if the result is still the same.<br>
<br>
$tfile=${file%.gz}<br>
if [ $tfile = $file ]<br>
then<br>
print &quot;$file is not gzip'ed&quot;<br>
else<br>
print &quot;$file is gzip'ed&quot;<br>
fi<br>
<br>
man ksh for more substitution commands. There is a prefix stripper also. :)<br>

 
Steve,<br>
<br>
Which bit of the code failed?<br>
<br>
Interesting idea from kaih by the way - completely different approach.<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
You don't need to involve grep if you're working in the Korn shell.<br>
<br>
Try this:<br>
<br>
if [ &quot;$file&quot; != &quot;${file%.gz}&quot; ]<br>
then<br>
print &quot;\&quot;$file\&quot; is gzipped&quot;<br>
else<br>
print &quot;\&quot;$file\&quot; is not gzipped&quot;<br>
fi<br>
<br>
<br>
<br>
CL<br>
<br>

 
Hello,<br>
<br>
the _basename_ command does exactly what you want:<br>
<br>
$( basename /a/file/path/and/name ) == name<br>
<br>
and<br>
<br>
$( basename /a/file/path/and/name.ext .ext ) == name<br>
<br>
then you can do<br>
<br>
fileext=$(basename &quot;$filename&quot;)<br>
file=$( basename &quot;$filename&quot; .ext )<br>
if [ &quot;$file&quot; = &quot;$fileext&quot; ]<br>
then<br>
--- file hasn't the extension<br>
else<br>
--- file has the extension<br>
fi<br>
<br>
I hope it works...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top