I was trying to figure out how to write a shell script to be able to print the name of the file and size to a text file I wanted to know if I was on the right track and am understanding this better.
In my script a for loop be able to print files over 4mb (we charge differently for larger images)
image="/Home/Desktop/images"
(cd "$image"
for f in *.jpg; do
let fsize=$(stat -c %s "$f")
if (( fsize == 4mb )); then
echo "$f"
fi
done) > mnt/desktop/sizes/results.txt
my understanding:
check variable $image that is being set from the directory
f is all images from *
do f's size and make a variable inside $()
stat -c %s prints the file name?
if condition is met for fsize equals 4mb size
print variable $f
to file results.txt
image="/Home/Desktop/images"
{
cd "$image"
for f in *.jpg
do
let fsize=$(stat -c %s "$f")
if (( fsize > 4194304 ))
then
echo "$f"
fi
done
} > mnt/desktop/sizes/results.txt
I extended my code to check the directory but now my issue is if try to echo $f it gives me the full path and the filename of the image. How can I just get the name to also print next to the directory??
echo 'checking images sizes'
{
for f in $(find $DIRECTORY/$directoryname -name \*.jpg);
do
let fsize=$(stat -c %s "$f");
if (( fsize > 2097152 ))
then
echo "$directoryname $f"
fi
done
} >> results.txt
EDIT:
and how do you do an && in bash so I can check for .jpeg and .jpg
thanks just finally got it after 24 hours. I needed basename
final script example:
echo 'checking images sizes'
{
for f in $(find $DIRECTORY/$directoryname -name "*.jpg" -o -name "*.jpeg");
do
let fsize=$(stat -c %s "$f");
if (( fsize > 2097152 ))
then
images=$(basename $f)
echo "The following images need reviewing"
echo "Location: $directoryname"
echo "Images: $images"
fi
done
} >> results.doc
EDIT:
I was trying to figure out how to sed or use --xpath before echo "the following" and access an .xml file that has a tag <name></name> but Im still stuck there and if I can figure out the way to pull data from xml files it would really help me out.
If that doesn't work, you'll have to do as asked and post the XML, because a trivial answer often isn't good enough. XML can take many different forms.
my apologies if Im being complicated Im still trying to learn and understand whats going on to help me better. Can you explain whats going on or tell me where I can learn what you did?
awk is a programming language found on any UNIX system handy for processing text files. It is handy like grep, powerful like sed, and has actual variables and statements, but not as unwieldy as perl. A full description of it would be out of scope here.
match() is a function that returns a positive number if the regular expression is found inside the line( i.e. $0 ). If it returns anything positive, then the code block following it inside { } is executed.
RSTART and RLENGTH are variables that tell where the string is found, match() sets them.
substr() is used to extract the string. substr($0, RSTART, RLENGTH) would extract the whole thing, just like egrep. Adding to RSTART and subtracting from RLENGTH excludes the tag portion.
Lastly, print prints.
Here's an old post of mine if you want to explore awk more in depth.