LinuxQuestions.org
Visit Jeremy's Blog.
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-04-2011, 10:03 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,535

Rep: Reputation: 51
Can ls take input from stdin?


Hi:

I want to feed the output of locate to ls. Say
Code:
locate -r .*mp3$ | ls
But I've seen this has the same effect as
Code:
ls
I know there is a way, and a specific command. It's name is something like 'args'. I also know it's a fundamental command together with grep. There is a way, but I think is too complicated:
Code:
a1=$(locate granite)
ls -l $a1
Any idea?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-04-2011, 10:07 PM   #2
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 496

Rep: Reputation: 213Reputation: 213Reputation: 213
Code:
for i in "`locate -r .*mp3$`"; do ls "$i"; done
I'm sure someone will post an alternative using find/xargs.
 
1 members found this post helpful.
Old 07-04-2011, 10:33 PM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
Yes Diantre..
Code:
find . -name "*.mp3" | xargs ls -l
or

Code:
find . -name "*.mp3" -exec ls -l {} \;
or
Code:
find . -name "*.mp3" -ls
Where . is the current directory ..

Last edited by divyashree; 07-04-2011 at 10:52 PM.
 
1 members found this post helpful.
Old 07-05-2011, 12:28 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by stf92 View Post
There is a way, but I think is too complicated:
Code:
a1=$(locate granite)
ls -l $a1
Simpler: ls -l $( locate granite ) but. like the original, it breaks on file names that have spaces etc. in them.
 
Old 07-05-2011, 12:35 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
As above, but with a couple of caveats.

If the command you're using with -exec can take multiple arguments for input, then you can replace the final semicolon with a plus sign. This makes the command execute in only one (or as few as possible) processes, like xargs, whereas the semicolon executes the command once for each input,
Code:
find . -name "*.txt" -exec ls -l {} \+
When using xargs on input that could contain spaces or other reserved characters, you should use the null-separator for processing. Using -print0 in find will output the filenames delimited by nulls instead of newlines, and the -0 in xargs tells it to read the null-separated input.
Code:
find . -name "*.mp3" -print0 | xargs -0 ls -l
Without it, xargs will try to execute ls on individual parts of filenames, and you'll get "file not found" errors in return.

locate also has null-separator output ability, (using -0).
Code:
locate -0 -r ".*mp3" | xargs -0 ls -l
 
2 members found this post helpful.
Old 07-05-2011, 02:36 AM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,535

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by catkin View Post
Simpler: ls -l $( locate granite ) but. like the original, it breaks on file names that have spaces etc. in them.
For this, a simple remedy: ls -l "$(locate granite)".

To everybody else: thanks, thanks a lot. Your posts are full of examples of the use of find, which has such a complex syntax and of xargs.
 
Old 07-05-2011, 07:40 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Quote:
Originally Posted by stf92 View Post
For this, a simple remedy: ls -l "$(locate granite)".
No, that doesn't work either, because then the entire output of locate is treated as a single word. The problem becomes the inverse of the non-quoted version. The only time this works correctly is if only a single file is found.

There's really no way to safely use simple command substitution when names can contain spaces. You need to be able to generate output in a way that treats each name as a separate entity, and to do that you need commands like find and xargs that allow control over the delimiting format, and/or loops of some kind (while+read in particular) to iterate over the entries.

See here:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/BashFAQ/020

Feel free to follow some of the links they provide for even more detail.
 
Old 07-05-2011, 07:44 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
@stf92

Use this command (posted by David the H.), it's very simple, and it's impossible for any unusual characters in filenames to cause it to work wrong:

Code:
locate -0 -r ".*mp3" | xargs -0 ls -l
 
Old 07-05-2011, 08:06 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
I'm confused. AIUI, Linux allows any character in pathname components except "/".

That means we can't simply take a list of pathnames generated by find and read it line by line because the names themselves may include line ends (C's "\n". ASCII's LF, 0x0A).

Happily find has the -print0 option which makes it list pathnames separated by null (C's "\0". ASCII's NUL, 0x00) and bash's read has the -r and -d options so we can specify null as a list separator by using -d '' because bash silently adds a null to the end of each string.

So far so good, but nulls are legitimate path component characters so the paths themselves could contain nulls which read -r -d '' would treat as list separators.
 
Old 07-05-2011, 08:37 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Null is illegal in filenames according to the posix specification, along with the backslash.

http://pubs.opengroup.org/onlinepubs...tml#tag_03_170

And while some filesystems will accept null-bytes (and/or slashes) in names, none of the common Linux ones like ext2/3/4 do.

http://en.wikipedia.org/wiki/Comparison_of_file_systems

In addition, the bash shell (and pretty much all shells, probably) doesn't allow null bytes inside of strings, which is why it can safely be used as a delimiter. See the page on "arguments" I gave above.

Last edited by David the H.; 07-05-2011 at 08:56 AM. Reason: updated url & minor rewording
 
1 members found this post helpful.
  


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
gzip: stdin: Input/output error,what does it mean? ayongying Linux - Newbie 11 03-03-2010 12:13 AM
[SOLVED] send signal every time input comes in on stdin crs_zxf Programming 9 11-08-2009 03:58 AM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
How to make extra stdin input in awk ? khaan Programming 3 07-30-2007 05:04 AM
Ctrl+Shift Unicode input gone, after installing Japanese Input Methodes polemon Linux - Newbie 1 09-20-2005 05:17 PM


All times are GMT -5. The time now is 10:51 AM.

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