bash

Changing file extensions

Suppose I need to change the file extensions of certain files in a directory.

#!/bin/bash
echo "Please specify a directory: "
read folder
cd $folder
for i in *.txt
    do
    mv "$i" `basename $i txt`fasta
done

The directory address specified by the user is read into the script in line 2. This address is used to cd to the directory. This script changes the extensions of .txt files to .fasta.

Displaying directory contents

For security reasons, most servers on the Internet do not show the contents of a directory if the directory address is typed. The user is either redirected to index.whatever, a default page, page not found or access denied. Thus the list of files in the directory are not displayed.

If you need to show the contents of your directory, you can use the following code:

bash - printing parameters

A parameter is an entity that stores value and is available to a program. There are many system variables available to BASH. They could be retrieved easily by prefixing a $ sign to the variable. Alternately, you can also surround them with curly braces and prefix a $ sign

#!/bin/bash
#: Title       : print parameters
#: Version     : 1.0
#: Description : print parameters
#: Options     : None

printf "%s " "Current Working Directory"
echo $PWD
printf "%s " "Home Directory"
echo ${HOME}
printf "%s" "Command Directories $PATH"

Output of the script

Display image properties with bash

The following bash script shows how to capture image properties from ImageMagick:

#!/bin/bash
a=`identify xa.jpg`
strarray=($a)
echo 'format: ' ${strarray[1]}
echo 'dimensions: ' ${strarray[2]}
echo 'filesize: ' ${strarray[6]}

xa.jpg is an image which is stored in the same directory as this script.

identify is an ImageMagick utility. The output of identify xa.jpg is:

Pages