Category Archives: Linux

What does > /dev/null 2>&1 mean?

What does > /dev/null 2>&1 mean?

I remember being confused for a very long time about the trailing garbage in commands I saw in Unix systems, especially while watching compilers do their work. Nobody I asked could tell me what the funny greater-thans, ampersands and numbers after the commands meant, and search engines never turned up anything but examples of it being used without explanation. In this article I’ll explain those weird commands.

Here’s an example command:

wibble > /dev/null 2>&1

Output redirection

The greater-thans (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.
Standard in, out, and error

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.

Given that context, you can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!

How to Upgrade Python in CentOS

How to Upgrade Python in CentOS

This guide will teach you how to upgrade from Python 2.4 to Python 2.7.

First you need to check your current Python Version:

python -V

If it returns 2.4 then execute the following steps:

wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar -zxvf Python-2.7.3.tgz
cd Python-2.7.3
yum install gcc
./configure –enable-shared
make
make altinstall

The above method will install Python 2.7 without overwriting the original 2.4 version of Python in CentOS (NOTE: Overwriting 2.4 completely with 2.7 is bad!)

Basically the “make altinstall” will install Python 2.7 to “/usr/local/bin/python2.7″

To check execute: “python2.7 -V” and it will show the 2.7 version while executing “python -V” will result to the 2.4 version.

So how are we going to configure CentOS to use Python 2.7 as default instead of Python 2.4?

Easy. Just follow the following:

1. Add Python 2.7 lib to system lib:

ln -s /opt/python2.7/lib/libpython2.7.so /usr/lib
ln -s /opt/python2.7/lib/libpython2.7.so.1.0 /usr/lib

2. Create important links and cache

/sbin/ldconfig -v

3. Create a symbolic link:

ln -s /opt/python2.7/bin/python /usr/local/python

4. Check version by executing “python -V”. If you still see old version proceed to step 5.

5. Modify /etc/profile (as root)

vi /etc/profile

Look for the following entry “export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC” and put the following setting “pathmunge /opt/python2.7/bin/” above it.

It should look like this:

pathmunge /opt/python2.7/bin/
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC

Then re-login system (very important), try “python -V” again.

6. (Very important last step) Make sure yum is just ok by executing “yum search php”. If it doesn’t return any errors then you are all good.

Credit:http://www.nerdsmind.com/how-to-upgrade-python-in-centos/

The Digital Text

In this post we will example the digital text and how and why it is encoded.
Denarius

Wars are often an unexpected event, and a lot of the major currency developments in the 19th and 20th centuries were due to wars. In the ancient world it was no different. The requirement to quickly gather resources required an efficient form of money.

During the Second Punic War, in 211 BC, Rome brought out the Denarius, which means ‘containing ten’ – because one silver Denarius was worth ten bronze (later copper) Asses.

During the Third Punic war, in 140 BC, Rome decided to go hexadecimal, where one silver-coloured Denarius became worth 16 copper Asses.

The silver-coloured Denarius was considered a day’s wages for a soldier. The gold-coloured Solidus varied in value but eventually stabilised by the 8th century as 12 denarii.

The Romans carried spread currency around and in Britain, the denarius became the penny but was still written as d until 1971 e.g. 5d for 5 pence.

12d made a shilling, which is the Anglo-Saxon term for the Solidus. The shilling was in the 16th century pegged to the value of a cow in Kent market.

Twenty shilling made a pound which was named after the unit of mass, a pound in cash was originally worth the value of a pound weight of silver (which is now about £300).

The pound of a unit of mass is itself Roman of course, from libra, which is why pound is shortened to lb. The pound £ sign was originally an L. 1 lb in mass is 16 ounces.

Part the deal when Britain applied to join the European Economic Community in the 1960s and 1970s, was that we got rid of all these crazy measurements and adopted metric, also known as scientific measurements, which we did eventually, to a certain extent. For example, milk, beer and cider are officially sold in units of 568 mL!

So until recently, the idea of non-base 10 measurements was completely normal.
Binary

George Boole was a theologian who was also one of the greatest mathematicians of the 19th Century.Boole understood mathematics and religion as intertwined. George Boole believed that studying mathematics would help reveal a new understanding of God.
../../../_images/george_boole.jpg

More on George Boole: http://zeth.net/archive/2007/07/19/what-is-truth-part-3-all-you-need-is-one-and-zero/

The core idea that all knowledge and thought could be reduced to two factors nothing (0) and God (1), had long been discussed, for example by the the Jesuit Gottfried Leibniz writing in the 17th Century. However, Boole had the mathematical knowledge to take the idea and build a complete system of logic around it.
../../../_images/yin_and_yang.png

Everything is either True (God – 1) or False (nothing – 0):

1 or 0 == 1
0 or 1 == 1
1 or 1 == 0
0 or 0 == 0
1 and 1 == 1
0 and 0 == 0
1 and 0 == 0
0 and 1 == 0
not 0 == 1
not 1 == 0

Everything that is not God is nothingness, everything that is something is God. God fills the nothingness but the nothingness cannot conquer God.

Any number can be represented by any sequence of bits. A bit is 0 or a 1.
Binary Decimal
0 0
1 1
10 2
11 3
100 4
101 5
110 6
111 7
1000 8
1001 9
1010 10
1011 11
1100 12
1101 13
1110 14
1111 15
10000 16

Traditionally, eight bits was called a byte (more correctly it is an octet). Four bits is a nibble.

A computer processor has lots of microscopic transistors. The CPU in my laptop (the Intel Ivy Bridge) has 1.4 billion of them. Each transistor is like a switch with an on and off state.
Hexadecimal

Binary is very low level. The first level of abstraction over binary is called hexadecimal.

In previous lecture, we looked at how and when and where computing was developed. These early computer developers choose the most efficient representation. As we mentioned earlier, until recently non-base 10 measurements were completely normal.

Hexadecimal (‘hex’ for short) is counting in base 16, here is the table from above with hex as well:
Binary Hex Decimal
0 0 0
1 1 1
10 2 2
11 3 3
100 4 4
101 5 5
110 6 6
111 7 7
1000 8 8
1001 9 9
1010 a 10
1011 b 11
1100 c 12
1101 d 13
1110 e 14
1111 f 15
10000 10 16

Now it is easy to convert any binary number to hex. You just split it up into nibbles from the right.

So this number:

11111011110

Split up is:

0111 1101 1110
7 d e

So in hex it is 7de.

What number is it in decimal? Well that is more complicated. Going from binary to decimal requires you to split the binary number up into parts:

10000000000 1024
1000000000 512
100000000 256
10000000 128
1000000 64
10000 16
1000 8
100 4
10 2

1024 + 512 + 256 + 128 + 64 + 16 + 8 + 4 + 2 = ?

So data is electrical impulses in a transistor, which represent 1 and 0, which are then hexadecimal numbers.

Now we have numbers, we can now encode characters. Each character is given a hex number.

So 41 in hex (which is 65 in decimal) is “latin capital letter A”.

There are different encodings (mapping between numbers and characters) but the only one that really matters in 2014 is called UTF-8 commonly called Unicode (although there are other forms of Unicode which did not win).

UTF-8 has room for 1,112,064 different characters and symbols which aim to represent all of the world’s languages.

The first 128 characters are carried over from an older standard called ASCII. The first 32 of these are historic control characters for controlling printers and teletype devices (remember those from a previous lecture?).

20 in hex (so 32 in decimal) is the empty space, then we get punctuation, then we get the numbers and so more punctuation etc then the letters in upper case then some more symbols then the letters in lower case etc.

This gets us to 7E (126) which is ~, and we have all of the English keyboard covered. The next 129 characters are Western European languages (German etc) and then it carries on after that through all the world’s letters and symbols.

Including some really fun stuff added to give compatibility with Japanese mobile phones:

http://www.fileformat.info/info/unicode/char/1f4a9/index.htm http://www.fileformat.info/info/unicode/char/1f302/index.htm http://www.fileformat.info/info/unicode/block/miscellaneous_symbols_and_pictographs/images.htm http://en.wikipedia.org/wiki/Emoji

So a digital text is a series of these hexadecimal numbers representing characters and symbols including spaces (20 in hex/32 in decimal) and control codes such as line breaks (0A in hex, 10 in decimal) and so on.

Here is a nice chart version of the first 127 (ASCII) characters: http://web.cs.mun.ca/~michael/c/ascii-table.html

So you can decode these characters (with some spaces added to make it simpler):

41 6e 64 20 74 68 65 72 65 66 6f 72 65 20 6e 65 76 65 72 20 73 65 6e 64 20 74 6f 20 6b 6e 6f 77 20 66 6f 72 20 77 68 6f 6d 20 74 68 65 20 62 65 6c 6c 20 74 6f 6c 6c 73 3b 20 49 74 20 74 6f 6c 6c 73 20 66 6f 72 20 74 68 65 65 2e

To make it clear that something is a hex value, it is often prefixed with 0x or x or U+.

This is as good as it far as it goes. But to make practical use of the data, just loads of text doesn’t help that much.

If we want to make a digital representation of a humanities artefact like a manuscript, we need to use a file format. Otherwise the digital text is of limited use for other scholars and software.

Credit to

http://commandline.org.uk/

Installing Django with Apache and mod_wsgi on Ubuntu 10.04

Installing Django with Apache and mod_wsgi on Ubuntu 10.04
By Kevan Stannard | Published: December 11, 2010

Step by step instructions for installing Django with Apache and mod_wsgi on Ubuntu 10.04.

PART 1 – Prepare the server

Update the server

> sudo apt-get update
> sudo apt-get upgrade

Install Apache and mod_wsgi

> sudo apt-get install apache2 libapache2-mod-wsgi

Install setup tools and pip

> sudo apt-get install python-setuptools
> sudo apt-get install python-pip

Install Django

> sudo pip install django

Create a folder for storing our sites

I’ll be placing our sites in the /srv/www directory. The /srv directory should already exist so we just need to create the /www directory

> sudo mkdir /srv/www

PART 2 – Add host entries for testing

We will set up two domains for testing the configuration
- one for testing that WSGI is working, and
- one for testing that Django is working.

My test virtual machine’s IP address is 172.16.52.130 so I’ll set up the following in my local hosts file (not on the server)

> sudo nano /etc/hosts

And add the following

172.16.52.130 djangoserver
172.16.52.130 wsgi.djangoserver
172.16.52.130 hello.djangoserver

PART 3 – Test WSGI is working

Create our wsgi test site content

> sudo mkdir /srv/www/wsgi
> sudo nano /srv/www/wsgi/app.wsgi

And add the content

def application(environ, start_response):
status = ’200 OK’
output = ‘Hello World!’

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]

