Check http headers with wget

If you want to see the http headers from your shell, you can do it with:

 

wget --no-check-certificate --server-response --spider https://yourwebsite.something

The result would be something like:

[bash]
Spider mode enabled. Check if remote file exists.
–2014-02-07 11:13:33– https://yourwebsite.something/something
Resolving yourwebsite.something… 129.177.5.226
Connecting to yourwebsite.something|129.177.5.226|:443… connected.
HTTP request sent, awaiting response…
HTTP/1.1 301 Moved Permanently
Date: Fri, 07 Feb 2014 10:13:33 GMT
Server: Apache
Location: https://yourwebsite.something/something/
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
Location: https://yourwebsite.something/something/ [following]
Spider mode enabled. Check if remote file exists.
–2014-02-07 11:13:33– https://yourwebsite.something/something/
Connecting to yourwebsite.something|129.177.5.226|:443… connected.
HTTP request sent, awaiting response…
HTTP/1.1 301 Moved Permanently
Date: Fri, 07 Feb 2014 10:13:33 GMT
Server: Apache
X-Powered-By: PHP/5.3.3
X-Content-Type-Options: nosniff
Vary: Accept-Encoding,Cookie,User-Agent
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: private, must-revalidate, max-age=0
Last-Modified: Fri, 07 Feb 2014 10:13:33 GMT
Location: http://yourwebsite.something/something/index.php/Hovudside
Connection: keep-alive, Keep-Alive
Keep-Alive: timeout=15, max=100
Content-Type: text/html; charset=utf-8
Location: http://yourwebsite.something/something/index.php/Hovudside [following]
Spider mode enabled. Check if remote file exists.
–2014-02-07 11:13:33– http://yourwebsite.something/something/index.php/Hovudside
Connecting to yourwebsite.something|129.177.5.226|:80… connected.
HTTP request sent, awaiting response…
HTTP/1.1 200 OK
Date: Fri, 07 Feb 2014 10:13:33 GMT
Server: Apache
X-Powered-By: PHP/5.3.3
X-Content-Type-Options: nosniff
Content-language: nn
Vary: Accept-Encoding,Cookie,User-Agent
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: private, must-revalidate, max-age=0
Last-Modified: Tue, 14 Jan 2014 11:52:09 GMT
Connection: keep-alive, Keep-Alive
Keep-Alive: timeout=15, max=100
Content-Type: text/html; charset=UTF-8
Length: unspecified [text language="/html"][/text]
Remote file exists and could contain further links,
but recursion is disabled — not retrieving.
[/bash]

rdesktop from Linux

You can easily access your Windows computer desktop from a Linux computer with the tool rdesktop:

[bash]rdesktop -u username -d domainname -k no -f -z -p- host[/bash]

where

-k no
specify that the keyboard is norwegian

-z
to compress

-p-
prompt for password before connecting

-f
full screen (to quit full screen press CTRL-ALT-ENTER)

host
is either the hostname or IP adresse of your windows computer.

Here is a screen shot of my Linux mint desktop, with a rdesktop (without the full screen option -f) to my Windows computer at work:

screen

Colours in nano

I am writing most of my bash script with nano editor. If one want to get colours in nano, on can do:

[bash]
locate *.nanorc
[/bash]

which will give a list of what is available on your system:
/usr/share/nano/html.nanorc
/usr/share/nano/java.nanorc
/usr/share/nano/man.nanorc
/usr/share/nano/mutt.nanorc
/usr/share/nano/nanorc.nanorc
/usr/share/nano/perl.nanorc
/usr/share/nano/python.nanorc
/usr/share/nano/ruby.nanorc
/usr/share/nano/sh.nanorc

Now, you can do:

[bash]
nano -w ~/.nanorc
[/bash]

and add your prefered styling.

Since I am happy with bash, I would add:
include /usr/share/nano/sh.nanorc
to my file.
Read more here:
http://askubuntu.com/questions/90013/how-do-i-enable-syntax-highlighting-in-nano

See blow how it looks like on my computer:

colours in nano

The text below is what I wrote a couple of years ago, but I keep just in case.

 

Some might remember the editor ‘pico’, today most people know it as ‘nano’.

Have you missed sometime to have colour highlighting when editing bash and php scripts with nano? Here are two interesting projects:

https://github.com/craigbarnes/nanorc

and

https://github.com/alekstorm/nanorc

nano editor with colours

dig

Sometimes you like to know some more what is behind a web-address or a domain-name. Then the linux command ‘dig’ can be helpful:

