Difference between revisions of "Sample Scripts"

From rbachwiki
Jump to navigation Jump to search
Line 248: Line 248:
  find . -type f -name '*2019*' -print0 | rename -0 's/2019/2020/gi'
  find . -type f -name '*2019*' -print0 | rename -0 's/2019/2020/gi'


===Rename Prepend ===
===Rename Prepend Mac Only ===
  rename -A 'xxxx-' *.jpg
  rename -A 'xxxx-' *.jpg



Revision as of 20:16, 26 November 2019

Script using if

 #!/bin/bash
echo -e "This program adds entries to a family database file . \n"
echo -e "Would you like to add an entry to the family database file? \n"
read ANSWER1
if [ $ANSWER1 = "y" -o $ANSWER1 = "Y" ]
then
echo -e "Please enter the name of the family member --> \c"
read NAME
echo -e "Please enter the family menber's relation to you (i.e. mother) -->\c"
read RELATION
echo -e "Please enter the family member's telephone number -->\c"
read PHONE
echo -e "$NAME\t$RELATION\t$PHONE">>database
fi
echo -e "Would you like to search an entry in the family databae file?\n"
read ANSWER2
if [ $ANSWER2="y" -o $ANSWER2="Y" ]
then
echo -e "What word would you like to look for? -->\c"
read WORD
grep "$WORD" database
fi

Script using CASE

#!/bin/bash
while true
do
clear
echo -e "What would you like to do?
Add and entry (a)
Search an entry (s)
Quit (q)
Enter your choice (a/s/q)-->\c"
read ANSWER
case $ANSWER in
a|A) echo -e "Please enter the name of the family member -->\c"
read NAME
echo -e "Please enter the family member's relation to you (i.e. mother) -->\c"
read RELATION
echo -e "Please enter the family member's telephone number -->\c"
read PHONE
echo -e "$NAME\t$RELATION\t$PHONE" >>database
;;
s|S) echo -e "What word would you like to look for?-->\c"
read WORD
grep "$WORD" database
sleep 4
;;
q|Q) exit
;;
*) echo "You must enter either the letter a or s"
sleep 4
;;
esac
done

While Loop

#!/bin/bash
index=1
while [ $index -lt 6 ]
do
echo "hello ${index}"
((index++))
done


while [ "$correct" != "y" ]
do
        read -p "enter your name:" name
        read -p "is ${name} matched" correct
done

Files in folder to html

echo "print directory conents to file"
echo -e "what is the title---->\c"
read title
sleep 1
echo -e '<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>' > index.html
sleep 2
echo -e $title >> index.html
sleep 2
echo '</title>' >> index.html
sleep 2
echo -e '</head>
<body>' >> index.html
sleep 2
echo '<h1>' >> index.html
sleep 2
echo $title >> index.html
sleep 2
echo '</h1>' >> index.html
sleep 2
ls | awk -F: '{print "<p> <a href=\""$1  "\">"$1 "</a></p>"}' >> index.html
sleep 2
echo -e '</body></html' >> index.html

Reading a file, line by line

read -p "Enter Filename " FILENAME
grep [rR]obert $FILENAME | while read LINE
do
echo "<p> ${LINE}</p>"
done

Send Find and replace to a separate file

read -p "Enter Filename " FILENAME
sed 's/[rR]obert'xor/g' > two.txt
sleep 2
#clear output file
echo " Cleaned file " > three.txt
grep -n -i xor two.txt | while read LINE
do
echo "<p> ${LINE} </p>" >> three.txt
done
clear
echo "--------- two.txt -------------"
cat two.txt
echo ""
echo "------three.txt ----------------"
cat three.txt

Backup MYSQL Database

#!/bin/bash
mdate=`date +%F`
echo "================ backing up databasename database =============="
mysqldump -u username -ppassword databasename > databasename-$mdate.sql
sleep 5
echo "================ backing up databasename ===================="
mysqldump -u username -ppassword databasename > databasename-$mdate.sql
sleep 5
echo  "=============== Backing up databasename database ============="
mysqldump -u username -ppassword databasename > databasename-$mdate.sql
sleep 5
echo " ========= done ========="
ls  *.sql > /var/www/sqlfiles.txt
sleep 5
# calls another script which does the actual backup to amazon
cd /var/dirtoscripts/
./bksql.sh

Backup HTML Directory

#!/bin/bash
bkdate = `date +%F`
cd /var/www/
sleep 1
filename=outwaterphotogallery-$bkdate.zip
#zip html folder
zip -r $filename /var/www/html
sleep 5
if [ "$filename" ]; then
aws s3 cp --region us-east-1 $filename s3://websitebackup-opi/web/
fi
sleep 4
count=`ls -l *.zip | wc -l 2> /dev/null`
if [ $count -ge 1 ]; then
mv *.zip /var/www/backups/web/
fi


Script that backs up to Amazon S3

#!/bin/bash
echo "--------- backing up to s3 --------------"
set -e
if [ -f sqlfiles.txt ]; then
while read line
do
aws s3 cp --region us-east-1  $line s3://bucketname
done < sqlfiles.txt

echo "++++++++++= Done  =+++++++++++++++"
else
echo " XXXXXXXXXXX  File Does not exist XXXXXXX"
fi
cd /var/www/dirtoscripts/
# call script that counts the num of sql files in dir and move them to an archive folder
./cleanupscript

Find files and rsync them to a dir

find . -type f -name '*.jpg' -exec rsync -zavhP {} /dirctorytocopyto/ \;

