Trim first 16 chars from filename and rename the file

Used this script to remove the chars from mp3 files. Keep this script file in the directory and execute it. Simple!
Enter the number or chars to remove in the command line param – ./ren.sh 10

#!/bin/bash

# -----------
# Usage: ./sh NoLeadingTrim [NoOfEndTrim]
#
# -----------
if [ $# -eq 0 ]
  then
    echo "Enter number of leading characters to trim"
    exit
fi

files=`pwd`/*.mp3
files=*.mp3
for f in $files
do
  fn="$f"
  len=${#fn}
  if [ $# -gt 1 ]; then
    rel=`expr $len - $2`
    nn=${fn:$1:$rel}
    echo "$nn".mp3
    mv "$fn" "$nn".mp3
  else
    nn=${fn:$1}
    echo "$nn"
    mv "$fn" "$nn"
  fi
done

Linux Shell – Iterate through file listings

This script is useful if you’d like to iterate through find results or say files in a directory and pick specific files and do operations on them. Specific to my case – I had to search for files with current timestamp in the filename and use those files for some operations. Here’s what I did in a bash script

TIMESTAMP=`date +%F`
echo $TIMESTAMP

cd /home/varun.verma/scripts/shell/getFiles
#find . -name "*.tar.gz" -type f -exec basename {} \; #Used this command to test the results. It finds files with a format and displays only the name of files, not the path (since I know where I am searching)

for i in `find . -name "*$TIMESTAMP*.tar.gz" -type f -exec basename {} \;`
do
  file=`echo -n $i`
  echo $file #Or do some operation here
done

Directory comparisons using diff

I’ve started to like the diff command a lot. It can help me do directory comparisons with a variety of options that I totally love. Here are a few samples that I mostly use

Compare directories and list only filenames – ignore content

# -q Report only whether the files differ, not the details of the differences
# -r When comparing directories, recursively compare any subdirectories found
diff -qr /dir1 /dir2

Ignore certain log files during comparison

diff -r /dir1 /dir2 -x *.log -x *_Mod

Automatically backing up JIRA DB (Postgres)

The below shell script can be run frequently to backup JIRA DB (Postgres) at any frequency. The script in this case is being executed on the DB Server and then the DB backups are pushed to an external server to prevent from a server crash.

#!/bin/bash

#----------------
# Configuration
# ---------------
backupDir="/var/jira/data/usr_backups/data/"
logDir="/var/jira/data/usr_backups/script/log/"
curDate=`date +%Y%m%d_%H%M`
fileName="jiradb_backup_"$curDate".sql.gz"
echo $backupDir$fileName

logMsg=''

#----------------
# DB Backup
#----------------
pg_dump -Fp -h localhost -U jira -w jiradb | gzip > $backupDir$fileName

if [ -e $backupDir$fileName ]
then
  echo "backup successful"
  logMsg="jiradb backed up successfully"
else
  echo "backup failed"
  logMsg="Error backing up jiradb"
fi

echo $logMsg > $logDir"log_"$curDate".log"

#----------------
# File Transfer
#----------------
sftp remoteserver.domain.com:/var/dbbackup/jira <<EOM
put $backupDir$fileName
EOM