determine file type by content, not extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting determine file type by content, not extension
# 1  
Old 07-23-2010
determine file type by content, not extension

I have a directory structure with multiple sub directories. Within each directory, there are files without extensions. Is it possible to somehow tell what the file type should be by the file contents?

For example, I opened one of the files using an editor. After scrolling to the end of the file, I can tell the file is a Microsoft Word document. So after closing the file, I add ".doc" to the end of the filename.

I want to automate this process if possible.
# 2  
Old 07-23-2010
Code:
for file in $(find /PATH -type f)
do
  tail -10 "$file"|grep "special Microsoft Keyword
  if [ $? -eq 1 ]; then
     echo " $file is not Microsoft Word file."
  else
     echo mv "$file" "$file.doc"
  fi
done

after confirm, remove second echo.
# 3  
Old 07-23-2010
Hi.
Quote:
[command] file tests each argument in an attempt to classify it. There are three
sets of tests, performed in this order: filesystem tests, magic tests,
and language tests.

-- excerpt from man file, GNU/Linux
cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Facing issues with Content-Type:application/x-download Content-Disposition:attachment

I am in the process of developing a perl cgi page. I had succeeded in developing the page but there are few errors/issues with the page. description about cgi page: My CGI page retrieves all the file names from an directory and displays the files in drop down menu for downloading the... (5 Replies)
Discussion started by: scriptscript
5 Replies

2. Shell Programming and Scripting

Determine Content-Type

I am limited to using only libraries that come with the default install of ActivePerl. I'd like to be able to determine the content type of a file on the fly. I thought CGI.pm might do it, but it looks a little weird to me from the perldoc. If someone has a quick way to determine the type of... (4 Replies)
Discussion started by: mrwatkin
4 Replies

3. Solaris

Solaris Cmd to determine type of zone?

I lost my notes on the subject, but I remember running across a single Solaris command that tells you the following Global zone vs local zone Sparse local zone vs Whole Root local zone Can anyone advise? Thanks-In-Advance!! (2 Replies)
Discussion started by: ckmehta
2 Replies

4. HP-UX

How to determine the fie system type?

Good day I need create new mount points on a server. I'm not very familiar with HP-UX and was given steps on how to go about doing the mounting, etc. Before I start though, I need to determine which file system type is currently being used. This will determine whether i need to extend the... (3 Replies)
Discussion started by: notreallyhere
3 Replies

5. Shell Programming and Scripting

Check file content type

I have a shell script that takes a file and uses "syncsort" to sort contents. I want to add a condition to check whether the input file is textual or binary format. If textual, the "syncsort" will be used to sort the files contents. Otherwise, the sorting process will be skipped. Note that the... (3 Replies)
Discussion started by: synthea
3 Replies

6. UNIX for Dummies Questions & Answers

How to determine what type of backup I've on the tape

I've tape which I've to use to restore data. The problem is that I don't know what type of backup I have on this type. Can someone help me and tell me how can I determine what type of backup is on the tape? Thanks for your help. (2 Replies)
Discussion started by: fraydey
2 Replies

7. Shell Programming and Scripting

Using 'stat' to determine file system type (with Zenity)

edited and removed (0 Replies)
Discussion started by: mdpalow
0 Replies

8. UNIX for Dummies Questions & Answers

Lynx - Downloading - extension handling - changing mime type?

Using Lynx, when I try to download a .rar, it confirms I want to download and its got it as an appication/rar file. However, split archives that end in .r## (.r00, .r01 ...) are not recognized as an appication/rar file and it reads the file like a .txt or .html. How can I fix this? Thanks! (2 Replies)
Discussion started by: yitzle
2 Replies

9. Shell Programming and Scripting

determine file type with perl

Hello i will like to know please how can i determine file type inside perl script not using the unix "file" program Thanks allot (1 Reply)
Discussion started by: umen
1 Replies

10. AIX

Command to determine Tape Type

I have 2 UNIX boxes and I need to determine whether or not they possess the same tape drive. What is the hardware command to determine what type of tape drive is present? Thx in advance. (4 Replies)
Discussion started by: Surdeymon
4 Replies
Login or Register to Ask a Question