Find Multiple filenames

find . -type f \( -name "*.nef" -o -name "*.jpg" -o -name "*.tif" \)

RSYNC copy directories with spaces

find . -type f -name '*177*' -exec rsync -zavP {} "/var/tmp/my dir" \;

Count Files in Directory

cd /var/www/

count=`ls-l *.sh | wc -l 2> /dev/null`
if [ $count -ge 1 ]; then
mv *.sql /var/www/backups/dirname
fi

Find and delete files with certain names

find -type -f -name '*one*' -exec rm {} \;

Find specific files and copy them to another folder

find -type f -name "*app1*" -exec cp {} /var/www/test/ \;

Using rename command

If its not installed in MacOS, use brew to install it

rename 's/dem/rob/g' * jpg

Will change dem and replace it with rob /g is global which will replace all occurrence of dem in the file name

rename 's/\+/_/g' *.tif

use the \ escape character with the + sign

rename '/.jpg/.tif/' *.jpg

Change jpg extension to tif

Deleting part of a filename

rename 's/dem//' *.jpg

Searching with Groupings

rename 's/(dem|rob)ng/bac/' *.jpg

The central expression of this rename command will search for strings within filenames that have the character sequence “dem” or “rob” where those sequences are immediately followed by “ng”. In other words, our search term is going to look for “string” and “demng”. The substitution term is “bacng”.

Example: demng.jpg,  robmg.jpg

Changing case of filename

rename 'y/a-z/A-Z/' *.jpg

Use the -n option to print names without renaming them

rename -n 's/.html/.php/' *.html

Rename files adding sequential numbers

rename -N 03000 's/find/$N/gi' *.jpg

You have to add the 0 at the beginning otherwise it wont work

Rename Delete string

rename -d '0' *.jpg

Result

030020-abc.jpg
30020-abc.jpg

Find and Rename

find . -type f -name '*2019*' -print0 | rename -0 's/2019/2020/gi'

Rename Prepend Mac Only

rename -A 'xxxx-' *.jpg
metacharacters	meaning
^	Matches the beginning of a string.
$	Matches the end of a string.
.	Matches any character, except a newline.
*	Matches occurrences of the preceding character, or group of characters, zero or more times.
+	Matches occurrences of the preceding character, or group of characters, one or more times.
?	Match occurrences of the preceding character, or group of characters, zero or one times.

If used after a repetition modifier, '?' specifies that the shortest possible match should be used. For instance, 'a{2,4}?' will match 'aa' even if 'aaa' and 'aaaa' would also match. See repetition modifiers, below.
|	Alternation; behaves like a boolean 'OR'. For instance, 'butter|jelly' will match either butter or jelly.
(...)	Grouping. For instance, '(eg|le)gs' will match either 'eggs' or 'legs'.
[...]	A set of characters. For instance, '[abc]' will match either 'a' or 'b' or 'c'. Character sets can be defined as:

[characters]	Matches any one of the characters listed.
[x-y]	Matches any in a range of characters between x and y, inclusive. For instance, '[c-e]' will match either c, d, or e, and '[a-z]' will match any lowercase letter.
[^characters]	Does not match characters; in other words, matches any character except those listed. Can also negate a character range; for instance, '[^a-d]' matches any character except a, b, c, or d.
[\-]	Matches the hyphen character ("-").
[x-yX-Z]	Multiple character ranges can be placed in a character set consecutively. For instance, '[a-zA-Z]' matches any letter, uppercase or lowercase.
{m[,[n]]}	A repetition modifier which matches at least m and at most n of the preceding characters. For instance, 'a{2}' will match 'aa', 'a{2,4}' will match either 'aa', 'aaa', or 'aaaa', and 'b{2,}' will match two or more consecutive b characters.
\	Escapes a metacharacter so that it is treated literally. For instance, '\+' matches a literal '+' (instead of the plus symbol having its special metacharacter meaning).
\t	Matches a tab character.
\n	Matches a newline character.
\r	Matches a carriage return character.
\w	Matches any single character classified as a "word" character (either an alphanumeric character or an underscore '_').
\W	Matches any single non-"word" character.
\s	Matches any single whitespace character (space, tab, newline).
\S	Matches any single non-whitespace character.
\d	Matches any digit character. This switch is equivalent to the character set '[0-9]'
\D	Matches any non-digit character.
\b	A "zero-width" matching assertion which matches any "word boundary".
\B	A "zero-width" matching assertion which matches any non-"word boundary".

Find files x days old and delete them

#!/bin/bash
find /path/to/files/ -type f -name '*.jpg' -mtime +10 -exec mv {} /path/to/archive/ \;
find /path/to/archive/ -type f -name '*.jpg' -mtime +30 -exec rm {} \;
  • First part is the path where your files are located. Don’t use wildcard * if you have a lot of files because you will get Argument list too long error.
  • Second part -type is the file type f stands for files
  • Third part -name is limiting *,jpg files
  • Fourth part -mtime gets how many days the files older than will be listed. +30 is for files older then 30 days.
  • Fifth part -exec executes a command. In this case mv is the command, {} gets the filelist, path where to move the files and \; closes the command

Find files over a certain size

$ find . -type f -size -4G

You can use size switch for other formats, such as

  • `c’ for bytes
  • ‘w’ for two-byte words
  • `k’ for Kilobytes
  • `M’ for Megabytes
  • `G’ for Gigabytes

Find Directory sizes

du -h --max-depth=2 | sort -hr