LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2006, 06:50 PM   #16
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49

Quote:
Originally Posted by Hko
Right.
I missed that part.
I should have read your post less quickly.
whooo!! That's so much to learn. Thank you guys.

Truly said "When you work with linux, everyday you learn something new"...
 
Old 01-26-2006, 06:56 PM   #17
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
Quote:
Originally Posted by Hko
Right.
I missed that part.
Nuh, it's my fault ... I never said what to call it :}


Quote:
Originally Posted by Hko
Right.
I should have read your post less quickly.
And I sure am guilty of that one too often! ;D

Cheers,
Tink
 
Old 01-26-2006, 07:12 PM   #18
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by Hko
They are not OK. Here's why:
Code:
find /home/user/test -type f
This will will search for any file called "/home/user/test". There will be only one file found: /home/user/test.

So you could expect the the file containing the list of file ("list.txt") would be truncate to zero-length! But you were lucky this did not happen, because of another error:

When redirecting to {} , the -exec option does not replace {} with the files found by "find". So /dev/null is catted to a file called "{}". I bet you have a an empty file now in your directory called "{}".

Code:
xargs -i cat /dev/null > {}
The same problem here with redirecting to {}.

Also, "xargs" will put as many arguments from its standard input (stdin) as possible. So the command actually executed will be something like:
Code:
cat /dev/null > file1 file2 file3 file4
...which isn't what you were expecting.
Sorry to ask the question again, actually, I noticed something in Hko's reply which I didn;t before.

Hko, you are right I do see file '{}' in my directory.

But, then why does the command
Code:
find ./ -type f -exec less {} \;
works fine. Also in man find, they actually do say that {} is replaced by the name of the files found in the directory from which you run the find command.

???

Last edited by kushalkoolwal; 01-26-2006 at 07:28 PM.
 
Old 01-26-2006, 07:36 PM   #19
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by kushalkoolwal
But, then why does the command
Code:
find ./ -type f -exec less {} \;
works fine. Also in man find, they actually do say that {} is replaced by the name of the files found in the directory from which you run the find command.
This is because of a limit of the "-exec" option of "find": "find" doesn't do the replacement after a '>'. The redirecting to a file is done before replacing '{}'.

So you could do:
Code:
find ./ -type f -exec cat {} > /dev/null \;
# '{}' before '>'
But not:
Code:
find ./ -type f -exec cat /dev/null > {} \;
# '{}' after '>'
There may be a "hack" to do it work around it. But I don't know off any.

Last edited by Hko; 01-26-2006 at 07:39 PM.
 
Old 01-26-2006, 07:50 PM   #20
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
I don't think it matters where the redirect is in the command. It's being applied for the 'find' not the command in the exec statement.

The easiest way around this is to dump the exec stuff into a sub-shell. For example:

Code:
find . -type f -exec sh -c 'cat /dev/null > {}' \;
That works on my system anyways...
 
Old 01-27-2006, 04:17 AM   #21
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by gilead
I don't think it matters where the redirect is in the command. It's being applied for the 'find' not the command in the exec statement.
Yes. That does make sense.
Thanks for clearing that up.
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
C: fread to read a file line by line until the end Blue_muppet Programming 2 09-19-2008 09:42 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 05:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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