giovedì 10 dicembre 2015

Automatically clean and format a file name for Web in Bash

Hello there!

You want to automatically format and clean a file name before upload on a website?
Do you want to remove spaces, symbol and accents from a filename automatically, or in batch?

These command will strip anything that's not good to have in a web filename and copy your file to the new file! You can rename original file by change last "cp" with "mv"

#!/bin/bash
FILE=$1
# Remove path
FILECLEAN="${FILE##*/}"
# Find extension (sometimes useful)
FILEXT="${FILECLEAN##*.}"
# Find filename without extension

FILENAME="${FILECLEAN%.*}"

# Convert spaces in dash

FILENAME="${FILENAME// /-}"

# Remove symbols and keep dash and underscores

FILENAME="${FILENAME//[^[:alnum:]_-]/}"

# Remove eventual double dashes from filename

FILENAME="$(echo $FILENAME | tr -s '-')"

# First letter uppercase and other lowercase

FILENAME="$(tr '[:lower:]' '[:upper:]' <<< ${FILENAME:0:1})$(tr '[:upper:]' '[:lower:]' <<< ${FILENAME:1})"


cp "$FILE" "$FILENAME"".""$FILEXT"

Save it into a file with .sh extension, then do this to make it executable:
chmod +x file.sh

Execute with:
./file.sh file

You can even bath the process with a script like this:
#!/bin/bash

# Rimuovo i *.jpg etc dalla lista dei FILE

shopt -s nullglob
for FILE in *; do   
   # Remove path
   FILECLEAN="${FILE##*/}"  
   # Find extension (sometimes useful) 
   FILEXT="${FILECLEAN##*.}"   
   # Find filename without extension
   FILENAME="${FILECLEAN%.*}"
   # Convert spaces in dash   
   FILENAME="${FILENAME// /-}"
   # Remove symbols and keep dash and underscores   
   FILENAME="${FILENAME//[^[:alnum:]_-]/}"   
   # Remove eventual double dashes from filename   
   FILENAME="$(echo $FILENAME | tr -s '-')"   
   # First letter uppercase and other lowercase  
    FILENAME="$(tr '[:lower:]' '[:upper:]' <<< ${FILENAME:0:1})$(tr '[:upper:]' '[:lower:]' <<< ${FILENAME:1})"

   cp "$FILE" "$FILENAME"".""$FILEXT"
done

Convert PDF to JPEG, JPG, PNG or other images format Mac OSX

Hello there,

You want to convert PDF to image for free? You are in the right place!
You can even automate the process in batch!

We will use Brew to install all the necessary stuff to convert PDF in JPEG, JPG, PNG or other images format.

1. Go to http://brew.sh/ and paste into a terminal the installation string:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. Install ImageMagick with Brew:
brew install imagemagick

3. Install GhostScript with Brew: (used for PDF conversion)
brew install ghostscript

4. Ready to go!
Type name of file you want to convert followed by save name of the converted file and the format you want:
PDF to JPG
convert SourceFile.pdf DestinationFile.jpg

PDF to PNG
convert SourceFile.pdf DestinationFile.png

If converted images have a black background instead of white, you have to set default background as white and remove transparency:
-background white -alpha remove

So command will be:
convert SourceFile.pdf -background white -alpha remove DestinationFile.png

Resize output images:
convert SourceFile.pdf -resize <Width>x<Height> DestinationFile.jpg

We can specify only one dimension or both like:

Resize as 600px width:
convert SourceFile.pdf -resize 600x DestinationFile.jpg

Resize as 600px height:
convert SourceFile.pdf -resize x600 DestinationFile.jpg

Resize both as squared 600x600:
convert SourceFile.pdf -resize 600x600 DestinationFile.jpg

Resize shortest dimension to preserve aspect ratio (use '^' operator):
convert SourceFile.pdf -resize 600x600^ DestinationFile.jpg

You want to convert file in batch?

Create a script file with this:

#!/bin/bash
# Remove *.jpg *.JPG *.jpeg from list of filesshopt -s nullglob
for FILE in *.jpg *.JPG *.jpeg; do   convert "$FILE" -resize x150 "$FILE""_x150.jpg"
done

Save it in a file with .sh extension and from terminal do this to make it executable:
chmod +x file.sh

Then execute this in you images folder:
./file.sh

It will convert and save the result of all the .jpg images in that folder.

Let me know if helped!