LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 06-19-2008, 04:23 AM   #1
nickleus
Member
 
Registered: Nov 2004
Location: Noreg
Distribution: ubuntu
Posts: 107

Rep: Reputation: 15
how to pass pipe output to file command


i want to recursively find all files in a folder and subfolders that are encoded as utf-8, using bash.

my verbal structure so far looks like this:
recursively find all files in folder and subfolders|file --mime filename|grep UTF-8

but i hit a wall at the "file --mime" part. i've tried testing it like this:
Code:
ls | grep index.jsp | file --mime
but i get the following error:
Quote:
Usage: file [-bcikLhnNrsvz0] [-e test] [-f namefile] [-F separator] [-m magicfiles] file...
file -C -m magicfiles
Try `file --help' for more information.
anybody have a solution?

thanks in advance =)
 
Old 06-19-2008, 04:31 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Piping the output of ls passes a string to the file command and it is not interpreted as a file. You can use find with -exec option or use xargs, for example
Code:
find . -name index.jsp -exec file --mime {} \;
or
Code:
ls | grep index.jsp | xargs file --mime
 
Old 06-19-2008, 04:41 AM   #3
nickleus
Member
 
Registered: Nov 2004
Location: Noreg
Distribution: ubuntu
Posts: 107

Original Poster
Rep: Reputation: 15
weird. it passes a *filename* as a string, so i would think that would be good enough, but apparently not.
xargs seems way cleaner. i think i'll use that. i fear find, just like garth fears change.
wicked. thanks for the quick reply =)

Nick

Last edited by nickleus; 06-19-2008 at 04:42 AM.
 
Old 06-19-2008, 04:45 AM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
or
ls | grep index.jsp | file --mime -
 
Old 06-19-2008, 04:50 AM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Or:
Code:
ls | grep index.jsp | file --mime -
The man page states:
Quote:
Originally Posted by man file
-f, --files-from namefile
Read the names of the files to be examined from namefile (one per
line) before the argument list. Either namefile or at least one
filename argument must be present; to test the standard input,
use '-' as a filename argument.
What this method doesn't do though, is preserve the filename section in the output of the file command. So instead of getting
Code:
index.jsp: some-file-type
you get
Code:
/dev/stdin: some-file-type
colucix's examples don't have this problem, but I thought I'd point out the '-' as a filename, meaning standard input, which can be used in many programs (tar, bzip etc.).

edit: Damn, I type too slowly!

Last edited by pwc101; 06-19-2008 at 04:51 AM. Reason: post humourous remark about my typing speed... ;)
 
Old 06-19-2008, 05:03 AM   #6
nickleus
Member
 
Registered: Nov 2004
Location: Noreg
Distribution: ubuntu
Posts: 107

Original Poster
Rep: Reputation: 15
awesome answers nx5000 and pwc101. thanks a lot for info =) i am smarter now.
 
Old 06-19-2008, 06:18 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Code:
ls | grep index.jsp | file --mime
This looks like you want the mime type of the index.jsp file. For that you could use "file --mime index.jsp".

Unless you have files of the pattern "*index.jsp". For that you can use "file --mime *index.jsp".

If the files to test have a certain pattern, you can wildcards instead of using ls or find.

file --mime dir1/*index.jsp dir2/*index.jsp *index.jsp

---

A caveat is when you have a very large number of files in the directory matching the pattern. This could cause an out-of-memory error in the shell because you exceed the maximum number of arguments. In that case, pipe find to xargs as in colucix's second example and use the -n or -L option. For your original goal of finding the mime type of all files, you will probably want to do this.

Also, because filenames may contain white space characters, you will want to use the -print0 option to find and the -0 option for xargs:
Code:
find ./ -type f -print0 | xargs -0 -L 500 file --mime >file-mime.list

Last edited by jschiwal; 06-19-2008 at 06:22 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How can I pipe output of a command into arguments of a script HGeneAnthony Linux - General 3 12-26-2007 06:24 AM
Pipe output of command to php !!! ALInux Programming 7 12-30-2006 12:36 PM
how to pipe and parse output of a command learnfast Linux - Newbie 2 06-15-2005 04:55 AM
pipe xine output to file sohmc Linux - Software 0 01-01-2005 10:03 PM
pipe output to append to a text file davee Linux - Newbie 3 08-27-2003 05:03 AM


All times are GMT -5. The time now is 12:31 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration