Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Thursday, August 5, 2010

How to resize photos with Bash shell script

Ivo Kaspar wrote this guide to resize multiple images using simple Bash shell script. It uses the convert program from ImageMagick, the swiss-army knife of command line image processing software. The original guide is edited and rewritten in some places.

You'll find this guide interesting if you want to resize a lot of pictures to upload to your website or email them. First you need to install the 'imagemagick' package if it is not installed on your computer. To install imagemagick, copy this line into your terminal application and confirm with your password. Close the terminal after it finished the installation. sudo apt-get install imagemagick

Open the text editor and paste these lines into it:


#!/bin/bash
for i in *.JPG; do convert $i -resize 10% $(basename $i .JPG).jpg; done

Save the document and give it a sensible name to recognize it later on.

Note that this script will work only with files that has uppercase '.JPG' extension and renames them to lower case '.jpg'. Some digital cameras follow this kind of file naming schema. You may like to change this to suit your needs.According to its manual page ImageMagick can read, convert and write images in a variety of formats (about 100) including GIF, JPEG, JPEG-2000, PNG, PDF, PhotoCD, TIFF and DPX.

If you want to define the size of the picture replace the "10%" with the amount of pixels you want it to have, 400x600 is a usable resolution. Of course it is also possible to keep the relative scale with any value instead of the “10” followed by "%" postfix. To change the file format to something other than “.JPG” you have to change the last “.JPG” into the format of your choice.

If you want to rename the pictures, you have to add something behind the "(basename $i .JPG)" and the ".JPG". I usually add an "­-s" (for small) to indicate the reduced size of the pictures.

When you run the script it will proceed to resize all the pictures in the current folder, but not in any sub folders. Keep in mind that:

  • Small pictures are small forever, you can't revert them.
  • The filenames must not contain any space
  • The filename must match to the first two “.JPG” or “.jpg”

Examples Scripts:


# capital .JPG files, 10% size, no rename, it will overwrite the old
pictures
#!/bin/bash
for i in *.JPG; do convert $i -resize 10% $(basename $i .JPG).JPG; done
---
# lower case .jpg files, 10% size, renamed to capital .JPG (with ubuntu it's
a different file, not with windows!)
#!/bin/bash
for i in *.jpg; do convert $i -resize 10% $(basename $i .jpg ).JPG; done
---
# capital .JPG files, 10% size, adds an -s in the name of the new ones,
keeps the old ones
#!/bin/bash
for i in *.JPG; do convert $i -resize 10% $(basename $i .JPG)-s.JPG; done
---
# capital .JPG files, 600x400 size, no rename, it will overwrite the old
pictures
#!/bin/bash
for i in *.JPG; do convert $i -resize 600x400 $(basename $i .JPG).JPG; done

Saturday, December 6, 2008

Start X Painlessly

Perhaps I should start this post in V. S. Naipaul style with a dedication 'To Sajith for asking me why' ;). Watching over my shoulder while I was working on my laptop, he asking me why I used this particular command to start the X Window server.

$ openvt startx

As command line junkie I work on linux console most of the time, only starting graphical X server with startx command when needed. The problem with using startx command is that it locks up the terminal by printing numerous warnings and messages while the X server starts and thus making the terminal unusable. Also I limit the number of virtual terminal on my computer to just two instead of standard six on gnu/Linux operation system.

So, the openvt command provided by the console-tools package helps me spawn a new virtual terminal and that's where all the warning and messages are printed leaving me to continue working in peace. Another added advantage to this is that I can do startx from a Emacs shell instance too, this is neat!.

Wednesday, November 12, 2008

Bash Path Security Gotcha!

The devil they say is in details, even the simple Bash path variable can open some security problems on your box. Every Bash documentation out there warns us not leave '.' in the path (it matches current directory). They however don't seems to mention that leaving a dangling ':' (colon) at the beginning or the end of Bash $PATH does create the same security vulnerability.

# Standard Bash $PATH $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games # Let's get evil, append a colon at the end $ export PATH="$PATH:" # Let's check the tainted Bash $PATH $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: # Create Trojan application (of sorts) $ echo "echo foo"> app $ chmod +x app # Run the command and Viola! $ app foo

Monday, November 12, 2007

Stopping Bittorrent client after finishing downloads

Leaving your bittorrent client running after finishing download is a generally a good idea. However for sake of geek curiosity I tried a simple method of stopping my transmission bittorrent client after finishing the download with ....

/usr/bin/transmission-cli -f 'killall transmission-cli' elephantsdream-1920-hd-mpeg4-su-ac3.avi.torrent

The -f, --finish <shell script> is used to launch shell scripts. The 'killall transmission-cli' command kills (all) transmission-cli bittorrent clients running on this computer. It seems to work for me, do share your experiences

Wednesday, November 7, 2007

