Shell script to remove whole ftp directory structure

in Categories Ftp last updated April 9, 2008
#!/bin/bash
# Shell script to remove whole (nuke) ftp directory structure  
# See url for more info:
# https://www.cyberciti.biz/tips/how-to-nuke-a-ftp-server.html
# -----------------------------------------------
# Copyright (c) 2006 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# NOTE: Requires ncftp/ncftlls ftp clients/commands
# =========================================================================
# Ftp server settings 
 
### change me ###############################
# ftp login user
FUSER='vivek' 
 
# ftp password
FPASSWD='password' 
 
# ftp server ip/name 
FTPS='array05.wan.nixcraft.co.in'
 
# ftp dirs to nuke Separate multiple dirs using
# a blank/white space i.e. /dir1 /dir2)
FTPDIRS='/dir1 /dir2'
##############################################
for d in $FTPDIRS
do
 subdires="$(ncftpls -u $FUSER -p $FPASSWD  ftp://${FTPS}/${d}/)"
 for dir in $subdires
 do
 rdir="${d}/${dir}"
ncftp -u"$FUSER" -p"$FPASSWD" $FTPS<<EOF
cd $rdir
rm *
rmdir $rdir
quit
EOF
  done
done
# end of script

Shell script to copy all files recursively and upload them to remote FTP server

in Categories Backup, Ftp last updated April 9, 2008
#!/bin/bash
# Shell script to copy all files recursively and upload them to 
# remote FTP server (copy local all directories/tree to remote ftp server)
#
# If you want to use this script in cron then make sure you have
# file pointed by $AUTHFILE (see below) and add lines to it:
# host ftp.mycorp.com
# user myftpuser
# pass mypassword
# 
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
FTP="/usr/bin/ncftpput"
CMD=""
AUTHFILE="/root/.myupload"
 
if [ -f $AUTHFILE ] ; then 
  # use the file for auth
  CMD="$FTP -m -R -f $AUTHFILE $myf $remotedir $localdir"
else
  echo "*** To terminate at any point hit [ CTRL + C ] ***"
  read -p "Enter ftpserver name : " myf
  read -p "Enter ftp username : " myu
  read -s -p "Enter ftp password : " myp
  echo ""
  read -p "Enter ftp remote directory [/] : " remotedir
  read -p "Enter local directory to upload path [.] : " localdir
  [ "$remotedir" == "" ] && remotedir="/" || :
  [ "$localdir" == "" ] && localdir="." || :
  CMD="$FTP -m -R -u $myu -p $myp $myf $remotedir $localdir"
fi
 
$CMD

Shell script to rename given file names to from uppercase to lowercase OR lowercase to uppercase

in Categories File-management last updated April 9, 2008
#!/bin/bash
# 
# Shell script to rename given file names to from uppercase to 
# lowercase OR lowercase to uppercase
# 
# Copyright (C) 2005 nixCraft project.
#
# This script licensed under GNU GPL version 2.0 or above
#
# Support/FeedBack/comment :  http://cyberciti.biz/fb/
# -------------------------------------------------------------------
# To rename file uppercase to lowercase create sym link:
# ln -s /path/2upper /path/2lower
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
FILES="$1"
ME="$(basename $0)"
 
# function to display message and exit with given exit code
function die(){
  echo -e "$1"
  exit $2
}
 
# exit if no command line argument given
[ "$FILES" == "" ] && die "Syntax: $ME {file-name}\nExamples:\n $ME xyz\n $ME \"*.jpg\"" 1 || :
# scan for all input file
for i in $FILES
do
   # see if upper to lower OR lower to upper by command name
   [ "$ME" == "2upper" ] && N="$(echo "$i" | tr [a-z] [A-Z])" ||  N="$(echo "$i" | tr [A-Z] [a-z])"
   # if source and dest file not the same then rename it  
   [ "$i" != "$N" ] &&  mv "$i" "$N" || :
done

Shell script to backup directories from Linux server to Windows 2000/NT Server

in Categories Backup last updated August 5, 2008
#!/bin/bash
# Shell script to backup directories from Linux server to Windows 2000/NT Server. 
# Run it as follows
# Scriptname /home backup abc123 //server2000/backup
# Backup /home directory from Linux box  to NT/2000 box called
# 'server2000' in share called '/backup' with username
# 'backup' and password 'abc123'
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
# backup what?
FROM=$1
# NT Connection Info #
# NT Username
NTUSER=$2
# NT Password
NTPASSWD=$3
# NT ShareName i.e //server/backup etc
NTSHARE="$4"
 
