ProgrammingThis 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.
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.
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
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).
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?
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:
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.