How to write a script that takes two arguments, lower bound and upper bound and displays the file sizes that are in that specific range?
This script has to be done WITHOUT using the command find. I believe I have to use the command awk and ls-l combined. This is what I have so far, but it doesn't seem to be working. I am a newbie and trying to figure this out. If anyone can help me it would be greatly appreciated.
ls -l|awk '{print $1, $2}' |
Quote:
use -v awk option for passing external arguments, then compare ls size field (not $1 or $2...) with bounds arguments |
Ok thanks for the help. Let me see if I can figure this out.
|
So yes, awk is the tool.
Looks like there is plenty for you to learn - start with "field splitting". Also to save some frustration, you can hard-code the limits in your code to get the hang of it. Learn about -v later when you've got something to work with. |
Is the piping okay though? Or it's not necessary?
|
You have to get data to awk somehow - piping a command output to it is very common, and fine in this case.
|
I'm still having trouble with this script. I did figure out by myself how to display the file size and name of the file by using:
ls -l|awk '{print $5 "\t" $9}' Now I just need to take user input for the range. For example if I run the script ./between 80 120 it should show me the files that are between 80 and 120 in size and print out only these files with their corresponding file size and name. I'm stuck. I'm not asking for the answer but could someone please point me in the right direction or maybe a link or post that talks about how to do this? It would be greatly appreciated. This is my second attempt: read $1 read $2 ls -l|awk -v '/$1/, /$2/ {print $5 "\t" $9}' |
Not being a big fan of parsing ls output due to issues that may arise (specifically that $9 is not always the name field and if the name contains white space you will return only part of the name), why not just use a simple for loop with globbing and the stat command.
|
Quote:
Code:
-v var=val |
Because file names are in the last column in the ls -l output, the read command will preserve spaces in it; a read loop makes sense.
Code:
#!/bin/sh Code:
./between 80 120 Note that this script is still not universal: there might be locales where the time is in two fields (so fname becomes field #8). |
Quote:
|
Another +1 for grail's hint of the stat command. I have never really used it this way before, and since we already have a solution up there here is my attempt, using globbing and stat. All credit to grail though.
Code:
#!/bin/bash HMW |
There is a glitch: if there is something that is not a file then the size of the previous file is reported. Better is the method that was given in the link:
Code:
# Loop with glob and use stat to get filesize. |
Quote:
Best regards, HMW |
@HMW - actually with your's you could just place the second if inside the first :)
|
All times are GMT -5. The time now is 11:25 AM. |