Operating System - HP-UX
1831420 Members
3221 Online
110025 Solutions
New Discussion

Re: UNIX script to check file type

 
Danny Fang
Frequent Advisor

UNIX script to check file type

Hi,

I'm attempting to place the command "file" in the script to check for specific file types.

I have some files in a diretory which has the .gzip extension. However, upon doing a check on such files, I found that they are not real gzip-ed files:
abkadirk[RNC10]$ file f1.gz
f1.gz: ASCII text

The real gzip file would be:
abkadirk[RNC10]$ file f2.gz
f2.gz: gzip compressed data, was "f2", from Unix

Therefore, may I know how do I implement a check a in my UNIX script which goes:

$INPUTD_DIR="/home/danny/samples"
for i in `ls $INPUT_DIR`
do
## if file has .gzip extension then
## if 'file' returns true if file type is gzip i.e. what value does the "file" return when it detected a gzip file?
## then unzip files in $INPUT_DIR
## end if
## move "incorrect" gzip files into /tmp dir.

Could anyone show me how do I script the portions in marked in ##, especially on checking the file extension and checking the return type of "file" command to see if it had returned a "true" when it detected a gzip file?


Thanks
Danny


4 REPLIES 4
RAC_1
Honored Contributor

Re: UNIX script to check file type

for i in "/some_dir/*.gzip"
do
type=$(file ${i}|awk '{print $2}')
if [ ${type} = "gzip" ];then
mv ${i} /your_dest_for_gzip_file/.
elif [ ${type} = "ASCII" ];then
mv ${i} /tmp/.
else
echo "no files"
fi
done

Check it.
There is no substitute to HARDWORK
inventsekar_1
Respected Contributor

Re: UNIX script to check file type

nice script RAC.

i am half the way with my little scripting skills.
-----------------------------
$INPUT_DIR="/test_dir"
mkdir gzip_dir
mkdir not_gzip_dir

for i in `ls $INPUT_DIR`
do
if
file $i |grep compressed
then
mv $i gzip_dir
else
mv $i not_gzip_dir
fi
-----------------------------

not completed. i dont know how to uncompress.
someone complete the script.
Be Tomorrow, Today.
Peter Nikitka
Honored Contributor

Re: UNIX script to check file type

Hi,

if you have preselected the files (use RACs method), you can easely deflate all compressed files:
...
cd $INPUT_DIR # containing now only zipped files

gunzip * # or gunzip *.gz

See he manpage of gunzip to get options.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: UNIX script to check file type

Hi Danny,

Here's another variation you could use in order to unzip the real gzip'ed files while moving others to the /tmp folder. It doesn't use the "file" command but relies on gunzip's return code to determine whether it's a real gzip'ed file or not:

$INPUT_DIR="/home/danny/samples"
for i in `ls $INPUT_DIR`
do
gunzip $i || mv $i /tmp
done

~hope it helps