Sep 12, 2011

How to rename multiple file extensions with bash

Imagine you have some logfiles with .txt extensions and you want them all to be .log files.
You just need to type this within the logfile directory:

for file in *.txt; do mv $file ${file%.txt}.log; done


There many situations where renaming many files very easly is interessting.

Feb 5, 2011

How to do something 10 times with bash

There is a command called seq (Sequence).
seq takes three parameter. The first parameter is the startnumber. The second is the step the start number will increment with until it reaches the third parameter. The endnumber.

seq 1 1 10 gives an array like: 1 2 3 4 5 6 7 8 9 10
seq 1 2 5 gives you: 1 3 5

If you want to do something 10 times you can use a for loop:

for i in `seq 1 1 10`
do
echo "hello $i"
done

Just play a little bit around with it.

Feb 4, 2011

Some basic linux commands 1

First steps with linux shell.


Path

In linux there absolute and relative path.
An absolute path is from the root directory /. If you are in /tmp and want to do something with /home/myuser the absolute path is /home/myuser.
A relative path is the path from your current position. For example ../home/myuser


ls

With ls you can list (LiSt) the current directory. This basic tool shows you its content.
You could use the -l parameter to show the files with some informations like size or owner.
If you append the -a parameter you could see the hidden files. (Files and directories that start with a dot: .hidden)
If you want to use both parameter you could do:
ls -la or ls -l -a or ls -al or ls -a -l


pwd

pwd shows the full path of your current directory


cd(Change Directory)

With cd you could change your current directory.
cd ../ or cd /home/myuser


rm (ReMove)

With rm you could delete files. rm /path/to/file.
If you want to delete a directory you have to use the -R parameter.
rm -R /tmp/delete


mv (MoVe)

With the mv command you could move or rename a file.
mv /tmp/old /tmp/new


cat

The cat command allows you to view the content of a file.
cat /etc/passwd

How to make backup's with rsnapshot

Rsnapshot is a very easy to use backup system for linux.
There just a view steps to make it working.

In this scenerio we have a fileserver called fileserver.mydomain and a USB backup drive mounted at /opt/backupdrive


Installation

On Ubuntu or Debian just type:
apt-get update && apt-get install rsnapshot -y


Configuration

There is a main config file located at /etc/rsnapshot.conf
You need to change some lines in this file:

Around line 27 you have to change the snapshot_root to /opt/backupdrive
Around line 102 you have to set some intervals:
#interval    hourly 24
interval    daily 7
interval    weekly 4
interval    monthly 12

You could activate hourly intervals but it costs to much resources for a fileserver to make a hourly backup.

Now you can setup the backup sources. Around line 203 you can enter some line like:
backup /home/    fileserver.mydomain/
backup /etc/    fileserver.mydomain/
backup /var/www/    fileserver.mydomain/
backup /opt/stuff/    fileserver.mydomain/

These lines describe what rsnapshot has to backup.
(Example: backup /home/ directory on fileserver)

Now rsnapshot is prepared to backup some files.


Usage

You surely want rsnapshot to backup in some intervals so you don't have to do that manuel.
This is done by cronjobs. Just open /etc/crontab with the editor of your choice and add:

0 18 * * 1 root rsnapshot daily
0 18 * * 2 root rsnapshot daily
0 18 * * 3 root rsnapshot daily
0 18 * * 4 root rsnapshot daily
0 18 * * 5 root rsnapshot daily
0 18 * * 6 root rsnapshot daily

0 20 * * 7 root rsnapshot weekly
0 22 1 * * root rsnapshot monthly


Restore

If you want to restore some data you just need to look at your backupdrive.
Look for example at /opt/backupdrive/daily.0/fileserver.mydomain/.
There your files.

Solve every Windows problem

First reboot.
If that doesnt work give WUPI a try.
Its a free diskless Ubuntu Desktop installer.
You can install Ubuntu from Windows and you can have both: Windows and Linux installed as a dual boot system.
This not really solves every Windows problems but it helps to avoid some issues. ;-)
By the way a reboot solves most Windows problems.


Feb 3, 2011

Better searching with Google

According to the statistics at Wikipedia: 80% of web searches are made via Google.
Anyone can type a few words in a text box and click "Google Search".
But there are some ways to optimize search!



How can you "improve your search" 

If your "search term" is an objective term, you should try to keep your item as objectively as possible. ;-)
The search result for the search: "hay fever" will be much more objective than the search for "I think I am allergic to birch."
If you want factual information. But sometimes you wish to be "inspired".
The search: "Introduction contrast 'is not nearly so funny deliver results as the search for " jokes ".

With this method you are able to influence the "direction " of search results.