# BackUpDir Name
BACKDIR="$(hostname -s)"
 
# Local mount point
MNT="/mnt/smbbox"
 
# Get date and time 
NOW=$(date +"%m-%d-%Y-%I_%M%P")
# backup file  name
BAKFILE="backup.$NOW.tar.gz"
 
if [ "$#" != "4" ]; then
	echo "Syntax:"
	echo "$(basename $0) {Linux-directory} {NTusername} {NTpassword} {//NTserver/share-name}"
	exit 1
fi
# make sure $from do exits
if [ ! -d $FROM ]; then
   echo "Backup source directory \"$FROM\" does NOT exist"
   exit 2
fi
#Create tar to backup first
tar -czf /tmp/$BAKFILE $FROM 
 
#Mount the smb to /mnt 
[ ! -d $MNT ] && mkdir -p $MNT || :
 
mount -t smbfs -o username=$NTUSER,password=$NTPASSWD $NTSHARE $MNT
 
[ ! -d $MNT/$BACKDIR ] && mkdir -p $MNT/$BACKDIR || :
 
# Copy new tar to ntbox
cp /tmp/$BAKFILE $MNT/$BACKDIR
 
# Send sync aka force to write data before issuing umount
sync
 
# issue umount
umount $MNT

Shell Script to swap values in two variables x and y

in Categories Shell Math last updated April 9, 2008
#!/bin/bash
# Shell Script to swap values in two variables x and y
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
echo "Enter value for x : "
read x
echo "Enter value for y : "
read y
echo "Before swap, x = $x and y = $y"
z=$x
x=$y
y=$z
echo "After swap, x = $x and y = $y"

Shell Script that outputs the number of users with entries in the /etc/passwd file

in Categories File-management last updated April 9, 2008
#!/bin/bash
# Shell Script that outputs the number of users with entries in the
# Unix or Linux /etc/passwd file
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
PASSWD=/etc/passwd
 
# easy method with wc
OUT=$(wc -l $PASSWD)
 
# More accurate method to find out
# If user account suspended using # character then second method 
# is more accurate and recommend
OUT1=$(grep -Ev "^#" $PASSWD | wc -l)
 
echo "$OUT [$OUT1]"
# You can filter result with awk
echo $OUT | awk '{ print $1 }'

Shell script to find whether an input number is palindrome or not

in Categories Academic, Shell Math last updated April 12, 2008

A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either direction (the adjustment of punctuation and spaces between words is generally permitted). Composing literature in palindromes is an example of constrained writing. The word “palindrome” was coined from Greek roots palin (“back”) and dromos (“way, direction”) by English writer Ben Jonson in the 1600s.

A palindromic number is a number where the digits, with decimal representation usually assumed, are the same read backwards, for example, 58285. They are studied in recreational mathematics where palindromic numbers with special properties are sought. A palindromic prime is a palindromic number that is a prime number.

#!/bin/bash
# Shell script to find whether an input number is palindrome or not
# -----------------------------------------------
# Copyright (c) 2006 nixCraft <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
# store single digit
sd=0
 
# store number in reverse order
rev=""
 
# store original number
on=$n
 
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 ))  # get next digit
    # store previous number and current digit in reverse 
    rev=$( echo ${rev}${sd} ) 
done
 
if [ $on -eq $rev ];
then
  echo "Number is palindrome"
else
  echo "Number is NOT palindrome"
fi

Shell script to read 5 digit number and calculate the sum of digit

in Categories Shell Math last updated April 4, 2008
#!/bin/bash
# Shell script to read 5 digit number and calculate the sum of digit
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter numnber : "
read n
 
# find out length of string using wc -c command
len=$(echo $n | wc -c)
 
# remove \n i.e. new line character
len=$(( $len - 1 ))
 
# use loop to go throug all digit one by one and calculate sum of digit on fly
for (( i=1; i <= $len; i++ ))
do
   sum=$(( $sum + $(echo $n | cut -c $i) ))
done
echo "Sum of $n is $sum"