Changes in Debian Transmission package (transmissioncli renamed)

I just noticed that the light-weight bittorrent client Transmission package on GNU/Debian Linux has renamed its CLI(Command Line Interface) program name from transmissioncli to transmission-cli .

Here is note from transmission (0.90.dfsg-1) package changelog.
* debian/transmission-cli.*: binary upstream rename from transmissioncli to transmission-cli (Leo "costela" Antunes)

I have updated my old post Using Transmission Bittorrent client on console to suit this.

Tuesday, October 30, 2007

Running automated package updates with Cron

LinuxLove.org's recent post about automatically running apt-get upgrades daily with cron using a simple bash script can be easily done using Cron-Apt package itself.

Please do note that such automated package upgrades in apt based distro's is a very bad idea. Joe had rewritten a detailed account about the problems and security issues involved in Debian-Administration.org few years back.

By default, cron-apt will only download updates -- it will not install them. I know other packages for other distributions like up2date will automatically install the updates for you, but I've learned to like this and that in the long run, automatically installing updates is a Bad Thing. Why? Well when I was experimenting with auto-installs, I ran into problems:

  • Even though I told apt to say Yes to everything, it still prompted me for input on config file conflicts (was I doing something wrong?). This caused the script to hang indefinitely in the background until it was killed.
  • Auto-accepting configuration changes can ruin your day. With Debian, it's fairly easy to back out of a change and hopefully you have a backup copy of the config file, but that's not the point. Good System Administration means you should never be in that position in the first place. "OK, what if I just deny all configuration changes and leave my copy?", you ask. Well, what if the upgraded program uses new syntax in its config file. Your program is now broke and you're in the exact opposite position as you were with auto-accepting.
  • Just as you should always review configuration file changes, always review what packages are being installed. If you know that installing that new version of Samba will mean some extra work in other places, don't install it yet.

Bottom line: Yes, it can be very tedious to manually review each update batch -- especially if you have several servers -- but that is part of your job when you are running a server. Deal with it!

cowsay: a configurable talking and thinking cow

Today's Debian Package of the day Cowsay reminds me of a dull day few years back when I wrote this 'cowwords' script to amuse myself.Even a useless tool like cowsay can be interesting with creative use, CrunchBang has another cool hack using vrms and cowssay Happy Hacking! :) .

Cowsay is a useless but very fun text filter written in Perl. If you send some text into cowsay, you get an ASCII cow saying your text. For example, cowsay Hello, World! prints


_______________
< Hello, World! >
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


Script in public domain originally posted on FSUG-Bangalore Mailing-list

#!/bin/sh # # SCRIPT: cow-words.sh # AUTHOR: Rakesh 'arky' Ambati (http://www.linuxgazette.com/blog/4864) # DATE: 17 January 2005 # VERSION: you_must_be_kidding # # CREDITS: I Thank Karma_Police for the 'cow file randomizer code' # # PURPOSE: Retrieves the Dictionary.com 'Word of the Day" RSS Feed and # a random character teaches them. # # DEPENDS: 'xmlstarlet' # 'cowsay' # Both are available packages in Debian (Sid) repository. # To install them just type 'apt-get install xmlstarlet cowsay' # # # set -x # Uncomment this for debugging. # # ##################################### # Use 'cowsay -l' to the full list of cow files in your system # # (Sorry fellas I removed possible offensive 'sodomized*, head-in,telebears, # # kiss' cow files # ###################################### cows=(apt beavis.zen bong bud-frogs bunny cheese cower daemon default dragon dragon-and-cow elephant elephant-in-snake eyes flaming-sheep ghostbusters hellokitty kitty koala kosh luke-koala mech-and-cow meow milk moofasa moose mutilated ren satanic sheep skeleton small stegosaurus stimpy supermilker surgery three-eyes turkey turtle tux udder vader vader-koala www) random=RANDOM%44 ######################################### ## Get the XML feed and do the selection ## ######################################## /usr/bin/xmlstarlet sel --net -t -m "/rss/channel/item/description" -v "." "http://dictionary.reference.com/wordoftheday/wotd.rss" | /usr/bin/cowsay -n -f ${cows[random]} # End of the script

Thursday, October 25, 2007

Bittorrent Mainline Port Randomizer / Custom Settings script

Here is a quick and dirty bash script that I used when Bittorrent Mainline removed the random port option.

  • The script uses a random port to listen from a given range
  • Sets custom upload limits,refresh intervals and client address(IP)
  • Disables Linux uPnP
  • Changes the default data directory (default is ~/Desktop/Bittorrent)
  • Saves the downloaded file to current directory

Copy the code to text file and save it as Bitty.sh and set execute permission with 'chmod a+x Bitty.sh' and launch with ./Bitty.sh <torrentfile>.torrent

