Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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).
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.