Create a new apache site

> sudo nano /etc/apache2/sites-available/wsgi

And add the content

ServerName wsgi.djangoserver
DocumentRoot /srv/www/wsgi


Order allow,deny
Allow from all

WSGIScriptAlias / /srv/www/wsgi/app.wsgi

And activate the site

> sudo a2ensite wsgi
> sudo /etc/init.d/apache2 reload

Then open your web browser and browse to

http://wsgi.djangoserver

You should see a ‘Hello World!’ message
PART 4 – Test Django is working

Create a new Django site

> cd /srv/www
> sudo django-admin.py startproject hello

Create a wsgi file for the site

> sudo mkdir /srv/www/hello/apache
> sudo nano /srv/www/hello/apache/django.wsgi

And add the content

import os
import sys

path = ‘/srv/www’
if path not in sys.path:
sys.path.insert(0, ‘/srv/www’)

os.environ['DJANGO_SETTINGS_MODULE'] = ‘hello.settings’

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Create a new apache site

> sudo nano /etc/apache2/sites-available/hello

And add the content

ServerName hello.djangoserver
DocumentRoot /srv/www/hello


Order allow,deny
Allow from all

WSGIDaemonProcess hello.djangoserver processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup hello.djangoserver

WSGIScriptAlias / /srv/www/hello/apache/django.wsgi