You may need to change the values to suit your needs. Also note that the You the /usr/bin/bittorrent-console is commandline client from the bittorrent Mainline for GNU/Linux and change the location on your Linux distro.


#!/bin/sh
#
#
#set -n
# Bitty.sh - a port randomizer for bittorrent mainline
#set -x 
#
MAXPORT=25000
MINPORT=20000

number=0

while [ "$number" -le $MINPORT ]

do 
  number=$RANDOM
  let "number %= $MAXPORT" 
done 


exec /usr/bin/bittorrent-console  --ip debbox.no-ip.org  --display_interval 30 
--no_upnp --minport $number  --maxport 25000 --max_upload_rate 5120 
--max_initiate 70  --save_incomplete_in /mnt/BigMaMa/.bittorrent/incomplete
 --data_dir /mnt/bigMaMa/.bittorrent  --save_in . "$1"


Please feel free to improve the script !

Monday, October 22, 2007

Disown, nohup : Bash Commands

Life of a geek is not without its temptations, they come sometimes in the guise of friends asking me to join for a steaming hot cup of coffee or the company of a sweet young thing :o)

Mostly I give in to temptations and allow myself to be drawn away from my computer terminal. Being a little paranoid about security I don't leave my console unattended instead I prefer to log off completely. But that's not always possible since I have quite a few jobs running in background, now what is fastest and the best way to get rid of them so that I can log off and leave them running after I am gone? disown is the answer. This bash specific command allows us to unbind various jobs from the current login session. I use 'disown <jobid>' to get rid of the job(s) running on my console login session. Volia now I can logoff and run.

But wait why not use some computing time while I am gone, perhaps I can compile a new kernel or download a package with wget. Using the nohup command is apt here since I am planning to logoff leaving the computer to do the tasks. Just prefix nohup before any command and run it in the background by placing "&" at the end of the command, that's it.

How does disown and nohup commands work? What they do is that they connect all your process to the parent process of the computer which happens to init (the granddaddy of all process running on the computer). Viewing all the process on the computer help us understand it better, Look a the the output of the pstree command below. The init is first (root of the inverted tree) process which radiates all the other process in the computer. Here you can see various daemons like cron,dbus etc, and also various programs like emacs, iceweasel (firefox), pidgin, xterm, xpdf.

