Convert Microsoft to LINUX: Bash


There are numerous good Bash tutorials on the web, please look at this one for starters:   Bash Tutorial
Be sure to read both parts.
Here is another tutorial:   More Advanced Bash Tutorial
Note Bash stands for Bourne Again Shell . It is just an updated version of what you will find as the default on many Linux and Unix systems. Today it seems many people prefer Perl, as it is a lot more powerful and can be more structured, but you will find this old relic quite useful. As outlined in the tutorials you need to pay close attention to your syntax. Note, the use of the symbol 2>&1 which means that 2 the standard error output is being sent to 1 the standard output . This is useful for example in searching for a file:
find ./ -name passwd > output.txt 2>&1
The output.txt file will contain both the logged error messages and the results you are looking for in one file.

Most Essential Backup Scripts for any Website

I assume your website has mysql on it or some kind of db which has this commmand to dump all the databases:
mysqldump -u username -h localhost --all-databases | gzip -9 > db_backup_ALL_`date +"%Y%m%d"`db.sql.gz
Note above is all one line the username is the Admin/root user for your website that has the power to run this script.

tar -czf backup_`date +"%Y%m%d"`.tar.gz /home/username/public_html
The username here is the directory you use to get the public_html you want to backup. Note, I nicely gzipped everything to make it smaller. I have set up as a cron job to make this run once a week for both of these scripts. See cron under commands page.

Here are some of my favorite scripts which I use:

OK, I bet that last script was a little bit much for you. Let us review it in little pieces first.
ls -l . | grep "^-"
will give you a grep on all the entries that are files, because there ls list entry begins with a dash That is what the "^-" means which you know already from perl pattern matching. Now you will take these lines and further only print the fifth column, that is
awk '{print $5}'
which will give you a number which is the far entry in the ls listing denoting the file size. Now because this whole long line is enclosed in parentheses with a $ sign in front, we are performing it in a shell. i.e.:
ls -l . | grep "^-" | awk '{print $5}'
will give us an array of file size values.
81
179
2

This is similar to Perl. You are done when you have added up all the entries in the file size array. You can see this with the debug output:

victor@victor-900HA:~$ ./filesize.sh
... first it will reprint the script ...
Next it will parse the command we gave it in the parantheses loop:
++ ls -l .
++ awk '{print $5}'
++ grep '^-'
+ for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')'
+ let totalsize=+81
+ for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')'
+ let totalsize=81+179
+ for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')'
+ let totalsize=260+2
echo "Total file size in current directory: $totalsize"
+ echo 'Total file size in current directory: 262'
Total file size in current directory:262

For more examples, please see this great little book by Ramesh 101 Linux Hacks online.

For a final example, use the tee command which is very powerful. It is like a Tee in plumbing giving you a way of siphoning off a pipe and seeing what is there. Here is an example:
awk -F: '{ print $1}' /etc/passwd | sort | tee users_list.txt | lp
Note the awk command takes as an input with the -F parameter the file /etc/passwd and prints the first column then sorts it. The Tee is inserted and the result is stored in the file users_list.txt. The sorted list is also piped, that is printed straight to the printer which is designated by lp
You can insert the tee anytime to have a copy of what is happening at this particular point in the pipe. It helps a lot in debugging.

Some System Scripts

  1. showstat.sh   Get basic statistics for system including users logged in, date, time and uptime.
  2. rsync.sh backup   Note, only root can run this and a logout of the shell is forced at the end.
  3. modified rsync.sh backup   Note, only root can run this and a logout of the shell is forced at the end.This script is launched from the Fedora 20 partition to backup the Debian 7.1 partition.

Advanced Guide to Bash

Here is one last guide   , I found that is extremely thorough, written by Mendel Cooper.
Enjoy !