And activate the site

> sudo a2ensite hello
> sudo /etc/init.d/apache2 reload

Then open your web browser and browse to

http://hello.djangoserver

You should see the Django default installation message.
Notes

NOTE 1 – Running in daemon mode

Our test Django site is configured to run in daemon mode – because of these two lines:

WSGIDaemonProcess hello.djangoserver processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup hello.djangoserver

So if we modify the code then rather than restarting apache we can just touch the wsgi file and the changes will be picked up:

> sudo touch /srv/www/hello/apache/django.wsgi

NOTE 2 – Specify the application module name

It appears to be a good idea to specify the application module when specifying the DJANGO_SETTINGS_MODULE. So rather than writing this:

os.environ['DJANGO_SETTINGS_MODULE'] = ‘settings’

We should write this:

os.environ['DJANGO_SETTINGS_MODULE'] = ‘hello.settings’

Useful Commands

Error log file

If you get errors, check the apache log file

> tail /var/log/apache2/error.log

Test using development mode

If your app does not seem to be working using wsgi, then check if it is working via the development server.

> cd /srv/www/hello
> python manage.py runserver 0:8080

The in your web browser go to

http://hello.djangoserver:8080

Reference:

http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/

http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/

Relaying Postfix mails via smtp.gmail.com

sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