$ pstree init-+-cron |-daemon |-another-daemon |-emacs |-getty |-login---bash---links |-login---bash---startx---xinit-+-Xorg | `-fluxbox-+-bash-+-firefox-bin---7*[{firef+ | | `-pidgin | `-xterm---bash-+-pstree | `-xpdf.bin |-preload |-syslogd `-udevd

You might ask me why I don't use screen instead of nohup ? Well, I tend not to use screen for those commands that don't need my supervision. I might run my rss reader raggle under screen but not commands like updating locate databases, downloading packages or files using bittorrent I leave it to nohup.

Saturday, October 20, 2007

Understanding the GNU / Debian Linux Boot Process

Learning a Unix-like operating system is like a Herculean task for most of us. Most people even those working in the IT industry break into cold sweats on hearing words like Unix, Linux and Debian. Such aura of mystery and wizardry commonly associated with it is perhaps much to do with fear is unknown rather than the difficulty of learning the operating system(s).

One of the important advantage of learning a Unix-like operating system is in its transparency. If you want to understanding the intrusive details of how an operating system works you can dig right into the bowls of the operating system and to its source code. A closed source Operating system like Microsoft Windows NT XP or Vista will not allow you do that thus a very bad choice for learning OS concepts.

The boot process starts the moment you power up your PC and until the time the computer is able allow you to log on. Understanding this booting process of a GNU/ Debian Linux is an essential part of mastering the OS, the From Power Up To Bash Prompt HOWTO written a decade ago provides a detailed look at the boot process from Power up to the bash prompt . Even the experienced Linux users will learn from this HOWTO.

The author(s) of the HOWTO document sums it up as thus.

I find it frustrating that many things happen inside my Linux machine that I do not understand. If, like me, you want to really understand your system rather than just knowing how to use it, this document should be a good place to start. This kind of background knowledge is also needed if you want to be a top notch Linux problem solver.

This HOWTO is can be found in doc-linux package on GNU/Debian in text and html formats.The Debian Documentation page has an extensive list of manuals that are available to aid the learning the OS. Also visit the The Linux Documentation Project(TLDP) for more such HOWTO's , Guides and FAQ's.

Monday, October 8, 2007

Using Transmission Bittorrent client on console

Update: A change in package since this post was published, read the update here
Here is an another light-weight bittorrent client Transmission which really works well. One of the highlight of this program is that its easy to use even for those who are new to Bittorrent P2P system and the bittorrent old hands would like Transmission because it stays out of their way and integrates nicely with your environment. I primarily work on command line console and like to run my bittorrent downloads in a screen session in the background and this bittorrent clients fits the bill for me.

You can learn more about this program from its features page.
Transmission runs natively on over five operating systems. Seamless configuration of your network, intelligent banning of peers who send corrupted data, and built in Peer Exchange are some of the features which enable Transmission to download your files as quickly as possible. All this is done in the background, without the user having to worry about complicated settings.

So, lets get started with transmission shall we, We will download the latest GNU/Linux Debian (Stable) CD ISO from the Internet. I already downloaded the torrent file of the first CD in my current directory. And installed transmission-cli package which provides the transmission-cli program on Debian Sid. This is a command-line only version of transmission, if you are interesting GUI version use the GTK version which is also available on Debian Sid or from the project page.

Here we launch the transmission with screen, the -p tells transmission to listen on port 25000. You can stop the program by pressing Ctrl+C .

screen -a -S 'Bittorrent' /usr/bin/transmission-cli -p 25000 debian-40r1-i386-CD-1.iso.torrent

Happy Torrenting !!!!

To learn about screen read the One minute Screen tutorial .

One minute Screen Tutorial

For those of you who love the command line screen provides a definitive way of organizing your work into a set detachable console sessions called screens which can be added and removed on demand. This removes the limitation of the number of virtual consoles(tty) you can have. Think of screen as a virtual windowing system for the console. ;o)

$ screen -a -S 'New Screen Session' $ sleep 60 #Press Ctrl+A+D for detaching the screen [detached] $ screen -list Your inventory: 4660.New Screen Session (Detached) 1 Socket in /var/run/screen/S-arky. $ screen -r 4660 $ sleep 60 $ #Press Ctrl+D for terminating the screen [screen is terminating]

Here we first started a new screen and ran a command 'sleep 60' then detached the screen session. Later we listed out (all) the current screens running on this system along with the their process ID's and re-attached our old screen session. Once the sleep command is completed we terminated the session. Also you can try pressing keys for command completion whenever possible.

  • screen -a Starts a new screen, -S add a title to the screen session, you can use either the process ID or the screen title with '-r'
  • screen -list Lists the currents screen sessions
  • screen -r Reattaches a existing screen with the given process ID

Wednesday, October 3, 2007

Recovering a runaway console

Sometimes a program dies leaving your console in a totally unusable state. All you see is a bunch of random numbers and unreadable characters like this.
a normal Linux console Instead of seeing a normal console with a blinking cursor and bash prompt like this.
linux console with random characters

Don't worry, the solution is rather very simple. So, the next time your console goes haywire like this, just type reset (the console won't display what you are typing, since its still in an abnormal state) and press enter. Voila, the console is reinitialized and back in its normal state.

The reset command is the little brother of tset (terminal settings) command and is provided by ncurses-bin package.

Saturday, September 29, 2007

Bash's noclobber

If there is something I don't miss is a chance of thumbing through a new book. It always seems that I never have enough money to buy them all, so the resort to the next best option. To seek out engineering students who buy books in the feverish days before the examinations only to banish them to the dark and dusty corners. Needless to say these are technical books explaining basics of languages or Unix shell environment, a particular favorite of mine.
Perhaps it is my insatiable curiosity that drives me to read all books even the 'for Dummies' books which are looked down with disdain by other geeks, I find them amusing and entertaining.

Lately I have started noticing that some information was missing here and there. For instance, the setting of no clobber of existing files is nowhere to be seen in shell books these days , I guess the authors of these technical books consider it unimportant and almost trivial.

The no clobber setting however is life saving sometimes, it prevents accidental overwriting of existing files while using IO redirection on bash shell or badly written shell scripts. It is better to set it with set -C than losing a very important file .

$ set -C # Enable it $ ls > foo bash: foo: cannot overwrite existing file $ set +C # Disable noclobber $ ls > foo # exiting file overwritten!!!

Just include a line with set -C option somewhere in .profile or .bashrc file inside your home directory.

Wednesday, September 26, 2007

Creating files the hackers way!

In *nix is all about text files, Let it be the costly piece of hardware that you brought or just some configuration setting to your favorite programs everything is humbled down to be a simple text file. Alright creating a new file doesn't necessarily mean opening your favorite editor pico or vi. There are quite faster ways for creating blank files on the shell, tell me find any simpler method. ;) Touch happens to be the standard way to create a blank file but that means I have to type five characters ( t o u c h ) being a lazy typist I use a much simpler method. $ touch testfile $ ls -l -rw-r--r-- 1 arky arky 0 Mar 23 22:31 testfile My hack is using the standard shell redirect operator > which does save some typing. ;) $ > testfile2 $ ls -l -rw-r--r-- 1 arky arky 0 Mar 23 22:31 testfile -rw-r--r-- 1 arky arky 0 Mar 23 22:31 testfile2

Popular Posts