Google Translate

Stella inattivaStella inattivaStella inattivaStella inattivaStella inattiva
 

Here some useful snippets.

 

Here some useful snippets.

 

Read list of file

 

Read list of file
for f in *.tar.gz; do echo "Processing $f "; done

 

Print Date Time and name of bash file

 

Print Date Time and name of bash file
#/usr/bin/env bash
echo  "`date +%Y-%m-%d_%H:%M:%S` - $0"

 

Check Argument n# 1

Check Argument n# 1

The following code checks if argument 1 was missing, otherwise checks its value.

#/usr/bin/env bash
p1=$1
if [ -z "$1" ]; then
	echo 'Missing parameter: <on> to mount <off> to unmount';
	exit;
elif [ $p1 != "on" ] && [ $p1 != "off" ] && [ $p1 != "offon" ]; then
	echo 'Wrong parameter: <on> to mount <off> to unmount';
	exit;
fi

 

Check if a folder exists

Check if a folder exists
#/usr/bin/env bash
if  [ ! -d /mnt/hubic/default ]; 	then
	echo "folder <default> not exists..."
fi

 

Function inside a bash script

Function inside a bash script
# Arguments: <folder> <filetar>
function tar_backup {
folder=$1
filetar=$2
echo "Backup $folder Started on:`date +%Y-%m-%d_%H:%M:%S`"
tar -pczf $savpath/$filetar.tar.gz $folder/$file
echo "Backup $folder Finish on:`date +%Y-%m-%d_%H:%M:%S`"
}

# tar the folder /myfolder/myfiles and create the file mybackup.tar.gz
tar backup /myfolder/myfiles mybackup

 

File older than...

Search file older than days - Tested and To be test!
find /your/file -mtime +182 -exec rm {} +
or
find . -name "*.sess" -mtime +182
or
find . -mindepth 1 -mtime +3 -delete
TESTED
find /var/log/myfolder/* -name "*.log"  -mtime +180 -delete

 

CD from bash script, alias, references

CD from bash script
#!/bin/bash
# cdsite.sh
# how to use: # . cdsite.sh
# note: there is a blank space between the dot and the csite.sh
cd /var/www/mysite.com/web
CD using the alias

You can use a simple alias to change directory.  ~/.bash_profile and/or in ~/.bashrc

alias cdsite="cd /var/www/mysite/web"

Stack Overflow

 

Delete Files

DELETE FILES

Delete all file with extension html in the folder /tmp and all sub-folder

find /tmp/ -name "*.html" -type f -delete

 

Create index.html

Create index.html if not exists...

This script will check and create if not exists the file index.hml or index.php in all folders starting from the path specified 

#!/bin/bash
# chkwebindex.sh - Check and create file index.html
#
# example: chkwebindex.sh /var/www/mysite.it/web/
#

# save and change IFS
OLDIFS=$IFS
IFS=$(echo -en "\n\b")

# try get path from param
path=""
if [ -d "$1" ]; then
    path=$1;
else
    path="/tmp"
fi

echo "Check and Create index.html in the folder and sub-folder: $path"

for x in `find $path -type d`
do
echo "$x"
        # ./yourexecutable -i $x -o $x.processed
        if  [ ! -f "$x/index.html" ] && [ ! -f "$x/index.php" ]; then
                echo "File index.html not exists in $x ... created!"
                echo "<!DOCTYPE html><title></title>" > $x/index.html
        fi
done

# restore IFS
IFS=$SAVEIFS

 

CHOWN All Files Owned by Root

CHOWN ALL FILES OWNED BY ROOT

Search all files owned by root and change it to another one

find /tmp -name "*.html" -user root -exec chown web1:client1 {} +

 

FIND SUBSTRING IN VAR USING GREP

LIST="some string with a substring you want to match"
SOURCE="substring"
if echo "$LIST" | grep -q "$SOURCE"; then
  echo "matched";
else
  echo "no match";
fi

 

more details on stack overflow

 

 

To Be continued

To Be Continued... 

Stay tuned, I will add more snippets soon...