LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   file $(ls Pictures/ | grep zip) gets me errors. Am I doing it wrong? (https://www.linuxquestions.org/questions/linux-newbie-8/file-%24-ls-pictures-%7C-grep-zip-gets-me-errors-am-i-doing-it-wrong-894675/)

Nassosdim 07-30-2011 03:06 PM

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.

colucix 07-30-2011 03:41 PM

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

Nassosdim 07-30-2011 04:12 PM

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. :(

Tinkster 07-30-2011 05:00 PM

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

Nassosdim 07-30-2011 05:02 PM

Quote:

Originally Posted by Tinkster (Post 4429540)
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 :D

MTK358 07-31-2011 06:49 AM

Also note thet it's almost always a bad idea to parse the output of ls, and it's never necessary.

David the H. 07-31-2011 08:12 AM

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


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