Last active
January 4, 2016 10:59
-
-
Save marji/8612551 to your computer and use it in GitHub Desktop.
filesizes - prints CSV line for each filepath given - path, size, width, height
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# filesizes, v. 20140125 | |
# Author: marji@morpht.com, http://morpht.com | |
# | |
# Script which reads filenames from the standard output | |
# and prints a CSV in form of filepath, size in bytes, image width, image height | |
# Required package: imagemagick (the identify binary) | |
# http://www.imagemagick.org/script/identify.php | |
# | |
# EXAMPLES | |
# Output info about all files in a directory: | |
# find /srv/www/drupal-7.25/sites/twinkledental.loc/files/ | filesizes | |
# Info about one file: | |
# find pics/my_cat.jpg | filesizes | |
# Info about all jpg in curent directory and below: | |
# find . -iname '*.jpg' | filesizes 2 | |
# | |
# PARAMETERS | |
# The first param is what part of the filepath to output the name, default 1 = whole path | |
# | |
fromdir=${1:-1} | |
if [[ ! $fromdir =~ ^[1-9][0-9]*$ ]] ; then | |
echo "The parameter must be a positive integer, meaning from which part of the filepath to print the name from." | |
exit 1 | |
fi | |
# read the standard output, line by line, for filepaths: | |
while read fname | |
do | |
# skip directories in case they were passed: | |
if [ -d "$fname" ]; then continue; fi | |
# get filesize from the ls command | |
size=$(ls -l "$fname" | awk '{ print $5 }') | |
# get the dimension, only if the file is a selected image type: | |
dim=$(identify -format "%m %w,%h" "$fname" 2>/dev/null | egrep 'ICO |GIF |JPEG |PNG ' | awk '{ print $2 }') | |
# Trim the filename path (fromdir is a parameter of this script): | |
trimname=$(echo "\"$fname\"" | cut -d/ -f${fromdir}- ) | |
# output the line - with or (if not an image) without dimensions: | |
echo -n "${trimname},${size}," | |
if [ ${#dim} -eq 0 ]; then | |
echo ',' | |
else | |
echo $dim | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment