LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 03: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, 03:41 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 04: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, 05:00 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 05: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, 06:49 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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, 08:12 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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



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 03:55 AM
grep search in zip/rar... etc plainkeyman Linux - Software 2 05-23-2005 06:05 PM
create a self-extracting zip file with zip on solaris? samsolaris Solaris / OpenSolaris 3 10-15-2004 01:50 AM
how to view a html file with pictures from within a zip archive? Blaumieser Linux - Newbie 0 02-12-2004 11:05 AM
How do I zip and attach the output data of a grep | awk | mail shell script? 360 Programming 1 05-08-2002 08:26 AM

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

All times are GMT -5. The time now is 05:17 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
Open Source Consulting | Domain Registration