dig www.interstingwebsite.com soa

Example:

dig www.google.com soa

which gives:

 ]$ dig www.google.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6_3.5 <<>> www.google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 18148
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 4, ADDITIONAL: 4

;; QUESTION SECTION:
;www.google.com.                        IN      A

;; ANSWER SECTION:
www.google.com.         8       IN      A       74.125.143.106
www.google.com.         8       IN      A       74.125.143.147
www.google.com.         8       IN      A       74.125.143.99
www.google.com.         8       IN      A       74.125.143.103
www.google.com.         8       IN      A       74.125.143.104
www.google.com.         8       IN      A       74.125.143.105

;; AUTHORITY SECTION:
google.com.             37733   IN      NS      ns3.google.com.
google.com.             37733   IN      NS      ns1.google.com.
google.com.             37733   IN      NS      ns2.google.com.
google.com.             37733   IN      NS      ns4.google.com.

;; ADDITIONAL SECTION:
ns1.google.com.         133889  IN      A       216.239.32.10
ns2.google.com.         133889  IN      A       216.239.34.10
ns3.google.com.         133889  IN      A       216.239.36.10
ns4.google.com.         133889  IN      A       216.239.38.10

;; Query time: 1 msec
;; SERVER: 129.177.30.3#53(129.177.30.3)
;; WHEN: Fri Nov 30 23:48:01 2012
;; MSG SIZE  rcvd: 264


Find your files

In Linux you can use the command ‘find’ to find files and folders and then also do something with them on your system.

Find all files that ends with the .php extension in the folder /var/www/:
find /var/www/ -type f -name '*.php'

This will give you a list with a lot of files.
So, what? What is next?

Well, you can search for words in those files:
find /var/www/ -type f -name '*.php' | xargs grep 'foobar'

that will give files with file extension .php that has the word ‘foobar’ in it.

 

find /var/www/ -type f -iname '*.php' | xargs grep 'foobar'
Does the same right? Almost, only that it also search for also files with .PHP (iname means none-case-sensitive)

 

find /var/www/ -type d -name 'tmp' -exec ls -ald {} \;
will find all the folders under /var/www/ with the name “tmp” and list the information out about these folder.
Here is my output from my server:
find / -type d -name 'tmp' -exec ls -ald {} \;

drwxrwxrwt. 11 root root 4096 Oct 22 21:54 /tmp
drwxrwxrwt. 2 root root 4096 Oct 22 16:33 /var/tmp
drwxrwx–T. 2 root lp 4096 Sep 5 10:28 /var/spool/cups/tmp
drwxr-xr-x 2 root root 4096 May 9 10:30 /backup/db/tmp

Let us get to something more interesting:

Find all files that has the string rocket or ROCKET in their filename, and move them to the folder /tmp:
find / -type f -iname '*rocket*' -exec mv {} /tmp/ \;

Git

Git is a tool that can help you track changes in your files, specially when you share your files with your friends on a Linux server.

Here is a very simple description:

log into your server
ssh foobarserver

go to your folder where you want to track files
cd to/your/folder/for-git-tracking/test/

create a local git repository
git init

Add your files to the local repo
git add myfile.sh

or add more files at once
git add .

Commit your changes to the repo, give a message -m, so that your friends can understand what you have done
git commit -m "myfile.sh: first commit" myfile.sh

or commit all files in current directory (the character “.” is the directory where your files are.)
git commit -m "My files, snapshot" .

Now, start editing your file:

vim myfile.sh

or

nano -w myfile.sh

After saving your changes, do:

git commit -m "myfile.sh: I have changed an important part in this file" myfile.sh

or
git commit -m "My files, here is my explanation of the changes" .

Later on, you can do:

to see the log and the changes done
git log

did you forget to commit some files, or did someone change something?
git status

Using unix command ‘du’, disk usage

Sometimes you like to know which folders are using up all your space on your Linux server.

This command will sum up the disk usage per folder, and print the result:

du -m --max-depth=10 / | sort -r -n | head -10

– m (display in megabyte)
– max-depth=10 (folder depth? Should be plenty, depends on your system)
– sort -n -r (sort it by numbers, and reverse it)
– head -10 (only show the 10 top folder with most data.)

result on “myserver”:

6268    usr
5330    var
3414    var/www
2715    backup
2714    backup/postgresql_dumps
2399    usr/share
2192    var/www/applications
2126    var/www/applications/logonlogoff
2034    var/www/applications/logonlogoff/cache
1691    usr/local