LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 05-26-2009, 06:31 AM   #16
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

Quote:
Originally Posted by Sergei Steshenko View Post
And for what an extra level of complexity with 'head -1' ? You will need one more sorting anyway.
Since ls -lS sorts files by size, head -1 returns the bigger one. That's it. Where do you see such a complexity?
 
Old 05-26-2009, 06:36 AM   #17
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by colucix View Post
Since ls -lS sorts files by size, head -1 returns the bigger one. That's it. Where do you see such a complexity?
When I copy-paste your command (searching from ~), without the 'cut' part, I get many lines, not just one line.

This is what 'man xargs' says in the beginning:



Code:
This manual page documents the GNU version of xargs.  xargs reads items from the standard input, delimited by blanks (which can be protected with double or single
       quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from stan-
       dard input.  Blank lines on the standard input are ignored.
- already invitation for trouble.
 
Old 05-26-2009, 07:14 AM   #18
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
By the way,

Code:
stat --printf=%s FILE;echo ""
prints just file size.
 
Old 05-26-2009, 07:34 AM   #19
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
Quote:
Originally Posted by Sergei Steshenko View Post
When I copy-paste your command (searching from ~), without the 'cut' part, I get many lines, not just one line.
That's really odd. Whatever the output is, the head -1 should give just one line. It works on my system. Anyway, as usual, there are many ways to do the same task in linux.
 
Old 05-26-2009, 07:41 AM   #20
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by colucix View Post
That's really odd. Whatever the output is, the head -1 should give just one line. It works on my system. Anyway, as usual, there are many ways to do the same task in linux.
Try the command without 'head' in a filesystem with a lot of files and watch output - you'll see it doesn't do what it should - my point is:


Code:
find /usr -type f -print0 | xargs -0 ls -lS | head -10000 | less
yields, among other things, this (seen from inside 'less' with -N on):

Code:
   5894 -rw-r--r-- 1 root root        3 2007-09-26 16:31 /usr/lib/YaST2/trans/sv.status
   5895 -rw-r--r-- 1 root root        3 2007-09-26 16:31 /usr/lib/YaST2/trans/zu.status
   5896 -rw-r--r-- 1 root root 40073986 2009-03-27 19:57 /usr/lib/jvm/java-1.5.0-sun-1.5.0/jre/lib/rt.jar
   5897 -rwxr-xr-x 1 root root 12832612 2009-04-20 21:13 /usr/lib/xulrunner-1.9.0.9/libxul.so
, i.e. overall you do not the longest file on top, so 'head- 1' won't bring you the needed file.
 
Old 05-26-2009, 07:42 AM   #21
Hitboxx
Senior Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562

Original Poster
Blog Entries: 3

Rep: Reputation: 68
Hey, I got the output.

maxSIZE=`ls -lR $1 | grep '^-' | cut -c 32-45| sort -n | tail -1`

>The maximum size of the file in /home/jukeboxhero/Public/ is 1270 20 bytes

Changed the I to l in ls, removed the spaces in grep and replaced l with 1 in tail.

Thanks for all the help.
 
Old 05-26-2009, 07:46 AM   #22
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hitboxx View Post
Hey, I got the output.

maxSIZE=`ls -lR $1 | grep '^-' | cut -c 32-45| sort -n | tail -1`

>The maximum size of the file in /home/jukeboxhero/Public/ is 1270 20 bytes

Changed the I to l in ls, removed the spaces in grep and replaced l with 1 in tail.

Thanks for all the help.
'ls' is a bad idea, and "cut -c 32-45" is absolutely horrible and wrong.

And the output you gave us proves that you can't rely on character positions which you do in "cut -c 32-45".

So, generally speaking, you've got a wrong output.

But 'cut' does allow you to have the right output, and with 'stat' you wouldn't even need 'cut'.

Just look at your "1270 20 bytes" and tell us about the space.

Last edited by Sergei Steshenko; 05-26-2009 at 08:08 AM.
 
Old 05-26-2009, 08:06 AM   #23
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
Quote:
Originally Posted by Sergei Steshenko View Post
, i.e. overall you do not the longest file on top, so 'head- 1' won't bring you the needed file.
You're right. This is because the standard input is buffered through the pipe, so xargs receive a certain number of file names at a time and execute the command. I stand corrected.
 
  


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
wget - how to download more than one file at once instead of file after file? De-M-oN Linux - Server 15 09-24-2010 05:35 PM
Display by File Name, File Size, and File Owner using ls akeenabawa Linux - Newbie 9 08-15-2008 02:21 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
gave wrong syntax for tar as tar -cvzf file file.tgz how to recover the file gautham Linux - General 4 04-13-2005 03:15 AM
How to play a media file/ video file/mp3 file recorded in harddisk/cd-rom arindam Linux - Newbie 2 09-05-2003 10:31 AM

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

All times are GMT -5. The time now is 09:21 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