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
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
05-25-2009, 11:18 PM
#1
Senior Member
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562
Rep:
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.
05-26-2009, 02:30 AM
#2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
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.
05-26-2009, 04:57 AM
#3
Senior Member
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562
Original Poster
Rep:
That too returns the same null. Any other ideas?
05-26-2009, 05:09 AM
#4
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
Hitboxx
That too returns the same null. Any other ideas?
Take the very first part of the command, i.e.
, replace $1 with an actual file (directory ?) name, run the command and copy-paste the results here.
05-26-2009, 05:15 AM
#5
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453
ls -lSr . |tail -1 |cut -d' ' -f5
05-26-2009, 05:30 AM
#6
Senior Member
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562
Original Poster
Rep:
@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 ~]$
05-26-2009, 05:40 AM
#7
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
Hitboxx
@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.
05-26-2009, 05:44 AM
#8
Senior Member
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562
Original Poster
Rep:
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.
05-26-2009, 05:50 AM
#9
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
Hitboxx
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
delimiter.
Last edited by Sergei Steshenko; 05-26-2009 at 05:52 AM .
05-26-2009, 05:58 AM
#10
Senior Member
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562
Original Poster
Rep:
[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
05-26-2009, 06:03 AM
#11
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
Hitboxx
[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:
in what field (column) number do we have size ?
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.
05-26-2009, 06:11 AM
#12
Senior Member
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 1,562
Original Poster
Rep:
Yeah read man cut, but didn't get deep enough.
For the size, I think its the 5th column from the left.
05-26-2009, 06:15 AM
#13
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
Hitboxx
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.
05-26-2009, 06:19 AM
#14
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
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 .
05-26-2009, 06:26 AM
#15
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
colucix
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.
All times are GMT -5. The time now is 10:38 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News