LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-25-2009, 11:18 PM   #1
Hitboxx
Senior Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562
Blog Entries: 3

Rep: Reputation: 68
maxSIZE of a file.


Hi,

A program from my sister's lab, I'm not much into Unix programming but I tried my best as I know but not getting the desired output. I believe it should return the maximum size of the file in some_dir.

Code:
maxSIZE=`ls -IR $1 | grep ' ^- ' | cut -c 35-42 | sort -n | tail -l`
echo "The maximum size of the file in $1 is $maxSIZE bytes"
OUTPUT:
[user@host]sh 3a.sh some_dir
The maximum size of file in some_dir is bytes.

As you see, it isn't returning the value but just a blank space as bytes. A little help please, thanks.
 
Old 05-26-2009, 02:30 AM   #2
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
Your command uses the -I (capital ai) option of ls instead of -l (lowercase el), hence the output is null due to the cut command which actually has nothing to cut.
 
Old 05-26-2009, 04:57 AM   #3
Hitboxx
Senior Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562

Original Poster
Blog Entries: 3

Rep: Reputation: 68
That too returns the same null. Any other ideas?
 
Old 05-26-2009, 05:09 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hitboxx View Post
That too returns the same null. Any other ideas?
Take the very first part of the command, i.e.

Code:
ls -IR $1
, replace $1 with an actual file (directory ?) name, run the command and copy-paste the results here.
 
Old 05-26-2009, 05:15 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
ls -lSr . |tail -1 |cut -d' ' -f5
 
Old 05-26-2009, 05:30 AM   #6
Hitboxx
Senior Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562

Original Poster
Blog Entries: 3

Rep: Reputation: 68
@Sergei,

Quote:
[jukeboxhero@Mothership ~]$ ls -IR ~/Public/
22 network-error.png network-offline.png network-transmit.png
cod4pc.txt network-idle.png network-receive.png network-transmit-receive.png


[jukeboxhero@Mothership ~]$ ls -lR ~/Public/
/home/jukeboxhero/Public/:
total 32
drwxrwxr-x 2 jukeboxhero jukeboxhero 4096 2009-05-24 08:32 22
-rwxrwxrwx 1 jukeboxhero jukeboxhero 907 2008-07-18 01:34 cod4pc.txt
-rw-r--r--. 1 root root 1270 2009-03-18 08:13 network-error.png
-rw-r--r--. 1 root root 989 2009-03-18 08:13 network-idle.png
-rw-r--r--. 1 root root 1143 2009-03-18 08:13 network-offline.png
-rw-r--r--. 1 root root 1120 2009-03-18 08:13 network-receive.png
-rw-r--r--. 1 root root 1134 2009-03-18 08:13 network-transmit.png
-rw-r--r--. 1 root root 1056 2009-03-18 08:13 network-transmit-receive.png

/home/jukeboxhero/Public/22:
total 28
-rw-r--r--. 1 root root 862 2009-03-18 08:13 network-error.png
-rw-r--r--. 1 root root 672 2009-03-18 08:13 network-idle.png
-rw-r--r--. 1 root root 811 2009-03-18 08:13 network-offline.png
-rw-r--r--. 1 root root 720 2009-03-18 08:13 network-receive.png
-rw-r--r--. 1 root root 720 2009-03-18 08:13 network-transmit.png
-rw-r--r--. 1 root root 689 2009-03-18 08:13 network-transmit-receive.png
-rw-r--r--. 1 root root 1537 2009-03-18 08:13 network-wireless-encrypted.png

[jukeboxhero@Mothership ~]$
 
Old 05-26-2009, 05:40 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hitboxx View Post
@Sergei,

So, you do need to find max file size in just one directory or recursively in that directory and all its subdirectories ?

And another question for you - have you thought about identifying files vs directories ? You listing shows both.
 
Old 05-26-2009, 05:44 AM   #8
Hitboxx
Senior Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562

Original Poster
Blog Entries: 3

Rep: Reputation: 68
I think recursively as -R is implied.

I don't think directories matter for identification, only files should suffice, and I don't know how to manipulate the listing.
 
Old 05-26-2009, 05:50 AM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hitboxx View Post
I think recursively as -R is implied.

I don't think directories matter for identification, only files should suffice, and I don't know how to manipulate the listing.
For starters, try this:

Code:
find ~/Public -type f -exec ls -lt '{}' \;
and publish the result here.

The next suggestion: read 'man cut' and think about the
Code:
' '
delimiter.

Last edited by Sergei Steshenko; 05-26-2009 at 05:52 AM.
 
Old 05-26-2009, 05:58 AM   #10
Hitboxx
Senior Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562

