Useful commands in Linux for specific tasks

Below are the few commands which were useful during my tasks in linux. There are so many challenges we face during our work and we always come with a solution at last. So it is good to spread that solution to everyone.

I always try to note down them in my documents. Now i will post them here in this blog.

1) RSYNC FOR LOCAL FOLDERS

rsync -rtvu uploads-bkp-26082016/ uploads/

Where

-r - recursive
-t - to save modified timestamp
-v - verbose
-u - do not overwrite existing files in destination folder untill the file modification time changed in source folder.

2) FIND COMMAND TO SEARCH FILES FOR MULTIPLE EXTENSIONS  AND FOR SINGLE FOLDER NOT RECURSIVELY

find /app/ -maxdepth 1 -iname '*.tar.gz' -o -name '*.sql'

AND FILES OLDER THEN n DAYS WITHOUT RECURSIVE SEARCH

find /app/ -maxdepth 1 -type f -mtime +n

3) Script to Automate document Root and also for the sql database with logging

#!/bin/bash
# -------------------------------------------------------------------------
# Prerequisites Variables 
DB_HOST="xyz.com"
DB_USER='user'
DB_PASS='password'
DB_NAME='db_name'
BACKUP_DIR='/backup'
LOG_DIR='/backup/logs'

echo "############################" >> $LOG_DIR/backup.log
echo `date`  >> $LOG_DIR/backup.log
echo "############################" >> $LOG_DIR/backup.log

#website folder backup
tar -zcvf $BACKUP_DIR/backup_name-`date +%d%b%Y-%HH`.tar.gz  /data_folder   > /dev/null

if [ $? -eq 0 ]
then
	tar -zcvf $BACKUP_DIR/httpd-`date +%d%b%Y-%HH`.tar.gz /etc/httpd     ## Apache dir backup

	mysqldump -h $DB_HOST -u $DB_USER $DB_NAME -p`$DB_PASS` > $BACKUP_DIR/db-`date +%d%b%Y-%HH`.sql
    if [ $? -eq 0 ]
	then
	    echo "`date`| DB backup done successfully.." >> $LOG_DIR/backup.log
	else
        echo "`date`| DB backup failed.." >>  $LOG_DIR/backup.log	
    fi

fi

4) How to print options fields for user 

PS3='Please enter your choice: '
echo $PS3
options=("Name" "Age" "Lname" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Name")
            echo "you chose choice 1"
            ENV_VAR=test
            echo $ENV_VAR
            break
            ;;
        "Age")
            echo "you chose choice 2"
            ;;
        "Lname")
            echo "you chose choice 3"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

5) Flush home page cache from varnish in magento website.

varnishadm ban.url '^/(?:index.php?)?$'

OR 

try this if required

varnishadm ban.url '^/(?:http:\/\/www.domain.com.au?)?$'

or if you want to flush any other specific url then replace index.php to your URL.

6) How to pass password in ssh command to remote server along with multiple commands with sudo user?

sshpass -p 'passwd' ssh -t user@ip "echo passwd | sudo -S cmd1 && cmd2"

Here

-p -> for asking password with sshpass – insecure method to pass password which will be visible to everyone in ps command

or

-e -> for this option we need to set ENV varibale “SSHPASS” for password

-S -> for standard input.

7) Merge two files and create a new file with complete sorted content.

diff -DVERSION1 file1   file2 | grep -v "#ifndef VERSION1" | grep -v "else" | grep -v "endif" > file3

8) Command to get hists per hour from apache access logs.

grep "15/Dec" apache-access_log | cut -d[ -f2 | cut -d] -f1 | awk -F: '{print $2":00"}' | sort -n | uniq -c

Will update this post later with more commands.

Thanks 🙂

 

Leave a Reply

Your email address will not be published. Required fields are marked *