- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: UNIX script to check file type
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2006 06:47 PM
06-09-2006 06:47 PM
UNIX script to check file type
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2006 07:19 PM
06-09-2006 07:19 PM
Re: UNIX script to check file type
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2006 07:52 PM
06-09-2006 07:52 PM
Re: UNIX script to check file type
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2006 11:37 PM
06-09-2006 11:37 PM
Re: UNIX script to check file type
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2006 07:48 AM
06-10-2006 07:48 AM
Re: UNIX script to check file type
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