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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-01-2004, 01:36 PM
|
#1
|
Member
Registered: Dec 2002
Location: Nevada
Distribution: fedora, RHEL, ubuntu, suse
Posts: 343
Rep:
|
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
|
|
|
01-01-2004, 01:52 PM
|
#2
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
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 \;
|
|
|
01-01-2004, 03:02 PM
|
#3
|
Member
Registered: Dec 2002
Location: Nevada
Distribution: fedora, RHEL, ubuntu, suse
Posts: 343
Original Poster
Rep:
|
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?
|
|
|
01-01-2004, 03:05 PM
|
#4
|
Senior Member
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552
Rep:
|
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.
|
|
|
01-01-2004, 07:01 PM
|
#5
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
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.
|
|
|
01-01-2004, 08:43 PM
|
#6
|
Member
Registered: Aug 2002
Location: MN USA
Distribution: slakware 9.0
Posts: 121
Rep:
|
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.
|
|
|
01-01-2004, 09:12 PM
|
#7
|
Member
Registered: Aug 2002
Location: MN USA
Distribution: slakware 9.0
Posts: 121
Rep:
|
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.
|
|
|
01-01-2004, 09:25 PM
|
#8
|
Member
Registered: Dec 2002
Location: Nevada
Distribution: fedora, RHEL, ubuntu, suse
Posts: 343
Original Poster
Rep:
|
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
|
|
|
01-01-2004, 09:57 PM
|
#9
|
Member
Registered: Dec 2002
Location: Nevada
Distribution: fedora, RHEL, ubuntu, suse
Posts: 343
Original Poster
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 06:27 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|