How to redirect a domain name using .htaccess

If your domain name is changed for some reason and you want users/search engines to redirect to new Location then add the below code in .htaccess file of your website.

Example:

If you want to redirect http://olddomain.com to http://www.newdomain.com

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com%{REQUEST_URI} [R=301,L]
</IfModule>

After you add above code in .htaccess file it will as also redirect links.
Example:

http://olddomain.com/archive/2019 will redirect to http://www.newdomain.com/archive/2019

How to Hide Apache Version from HTTP Header

You can check the HTTP header of your server by using a simple curl command.
In HTTP header you will see on what version Apache is running.

$ curl -I  http://justgeek.in

You can see that your server is running with Apache 2.4.18 server.

 HTTP/1.1 200 OK
 Date: Mon, 2 December 2019 11:36:14 GMT
 Server: Apache/2.4.6 (CentOS)
 Last-Modified: Thu, 20 Oct 2016 05:30:08 GMT
 Accept-Ranges: bytes
 Content-Length: 11321
 Vary: Accept-Encoding
 Content-Type: text/html

To hide Apache Version, you will have to edit your Apache configuration file and add following variables.
Restart your apache after making change.

ServerTokens Prod
ServerSignature Off

Use the same http command again to check HTTP Header

$ curl -I  http://justgeek.in

Now you can see that header is only showing that Apache is running, but no version or OS details available there.

 HTTP/1.1 200 OK
 Date: Mon, 2 December 2019 11:36:14 GMT
 Server: Apache
 Last-Modified: Thu, 20 Oct 2016 05:30:08 GMT
 Accept-Ranges: bytes
 Content-Length: 11321
 Vary: Accept-Encoding
 Content-Type: text/html

Create a large file Quickly on Linux servers.

How can I quickly create a large file on a Linux system?

Normally dd command creates a large file but it reads from /dev/zeor and then writes to drive which can take a longer time which you need a very large file.
If you don’t care what should be the contents and just want to add the file quickly you can use fallocate command to do this.

Below command will create 10GB of file in no time. You can modify it as per your needs

# fallocate -l 10GB /justgeek
[root@server /]# du -sh justgeek
9.4G    justgeek
[root@server /]#

There are other options you can use for fallocate, you can try help command to explore more.

[root@server /]# fallocate --help

Usage:
 fallocate [options] <filename>

Preallocate space to, or deallocate space from a file.

Options:
 -c, --collapse-range remove a range from the file
 -d, --dig-holes      detect zeroes and replace with holes
 -i, --insert-range   insert a hole at range, shifting existing data
 -l, --length <num>   length for range operations, in bytes
 -n, --keep-size      maintain the apparent size of the file
 -o, --offset <num>   offset for range operations, in bytes
 -p, --punch-hole     replace a range with a hole (implies -n)
 -z, --zero-range     zero and ensure allocation of a range
 -x, --posix          use posix_fallocate(3) instead of fallocate(2)
 -v, --verbose        verbose mode

 -h, --help           display this help
 -V, --version        display version

For more details see fallocate(1).
[root@server /]#

Find Command examples

Find Command is one of the most important and frequently used command utility in Unix like operating systems. It is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.

Find all file whose name is justgeek.txt

# find -name justgeek.txt

Find all files whose name starts with justgeek

# find -name justgeek.*

Find all Directories whose name is justgeek

# find -type d -name justgeek

NOTE: you can use iname instead of name if you do want to match exact case.

Find all Directories whose name is justgeek in /home

# find /home -type d -name justgeek

Find all html file in /home folder

# find /home -type f -iname "*.html"

Find all files whose name ends with .tmp in /home folder and delete them.

# find /home -type f "*.tmp" -exec rm -fv {} \;

Find Empty files

# find /tmp -type f -empty

Find empty directories

# find /tmp -type d -empty

Find all files whose size is more than 500 MB

# find /home/justgeek -type f -size +500M

Find particular files types with size more than 100 MB and delete it.

# find / -type f -name message.logs -size +100M -exec rm -fv {} \;

Find files older than 24 hours and delete them

# find /tmp/logs -type f -mmin +1440 -exec rm -fv {} \;

Find all files older then 60 Mins and move them to a different folder

# find /tmp/logs*tar -mmin +60 -exec mv {} /home/backup \;

Script to format server list into ansible format.

Script to format list of servers into ansible format.
Example:
if you have a text file which has list of servers as below.

app.server.com
app2.server.com
web1.server.com
web2.server.com
[app]
app.server.com
app2.server.com
[web]
web1.server.com
web2.server.com

How to use it ?
create a file generateInventory.sh with the contents below and it the same directory create a file named raw_inventory.txt with list of the servers and run the script using ./generateInventory.sh and you will get ansible format Inventory file will generated.

# Script to format list of servers into ansible format.
file=raw_inventory.txt
 last=''
 while read nameX
 do label="${nameX%%[.0-9]*}"
    if [[ "$label" != "$last" ]]
    then echo "[$label]" # or printf "\n$label\n" for a separator line
         last="$label"
    fi
    echo "$nameX"
 done < $file > Inventory