Original Poster
Blog Entries: 3

Rep: Reputation: 68
[jukeboxhero@Mothership b]$ find ~/Public/ -type f -exec ls -lt '{}' \;
Quote:
-rwxrwxrwx 1 jukeboxhero jukeboxhero 907 2008-07-18 01:34 /home/jukeboxhero/Public/cod4pc.txt
-rw-r--r--. 1 root root 1120 2009-03-18 08:13 /home/jukeboxhero/Public/network-receive.png
-rw-r--r--. 1 root root 1143 2009-03-18 08:13 /home/jukeboxhero/Public/network-offline.png
-rw-r--r--. 1 root root 1056 2009-03-18 08:13 /home/jukeboxhero/Public/network-transmit-receive.png
-rw-r--r--. 1 root root 1537 2009-03-18 08:13 /home/jukeboxhero/Public/22/network-wireless-encrypted.png
-rw-r--r--. 1 root root 720 2009-03-18 08:13 /home/jukeboxhero/Public/22/network-receive.png
-rw-r--r--. 1 root root 811 2009-03-18 08:13 /home/jukeboxhero/Public/22/network-offline.png
-rw-r--r--. 1 root root 689 2009-03-18 08:13 /home/jukeboxhero/Public/22/network-transmit-receive.png
-rw-r--r--. 1 root root 862 2009-03-18 08:13 /home/jukeboxhero/Public/22/network-error.png
-rw-r--r--. 1 root root 720 2009-03-18 08:13 /home/jukeboxhero/Public/22/network-transmit.png
-rw-r--r--. 1 root root 672 2009-03-18 08:13 /home/jukeboxhero/Public/22/network-idle.png
-rw-rw-r-- 1 jukeboxhero jukeboxhero 156 2009-05-25 16:30 /home/jukeboxhero/Public/.directory
-rw-r--r--. 1 root root 1270 2009-03-18 08:13 /home/jukeboxhero/Public/network-error.png
-rw-r--r--. 1 root root 1134 2009-03-18 08:13 /home/jukeboxhero/Public/network-transmit.png
-rw-r--r--. 1 root root 989 2009-03-18 08:13 /home/jukeboxhero/Public/network-idle.png
 
Old 05-26-2009, 06:03 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hitboxx View Post
[jukeboxhero@Mothership b]$ find ~/Public/ -type f -exec ls -lt '{}' \;
Nice - now we have just files, and the listing is actually very regular - the same number of columns if we consider ' ' (space) to be the delimiter.

So, the questions:
  1. in what field (column) number do we have size ?
  2. have you read 'man cut' ?

Again, column/field should not be understood in plain text manner, but as number of entity where entities are groups of non-space characters.
 
Old 05-26-2009, 06:11 AM   #12
Hitboxx
Senior Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562

Original Poster
Blog Entries: 3

Rep: Reputation: 68
Yeah read man cut, but didn't get deep enough.

For the size, I think its the 5th column from the left.
 
Old 05-26-2009, 06:15 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hitboxx View Post
Yeah read man cut, but didn't get deep enough.

For the size, I think its the 5th column from the left.
Yes, it's the fifth element, or field in 'cut' terms.

So, your next step is piping the output of already tried 'find' command into 'cut' in order to get the 5-th field, using space as delimiter.

Make na effort, it is easy.

Start from single line piped into 'cut', i.e.

Code:
echo "-rw-r--r--. 1 root root 1134 2009-03-18 08:13 network-transmit.png" | cut YOUR_ARGUMENTS_HERE
- replace YOUR_ARGUMENTS_HERE with the needed arguments based on your understanding of 'man cut'. It is easy. I.e. you basically need the simplest 'cut' usage case.
 
Old 05-26-2009, 06:19 AM   #14
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
Joining the advices from Sergej and Guttorm, with some modifications:
Code:
find ~/Public -type f -print0 | xargs -0 ls -lS | head -1 | cut YOUR_ARGUMENTS_HERE
See man find, man xargs, man ls, man head, man cut for details.

Last edited by colucix; 05-26-2009 at 06:20 AM.
 
Old 05-26-2009, 06:26 AM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by colucix View Post
Joining the advices from Sergej and Guttorm, with some modifications:
Code:
find ~/Public -type f -print0 | xargs -0 ls -lS | head -1 | cut YOUR_ARGUMENTS_HERE
See man find, man xargs, man ls, man head, man cut for details.
And for what an extra level of complexity with 'head -1' ? You will need one more sorting anyway.

And I have tried your command, and it doesn't work.
 
  


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 10:38 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