LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-30-2011, 04:06 PM   #1
Nassosdim
LQ Newbie
 
Registered: Jul 2011
Location: Athens, Greece
Distribution: Ubuntu and Fedora
Posts: 3

Rep: Reputation: Disabled
file $(ls Pictures/ | grep zip) gets me errors. Am I doing it wrong?


I am reading a book regarding Unix/Linux commands and this here gets me into questions.

While I can use the output of a command as an expansion if I don't include a path in the ls part, including a path throws an error.

So file $(ls Pictures/ | grep zip) from the home directory results in error while changing to Pictures/ beforehand doesn't.
So, using file $(ls | grep zip) works fine.

http://cl.ly/2L0R2u431M3h442L051v

I'm using Ubuntu 10.04 LTS if that has something to do with it.
Thanks in advance.
 
Old 07-30-2011, 04:41 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
When the argument of ls is a directory, the directory content is displayed and each file is listed without its actual path. In many circumstances the usage of ls can be avoided in favor of shell's globbing capabilities, e.g.
Code:
file Pictures/*.zip
 
Old 07-30-2011, 05:12 PM   #3
Nassosdim
LQ Newbie
 
Registered: Jul 2011
Location: Athens, Greece
Distribution: Ubuntu and Fedora
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you very much. Yeah, that was kind of example taken from the book and wanted to know if I'm doing it wrong or that the example is wrong.
 
Old 07-30-2011, 06:00 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

Just a word of advice: for your intents you would have been better
off using copy & paste from a terminal into a
PHP Code:
[CODE][/CODE
block.

No external links, no uploads, much faster.



Cheers,
Tink
 
Old 07-30-2011, 06:02 PM   #5
Nassosdim
LQ Newbie
 
Registered: Jul 2011
Location: Athens, Greece
Distribution: Ubuntu and Fedora
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tinkster View Post
Hi, welcome to LQ!

Just a word of advice: for your intents you would have been better
off using copy & paste from a terminal into a
PHP Code:
[CODE][/CODE
block.

No external links, no uploads, much faster.



Cheers,
Tink
I'll keep that in mind
 
Old 07-31-2011, 07:49 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Also note thet it's almost always a bad idea to parse the output of ls, and it's never necessary.
 
Old 07-31-2011, 09:12 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
file $(ls Pictures/ | grep zip)
This command is doing three things.

First, the ls command outputs the contents of the Pictures directory.

Then grep searches for lines from that output that match "zip". There can be zero, one, or more filenames.

Finally, the file command attempts to read those names that grep matches.

So try running each step in turn and see what you come up with.

Well, it turns out that the problem is with the output of ls. "ls Pictures/" gives you a list of filenames inside Pictures, but without the full path included. That is, you're getting file.zip when you really need Pictures/file.zip. So when file tries to read them, it looks in your present working directory for them and comes up with a missing file error.

You've just discovered, as MTK358 mentioned, one reason not to parse ls for filenames.

http://mywiki.wooledge.org/ParsingLs
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pixel errors in pictures with ati drivers, TooMpa Linux - Hardware 1 01-05-2010 04:55 AM
grep search in zip/rar... etc plainkeyman Linux - Software 2 05-23-2005 07:05 PM
create a self-extracting zip file with zip on solaris? samsolaris Solaris / OpenSolaris 3 10-15-2004 02:50 AM
how to view a html file with pictures from within a zip archive? Blaumieser Linux - Newbie 0 02-12-2004 12:05 PM
How do I zip and attach the output data of a grep | awk | mail shell script? 360 Programming 1 05-08-2002 09:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:42 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration