Categories
Linux shell

Create Thumbnails from a bunch of images

Linux Code snippet. Ever wated to batch create thumbnails? “musclecarr” posted this bash script to create thumbnails of all images of a specific type. It uses imagemagick and specifically the convert command. minor modifications done to it and comments added for easy understanding, here it is :

#!/bin/bash
# take argument or use current dir
DIR=${1:-`pwd`}
# create dir thumbs if doesnt exist
[ -d "$DIR/thumbs" ] || mkdir "$DIR/thumbs"
# error out if cant enter dir for some reason (permissions?)
cd "$DIR" || { echo >&2 "error: couldn't enter directory: $DIR"; exit 1;}
# For every file with extension XXX (jpg in this case)
for i in ./*.jpg; do
# Resize image to 100x100
convert "$i" -resize 100x100 "$DIR/thumbs/$i"
# profit? nah.
done
Categories
Linux shell

recursively delete all empty subdirectories

find /home/user/somedir -depth -type d -empty -exec rmdir -v {} \;
Caution
Caution!

This CAN and WILL destroy all your data and eat your small { dog | cat | turtle | parrot } if you aren’t careful! ALWAYS check the path twice and then check it again before using.

Categories
Linux shell

Directory FileCount

I have close to 3000 images in a folder and needed to know exactly how many for a stupid bet I made. I dont feel like counting them! there must be something in the linux world to make this easier…. well there is. simple extension of the ls command.

my directory is called images but I’m sure contains no images whatsoever. *cough* so lets get the basics :

ls -1 images | wc -l

what if you wanted to not count sub directories or symlinks?

find images -maxdepth 1 -type f | wc -l

want to add symlinks?

find images -maxdepth 1 -type f -follow | wc -l

so yeah the basics should be done ūüôā

Categories
Linux

bash file listing with markup

every now and again we need to take a bunch of file names from a folder and output then into a single file. sometimes with markup around them. Well there’s always ls.

ls > test.txt

this nets you the following in a file:

0.png
1.png
blockcontent_h.png
blockcontent_s.png
blockcontent_v.png
blockheader.png

what if you want markup around the file names? like a list of all images in a folder with html tags around it. well things get slightly more involved. say you have a bunch of png files and want them listed in an html file with img tag and span tags so you can add css classes to it.

for i in *.png; do echo "\t\r\t\t<img src="$i" alt="" />\r\t"; done > images.html
Categories
Linux

Archive & Compression utilities

Compression types and utils in Linux

There are many compression utilities available to the average users on a linux system, so which one is best? well it all depends on what your intended use/goal for the file(s) is. Are you planning on sending the file to other OS’s? are you planning on decompressing the file on a new system where you might not have the same tools available? etc etc. I am writing about some of the ones I have used, and since this is from my usage some of it may not be entirely true and I may have dreamed up during my hallucinatory state while attending classes.

Zip

The Most popular tool, archives and provides basic compression. Works on most OS’s popularly used today.

  • zip <filename> <files to compress> ¬† ¬† ¬†—– zip files into one archive
  • zip mydocs.zip doc1 doc2 doc3 doc4
  • unzip <filename>¬†¬†¬† ¬† ¬†—– unzip files from an archive into their proper paths under current directory.
  • unzip -j <filename>¬†¬† ¬† ¬†—– unzip files from the archive into current directory not following directory structure.

Tar

Archive only, no compression applied by default. usually found with .tar extension

  • tar -cf ¬†¬†¬† ¬† ¬†—– tar multiple files into one archive with no compression (see bzip2 / gzip for compression)
  • tar -xf <filename>¬† ¬† ¬† —– extract files from a tarball

Rar

Proprietary but with support for almost all popular platforms, decent compression + archival. usually found with .rar extension

  • rar a <filename>¬†<files to compress>¬† ¬† ¬† —– archive + compress
  • unrar x <filename>¬†¬† ¬† ¬†—– extract files using full paths

Bzip2

What many people switch to once they learn of it. Compression only so singular file at a time, very good compression, use in conjunction with tar in order to get multiple files into one tarball. use the -j flag in gnu tar to provide bzip2 support. usually found with .bz2 extension.

  • tar -xjf ¬† – used to extract files with .bzip2 compression.
  • tar cjf ¬† – used to compress n¬†files into a tarball with bzip2 compression.
  • bzip2 -z9 ¬† ¬†—- compress a single file using bzip2 itself, use any number instead of 9 to lower the compression from 1 – 9, 9 being best 1 being fastest.
  • bzip2 -d ¬† ¬†—- decompress a file packed using bzip2. if the file is also¬†tar balled¬†it will still stay archived but just not be compressed anymore.

Gzip

Comes integrated into gnu tar by default with the -z switch, pretty good compression. all gnu systems should be default have support for it I believe. usually found with .gz extension, if used by itself, .tar.gz, .tar.gzip or just .gz if used with tar.

  • tar -xzf <filename> ¬† – used to extract files with .gz compression.
  • tar -czf ¬† ¬†—- used to compress¬†n¬†files into a tarball with gzip compression.
  • gzip -z9 ¬† ¬†—- compress a single file using gzip itself, use any number instead of 9 to lower the compression from 1 – 9, 9 being best 1 being fastest.
  • gzip -d ¬† ¬†—- decompress a file packed using gzip. if the file is also¬†tar balled¬†it will still stay archived but just not be compressed anymore.

lzma

great compression potential, can be used byitself or with tar using the –lzma switch

  • tar -xf –lzma ¬† – used to extract files with .gz compression.
  • tar -cf –lzma ¬† ¬†—- used to compress¬†n¬†files into a tarball with gzip compression.
  • gzip -z9 ¬† ¬†—- compress a single file using lz,a itself, use any number instead of 9 to lower the compression from 1 – 9, 9 being best 1 being fastest.
  • gzip -d ¬† ¬†—- decompress a file packed using lzma. if the file is also¬†tar balled¬†it will still stay archived but just not be compressed anymore.

xz

uses lzma2 as its compression and provides the xz container for archival. single file only. best compression out of all the tools mentioned here in my opinion. tested on a 1091MB text file using -9 compression and it brought it down to 106MB. If your system has xz utils installed then using the -J / –lzma / –xz switch will pass thru xz¬†instead¬†of lzma itself.

  • tar -xf –xz ¬† – used to extract files with .gz compression.
  • tar -cf –xz ¬† ¬†—- used to compress¬†n¬†files into a tarball with gzip compression.
  • xz -z9 ¬† ¬†—- compress a single file using xz itself, use any number instead of 9 to lower the compression from 1 – 9, 9 being best 1 being fastest.
  • xz -d ¬† ¬†—- decompress a file packed using xz. if the file is also¬†tar balled¬†it will still stay archived but just not be compressed anymore.

My pick is xz as of last month when I started using it for practically all my compression needs. best results out of all of the above for me. let me know if anyone has any questions.

This post is mostly from memory/conjecture and some from man pages, ¬†so if¬†there are¬†any errors please let me know and I will fix ūüôā

Categories
hardware

Cable pronz? A very neatly done Datacenter

[et_pb_section admin_label=”Section” fullwidth=”on” specialty=”off”][et_pb_fullwidth_post_title admin_label=”Fullwidth Post Title” title=”on” meta=”on” author=”off” date=”on” categories=”on” comments=”off” featured_image=”on” featured_placement=”background” parallax_effect=”off” parallax_method=”on” text_orientation=”center” text_color=”dark” text_background=”on” text_bg_color=”rgba(255,255,255,0.9)” module_bg_color=”rgba(255,255,255,0)” title_all_caps=”off” use_border_color=”off” border_color=”#ffffff” border_style=”solid”] [/et_pb_fullwidth_post_title][/et_pb_section][et_pb_section admin_label=”section”][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_gallery admin_label=”Gallery” gallery_ids=”46,45,44,43,42,41,40,39,38″ gallery_orderby=”rand” fullwidth=”on” show_title_and_caption=”on” show_pagination=”on” background_layout=”light” auto=”on” auto_speed=”7451″ hover_overlay_color=”rgba(255,255,255,0.9)” caption_all_caps=”off” use_border_color=”off” border_color=”#ffffff” border_style=”solid” /][/et_pb_column][/et_pb_row][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”bottom above footer” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[/et_pb_text][et_pb_divider admin_label=”Divider” color=”#ffffff” show_divider=”off” divider_style=”solid” divider_position=”top” hide_on_mobile=”on”] [/et_pb_divider][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”] [jetpack-related-posts] [/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]