LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-01-2004, 01:36 PM   #1
illtbagu
Member
 
Registered: Dec 2002
Location: Nevada
Distribution: fedora, RHEL, ubuntu, suse
Posts: 343

Rep: Reputation: 30
need script expert for file command


I am trying to use the command "file" to search though a very large directory I have and hunt down any files that are of pdf, bmp, jpg, xls, doc etc. type. I do not want to do a simple file search I need to do it this way. Some files do not have extensions on them and some extensions might be the wrong ones. so far I have

find /mnt/user storage/* -iname "*" -exec file {} -z \;

The problem with this is I get a couple thousand output lines or more that I must search through. Is there anyway to limit the output to say just of pdf type only.

Thanks
 
Old 01-01-2004, 01:52 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Not being an expert and at the risk of giving another unwanted answer.....

How about using the extension *.pdf instead of just *

example...
find /mnt/user storage/* -iname "*.pdf" -exec file {} -z \;
 
Old 01-01-2004, 03:02 PM   #3
illtbagu
Member
 
Registered: Dec 2002
Location: Nevada
Distribution: fedora, RHEL, ubuntu, suse
Posts: 343

Original Poster
Rep: Reputation: 30
What up homey,

Thanks for trying to help

I tried your suggestion but the problem is when I use ..."*.pdf"... It does a search for any files with the *.pdf extension. I cant use this because its looking at the extension instead of the file type.

In other words I need to ignore each and every files extension and only do a search for a specific file type. for example the file might be named
file.tif
when really it is a pdf file and should be file.pdf
So really what I'm looking to do is a search by file type and not extension.

Thanks for trying to help me out though what makes you think that your help is not wanted?
 
Old 01-01-2004, 03:05 PM   #4
stickman
Senior Member
 
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552

Rep: Reputation: 53
Try using grep to filter the results:
find /directory -exec file {} \; | grep pdf

You can also remove the name option since it is irrelavant to the search.
 
Old 01-01-2004, 07:01 PM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
I did some serious digging around for a solution and the command line just doesn't seem to do the trick. The problem comes from the lack of file extensions so you can't just search for *.pdf because " -type d or -type f " refers to directory or file. I couldn't find any way to do a command line search for files by association type.

The good news is that Konqueror does the job very nicely. I took a known file type and removed the extension ( pic.bmp for instance ) then went to Tools / Find File... then to the Contents tab / File type: . I scrolled down to the file type which I was looking for and it was correctly located even without the extension.

That doesn't work if the file is grossly misnamed like naming a text file with a gif extension.
 
Old 01-01-2004, 08:43 PM   #6
sharper
Member
 
Registered: Aug 2002
Location: MN USA
Distribution: slakware 9.0
Posts: 121

Rep: Reputation: 15
Here's something that might be a start.

file * | grep -e HTML will list al HTML files in the directory whether or not they have an HTML extension. You could do the same thing with other file types. I don't know enough about bash scripting or awk to write something that would do it automatically.
 
Old 01-01-2004, 09:12 PM   #7
sharper
Member
 
Registered: Aug 2002
Location: MN USA
Distribution: slakware 9.0
Posts: 121

Rep: Reputation: 15
Another one. I ran this on one of my directories and the output is
FILETYPE FILENAME: sorted by FILETYPE

file * | awk '{print $2, $1}' | sort > output_file_to_inspect

You may have gawk instead of awk. But most distros have at least one of the two.

Last edited by sharper; 01-01-2004 at 09:14 PM.
 
Old 01-01-2004, 09:25 PM   #8
illtbagu
Member
 
Registered: Dec 2002
Location: Nevada
Distribution: fedora, RHEL, ubuntu, suse
Posts: 343

Original Poster
Rep: Reputation: 30
Cool thanks for all of the great help guys
here is what seems to work I have just tested this out really quick
find /mnt/user storage/ -exec file {} \; | grep -e pdf

I just noticed your last post sharper I will give awk a try. This is something I have never heard of (not that that means anything at all ) when I do a man awk I get
gawk - pattern scanning and processing language

I will post back here in a miniute when my machine is done doing its nightly scheduled dump. Kind of slows things down to a grinding hault .

Thanks again everyone
 
Old 01-01-2004, 09:57 PM   #9
illtbagu
Member
 
Registered: Dec 2002
Location: Nevada
Distribution: fedora, RHEL, ubuntu, suse
Posts: 343

Original Poster
Rep: Reputation: 30
The problem I had with awk is getting it to look at any files that are in sub-directories in other words I dont see a recursive switch.

I have had some wierd results with doing this
find /mnt/user storage/ -exec file {} \; | awk '{print $2, $1}' | sort > /home/my user name/Desktop/test-awk
I seems that it prints out all of the file names first then does another print out of the actual file type.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script File: Parsing command sent to shell cheema Programming 1 07-01-2005 12:54 PM
help! Script or command needed to replace text in a file. farmerjoe Linux - Newbie 2 01-02-2005 03:07 PM
How to start a Tcl/Tk script by simply invoking the script file itself ? cyu021 Programming 2 10-10-2004 11:00 AM
nfs a file shares ok via file mngr but not command line. Suse 9.1 acummings Linux - Networking 2 10-09-2004 02:23 PM
Script, Reading a file, When end of file? elibm Programming 2 07-16-2001 11:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration