LinuxQuestions.org
Help answer threads with 0 replies.
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 08-19-2011, 02:39 AM   #1
cober
LQ Newbie
 
Registered: Feb 2009
Distribution: Debian 5.0
Posts: 14

Rep: Reputation: 0
Bash if filesize is greater than 2GB


Hello,

I have some problems with a bash script...

Here it is:
Code:
for FILENAME in /home/cober/downloads/*/*

    do
    FILESIZE=$(stat -c%s "$FILENAME")

        if [[ $FILESIZE > 2050000000 ]] ;then
        echo "$FILENAME is too large = $FILESIZE bytes."
        fi

    done

exit 0
I just want the output of the files which are greater than 2GB, but I get this right now..

Code:
/home/cober/downloads/x1/1.wmv is too large = 3555295269 bytes.
/home/cober/downloads/x2/2.wmv is too large = 2901575187 bytes.
/home/cober/downloads/x3/3.wmv is too large = 2580265584 bytes.
/home/cober/downloads/x4/4.mp4 is too large = 651414946 bytes.
/home/cober/downloads/x5/5.mp4 is too large = 783791599 bytes.
/home/cober/downloads/x6/6.mp4 is too large = 838259812 bytes.
As you see some info is incorrect... Don't know why am I getting the notice that 4.mp4, 5.mp4 and 6.mp4 are larger than 2050000000 bytes.

Anyway, how to deal with this?

Plus, how could I make that those files which are bigger than 2GB take an appropriate action (let say, to make a rar) - rar a -v2048000000b -m0 -r $file

Thanks for your help!
 
Old 08-19-2011, 03:01 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
In bash the integer comparison is performed by the -eq, -ne, -gt, -lt, ... operators. If you use the =, >, < notation it will perform a string comparison. Change the operator > to -gt in the IF statement and the trick is done.

Regarding the action, you can just put the command inside the IF statement using the loop variable FILENAME in the command line (in place of $file in your rar statement).
 
1 members found this post helpful.
Old 08-19-2011, 03:56 AM   #3
cober
LQ Newbie
 
Registered: Feb 2009
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
colucix, thank you very much! -gt did the trick.

about an action, i just found out that I have to go (cd) in every single dir and then perform this action with rar, otherwise it won't pack
as it should. rar has "w<path> Assign work directory" option that could fix my problem, but don't know how to integrate into if statement.

So, as we said, these 3 are above 2GB

Code:
/home/cober/downloads/x1/1.wmv is too large = 3555295269 bytes.
/home/cober/downloads/x2/2.wmv is too large = 2901575187 bytes.
/home/cober/downloads/x3/3.wmv is too large = 2580265584 bytes.
Now I have to "enter" workdir (x1, x2 and x3) and then I'll be able to perform rar action. Any clues on that?

Much appreciated!
 
Old 08-19-2011, 05:37 AM   #4
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
Yes. Once you have the full path inside the loop (as in your example) stored in the variable FILENAME, you can either use parameter substitution to strip the filename from the end of the string:
Code:
cd ${FILENAME%/*}
or use the dirname command:
Code:
cd $(dirname $FILENAME)
Hope this helps.
 
1 members found this post helpful.
Old 08-19-2011, 08:26 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You can also do this with find:
Code:
find /home/cober/downloads/*/ -maxdepth 1 -size +2G | while read file; do
  echo "$file is too large = $(stat -c%s "$file") bytes."
done
Kevin Barry

Last edited by ta0kira; 08-19-2011 at 08:32 AM.
 
1 members found this post helpful.
Old 08-19-2011, 01:34 PM   #6
cober
LQ Newbie
 
Registered: Feb 2009
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks! Now just works perfect!
 
Old 08-19-2011, 10:14 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You should use ((..)) for numeric tests anyway. The <> characters are treated as integer comparators when used in an arithmetic context.

Code:
if (( FILESIZE > 2050000000 )) ;then
     echo "$FILENAME is too large = $FILESIZE bytes."
fi
http://mywiki.wooledge.org/ArithmeticExpression
http://www.tldp.org/LDP/abs/html/comparison-ops.html

Last edited by David the H.; 08-19-2011 at 10:20 PM. Reason: added link
 
1 members found this post helpful.
Old 08-20-2011, 12:35 AM   #8
cober
LQ Newbie
 
Registered: Feb 2009
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
David, thanks for that explanation.

Just last question regarding bash.. let say that I want upload some files with various extensions through ftp via command line.

It goes just like this:
Code:
ncftpput -u $username -p $password $host / /*.mp4
Ok, how could I make this command correct to upload some files with different extensions like .mp4, .wmv, .avi etc... in one command.

Thanks guys for helping me out.

EDIT: i have also this tiny script to get direct urls of the movies on my server

Code:
for i in `ls -1 */* | grep jpg`; do echo http://domain.com/$i; done
but instead of */*, i'd like to specify this movie extensions to avoid any non-movie file around there. (*/*.mp4, */*.avi ... - in one command)

Last edited by cober; 08-20-2011 at 12:49 AM.
 
Old 08-21-2011, 05:31 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quite detailed filtering can be done with globbing, particularly with bash's extended glob patterns.

http://mywiki.wooledge.org/glob
http://www.linuxjournal.com/content/...ended-globbing

Note also though that globbing patterns are expanded into lists of files before the command itself is executed, so you should be able to simply put two or more separate globs in the same command.

Code:
ncftpput -u $username -p $password $host / /*.mp4 /*.wmv /*.avi
The actual command when executed will look something like this:

Code:
ncftpput -u username -p password host / /file1.mp4 /file2.mp4 /file1.wmv /file2.wmv /file1.avi /file2.avi
Assuming those files exist, of course. Stick an echo in front of the command to view what bash would actually run. See the nullglob shell option for what to do if a glob fails to match anything.

For more advanced matches, you'll generally want to use find.

http://mywiki.wooledge.org/UsingFind

Always use the -print0 option whenever possible, to avoid problems with spaces and special characters in names. But the receiving program has to have the ability to handle the null-delimited file list, like xargs.

There are lots of previous discussions on filename matching here and on the web. Try searching around a little.


As for this one:
Code:
for i in `ls -1 */* | grep jpg`; do echo http://domain.com/$i; done
1) $(..) is highly recommended over `..`

2) Parsing ls is both unsafe and generally unnecessary. The for loop will accept glob inputs directly, and without word splitting.

3) If globbing isn't up to the job, then use find, either alone with -exec, or run the output via process substitution into a while+read loop, instead of a for.

Last edited by David the H.; 08-21-2011 at 05:35 AM.
 
1 members found this post helpful.
  


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
Samba filesize limit 2Gb slideh Linux - Newbie 12 07-09-2008 01:44 PM
Samba filesize limit 2Gb slideh Linux - Newbie 3 07-09-2008 01:19 PM
xfsdump max filesize 2GB Crazy_lenny Linux - Kernel 2 09-13-2006 05:31 AM
Slackware-10.0 DVD ISO d/l greater than filesize Bruce Hill Slackware 7 02-12-2005 06:15 PM

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

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