Another very effective method that are "qoutes".
With them, you can change the weight of search terms.
Just try:
How can I improve my search?
and:
How can I "improve" my "search"?

You will notice that the "quoted strategie" provides better search results.
With quotes you tell the search engine that "the term" must appear within the results.



Search on "Site"

Google offers a variety of operators, which help you influence your search.
Where do you put that operator in the query doesn't matter.


site:

"Where on that page can i find foobar?"
The "site:" operator can solve this problem easily.
The search for "Do not be evil site:http://www.google.com" delivers relevant search results.


cache:

With the cache: operator you can look at the last saved version of a page.
cache:google.de


(plus) + :

Yes, there is a + operator.
Sometimes you'd like to ensure a particular conjunction involved within a search.
If you want to search a conjuction you can put a + in front of a word.

For example:
You +and me


related:

The "related:" operator can look for something similar websites.
Just try it:
related:google.de


link:

The "link:" operator can show you sites re- linking on your "search site".
A very interesting operator wich helps you to find out who have a link to your site ;-)
link:selfhtml.de



Features

In addition to the Operators, Google also offers various functions to facilitate the search.
Most functions has to be the first search term in the google textbox
Function_Name Search
But there are exceptions.
everybody knows the spelling correction. ;-)


Weather

The
Weather searchterm
Function shows the weather for a specific region, with a 3-day preview
The search term can be both: a postcode or the name of a region.


Calculator

You can solve some calculation with google's search engine
For example:
21 +2
or
23 + (44 * 67-9 * (21 +2)) - (2 * 1382)


Cinema

With
Cinema Search
you can see the region's cinema program.
The search term could be both: region, or zip code.
For example:
cinema Paris


Converting units

With Google search, most compatible units can be converted.

Grammar:
Unit number in unit

For example:
1 dollar in euro
or
1 barrel to liter


Google Alert

With  Google Alert you coudl be notified if new search results appear via email or RSS feed.
You just type the keyword and your email address and when a new item appears, you will get an email a preview and a link to the page where your keyword appears. If you want to be up to date you could use this service.
Google Alerts can be found here: http://www.google.com/alerts


Google Desktop

Google Desktop is an extension of Google search, where you can index and search your PC and search on it as a normal search on Google.
Google Desktop connects to the indexed data of your Google Account. You can browse search items on  your home computer from work! Chat logs and HTTPS connections are disabled by default.
There are several plug-ins for Google Desktop, which allows to index a lot of different filetypes. You can use widgets with Google Desktop, as they are known for Windows Vista.
Google Desktop is available for Windows, Linux and Mac on following page: http://desktop.google.com

The methods show hopefully illustrate how the search with Google can be made much more effective.

Google search operators
Google search functions

With linux everything is possible...

How to SSH login without password

If you want to connect to a remote machine via SSH and without password you have to setup a public key.
You need to generate a public and a private key.

Lets say your username is linuxuser. On your local system and your remote system.


After login you need to run:
ssh-keygen -t rsa -b 4096
-t rsa is the encryption method and -b 4096 tells ssh-keygen to use 4096 bit for keys.

After that you have to ensure that your keys are located at /home/linuxuser/.ssh/. In most cases there called
id_rsa and id_rsa.pub.

Now you need to put the public key into the authorized_keys file of your target host. (lets call the host: linuxhost)

First Transfer the key: scp /home/linuxuser/.ssh/id_rsa.pub linuxuser@linuxhost:/home/linuxuser/id_rsa.pub .
Then login to the remote host (last time with password.. hopefully :-)) and enter:
cat /home/linuxuser/id_rsa.pub >> /home/linuxuser/.ssh/authorized_keys; rm /home/linuxuser/id_rsa.pub

The last step is to ensure that your private key permissions are 600. ( chmod 600 /home/linuxuser/.ssh/id_rsa )

Now you can just login to your remote machine :-)
ssh linuxhost (maybe you have to use the -i parameter: ssh -i ~/.ssh/id_rsa)

Thats it!

How to run a shell script after login

You can run every shell script you like if you put it into the /etc/profile file.

After login most linux systems run the /etc/profile script. (many others too, but this one would do it)

If you want a script to run for a single user only, you can use the .profile file in users home directory
(/home/username/.profile)

If your script is called afterlogin.sh and located at /opt/afterlogin.sh you just need to add:
bash /opt/afterlogin.sh or /opt/afterlogin.sh (if execute bit is set! chmod +x /opt/afterlogin.sh)
to your /etc/profile or /home/username/.profile file.

After login your script will execute now.

This is all about...

This Blog is about Linux, Google, Cloud Computing and SaaS!

Any questions or ideas? Leave a comment!