vim /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

vim /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 USERNAME@gmail.com:PASSWORD

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

/etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

sudo /etc/init.d/postfix reload

echo “Test mail from postfix” | mail -s “Test Postfix” you@example.com

Reference:

https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/

Build & Install CMake 2.8.10.2 in CentOS6.3

Download CMake 2.8.10.2 from http://www.cmake.org/cmake/resources/software.html#latest
cd /usr/local
tar -zxf cmake-2.8.10.2.tar.gz
rpm -qa|grep ncurses-devel // to check if you installed ncurses-devel, if not, please install this firstly
rm -f cmake-2.8.10.2.tar.gz
cd cmake-2.8.10.2
./configure
make
make install
cd ..
mv cmake-2.8.10.2 cmake

vi /etc/profile

Put below 2 lines into the bottom line of /etc/profile:

PATH=/usr/local/cmake/bin:$PATH

export PATH

source /etc/profile
echo $PATH // to check if cmake in the PATH.
cmake –version // to check if the version is right, should be cmake version 2.8.10.2

DONE~~~

https://github.com/jackywei/HOW-TO-BUILD-HHVM-WiKi/wiki/Build-&-Install-CMake-2.8.10.2-in-CentOS6.3

How to upload file with wput

wput -B -u . ftp://user:password@ftphost/

wput -B -u yourdirecotry ftp://user:password@ftphost/

 

download with wget

wget -r -l 0 -nH ftp://user:pass@ftp.server.com

Option     Description
user     FTP username
pass     FTP password
ftp.server.com     IP address or domain name of an FTP server
-r, –recursive     Recursive retrieving
-l, –level     Maximum recursion depth (0 = unlimit)
-nH, –no-host-directories     Disable generation of host-prefixed directories

 

Reference:

http://www.shellhacks.com/en/Backup-Site-Recursively-from-FTP-with-Wget

Site Lists

http://www.shellhacks.com/en/HowTo-Restore-MySQL-Database-From-the-Command-Line

http://webcheatsheet.com/sql/

http://www.shellhacks.com/en/Categories/Linux-Apps

Linux / Unix ncftp: Upload Directory Tree To Remote FTP Server Recursively

Install ncftp client
sudo apt-get install ncftp
yum install ncftp

ncftpput ftp-host-name /path/to/remote/dir /path/to/local/dir
ncftpput -options ftp-host-name /path/to/remote/dir /path/to/local/dir

You can pass the password with -p option:
ncftpput -R -v -u “username” -p “passwordHere” ftp.nixcraft.biz /nixcraft/forum /tmp/phpbb

You can use port number 2021 instead of the default FTP service port # 21 as follows:
ncftpput -R -v -u “username” -p “passwordHere” -P 2021 ftp.nixcraft.biz /nixcraft/forum /tmp/phpbb

 

 

#!/bin/bash
# Shell script to copy all files recursively and upload them to
# remote FTP server (copy local all directories/tree to remote ftp server)
#
# If you want to use this script in cron then make sure you have
# file pointed by $AUTHFILE (see below) and add lines to it:
# host ftp.mycorp.com
# user myftpuser
# pass mypassword
#
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# ————————————————————————-
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ————————————————————————-

FTP=”/usr/bin/ncftpput”
CMD=””
AUTHFILE=”/root/.myupload”

if [ -f $AUTHFILE ] ; then
# use the file for auth
CMD=”$FTP -m -R -f $AUTHFILE $myf $remotedir $localdir”
else
echo “*** To terminate at any point hit [ CTRL + C ] ***”
read -p “Enter ftpserver name : ” myf
read -p “Enter ftp username : ” myu
read -s -p “Enter ftp password : ” myp
echo “”
read -p “Enter ftp remote directory [/] : ” remotedir
read -p “Enter local directory to upload path [.] : ” localdir
[ "$remotedir" == "" ] && remotedir=”/” || :
[ "$localdir" == "" ] && localdir=”.” || :
CMD=”$FTP -m -R -u $myu -p $myp $myf $remotedir $localdir”
fi

$CMD

 

 

credit to:

http://bash.cyberciti.biz/backup/copy-all-local-files-to-remote-ftp-server-2/

http://baptiste-wicht.com/posts/2011/06/upload-files-to-ftp-using-bash.html