Run a HTTP server in any folder (Mac)

Use this one-liner to serve any directory on Mac on the local network. Navigate to the directory in the terminal first.

python -m SimpleHTTPServer 8000

You can also leave the port out so that it uses the default port 80.

Now, in a browser, type in the below to access the directory files.

http://localhost:8000

Use CTRL+C to end.

If other devices on the local network want to access, replace localhost with your local ip e.g. below:

http://192.168.0.14:8000

Screen Shot 2016-07-16 at 23.15.23

Most frequent Linux tools  – Quick start guide

This quick one minute video demonstrates how to use some of the most frequently used tools in Linux or Mac. The processes are elaborated below as this video does not contain any audio. This quick video is from my Mac. Leave comments or questions at the bottom.

Tools demoed:
 vi
 less
 cat
 grep
 awk
The above video demonstrates the following:
  • How to create a directory called tutorial in Linux – the mkdir command
    mkdir tutorial
  • Navigate into the directory
    cd tutorial/
  • How to get a list of contents in directory
    ls
  • How to create file named file.txt using vi or vim and save it
    vi file.txt

    to open editor, then press ‘i’ to start inserting text and ESC to come out of writing mode; and to save and quit, use ‘:wq’ and ENTER!

  • How to quickly view the file without it hogging the screen permanently using less –
    less file.txt

    and then ‘q’ to exit viewing

  • How to view the file in a manner that it stays on the terminal screen using cat
    cat file.txt
  • How to find the string ‘make’ in the file or every occurrence of this string using grep
    grep make file.txt
  • How to use awk to print only certain strings from a line in the file – basically, we want to print certain strings from a line, so its clever to choose a string that is only in that line and grep it first so that we have the line alone; then pipe (|) it to awk to filter the strings using – In this case, awk prints the 4th, 8th, 9th and 10th string from the grep result as output.
    grep ‘abracadabra’ file.txt | awk ‘{print $4,$8,$9,$10}’
 Screen Shot 2016-06-12 at 13.19.14.png
 
In addition to these tools, there are various other tools used (e.g. sed, tail etc) that come in handy with these tools. Please leave any comments or questions. Thanks for visiting.

Fix ‘cannot write’ homebrew error on Mac

I have been using Mac terminal to get a MAMP setup done lately and I kept running back in to and error I spent a lot of time trying to fix a while ago…sigh. This comes up when you try to install or update with brew.

Irshads-MBP:~ Ishy$ brew upgrade
Error: Cannot write to /usr/local/Cellar

Fix it this way:

sudo chown -R $USER /usr/local

Note: You can replace $USER to root as homebrew is better set with root.

brew2

How to stream videos from your Mac or Linux on Smart TVs

Okay, since getting a 50″ Samsung Smart TV, I wanted to be able to stream existing videos from my Mac on the TV. Where do I start? Search for apps? Not helpful. Chromecast is the easiest and cheapest solution but its £35 and as a bit of an IT nerd, I would not go for that option. So, instead I came up with serving files via apache and php to stream. This is not the most convenient way as we will need to be editing file names every time we need a new movie added (which isn’t too bad) but I think would help many like me.

I assume the readers here are somewhat capable of editing configs, Apache and virtual hosts, terminal etc. Pop in any questions below if something is missing or still doesn’t work after these steps.

Checks

System

A Mac/ Linux system with Apache/ PHP (This is already built in on Macs)

Check if Apache works

sudo apachectl start

and in your browser, paste the below. This should display the page below.

http://localhost

localhost
If you don’t see this, that means we’ll need to fix Apache with some help from the section further down. But if it works, follow these next steps to continue.

Steps

1.Make a directory for our new files via terminal

sudo mkdir ~/Sites

2.Create a new user config (You can use the Mac user account short name)

sudo vi /etc/apache2/users/username.conf

3.Paste the following lines into the file and save + quit

<Directory "/Users/*/Sites/">
Options Indexes MultiViews FollowSymLinks AllowOverride All
Require all granted

4.Now enable this user directory to serve files

Note: Take a backup of the httpd.conf file before editing.

cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.bak
sudo vi /etc/apache2/httpd.conf

5.Uncomment the lines below if they aren’t (Find the block by searching for “userdir”)

LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule alias_module libexec/apache2/mod_alias.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php5_module libexec/apache2/libphp5.so

Screen Shot 2016-03-20 at 08.49.03

6.Final change to be made

sudo vi /etc/apache2/extra/httpd-userdir.conf

Uncomment the following line in this file, save and quit.

Include /private/etc/apache2/users/*.conf

7.Restart Apache

sudo apachectl restart

Embed HTML5 Video

Now that our servers are working fine and the new directory from step 1 is being served, we need to put some html files in here to access the video we will be embedding. Smart TV’s do not usually support streaming video on their inbuilt ‘browsers’ but can ‘play’ html5 content.

Assumptions

Video file you will be serving:

Daredevil.mp4

Video and HTML File location:

/Users/username/Sites

HTML file name:

daredevil.html

Steps

1.Create html file in the ~/Sites directory and paste this code in and save.

<html>

<body>

<p3 style ="color:Black;font-family:papyrus"><center><b>Daredevil HD</b></center></p3>

<center>

<video width="640" controls> <source src="/Users/username/Sites/Daredevil.mp4" type="video/mp4"> </video> </center>

</body> </html>

2.Check on browser (This is what you should see on the Smart TV)

Type in the following address and you should see a page as below

http://localhost/~username/daredevil.html

Screen Shot 2016-03-20 at 09.31.57

Fixing Apache

Unless you’ve broken the configs. If you have, follow steps here to fix your Apache configs and start over from Step1 above. This site also provides a default copy of the httpd.conf file which can be used to start clean.

Note: Create backups of all files that you will be editing/ replacing.

 

 

Mac OS X El Capitan Spotlight Tip

For those who still do not use Mac OS X’s handy spotlight feature, this is a sample of how powerful and comprehensive this search tool is:

Apologies for the dimensions on the GIF, this shows a currency conversion from dollars to pounds which only takes about 1 second for the result.

ZInwIhT

No internet on Mac despite being connected to the access point.

Okay, today my friend came across a weird issue on his Mac. His Mac was connected and so was his iPhone and all other devices in the house, to the same access point. But the Mac had no internet access with an exclamation mark on the wifi icon. Despite toggling between the two bands offered by the wireless router, this wasn’t resolved. We even tried toggling wifi on and off, restarting the Mac tna. 

It was time to dive into this deeper with some arrogance courtesy of my Linux administration skills! Tried bringing the interface down via Terminal but the problem persisted. 

Something overlooked here was that the IP address assigned to his Mac was a 169. address! The home router, like all other home routers runs a 192. address. Set the IP address to be manual instead of DHCP and voila!

p.s. Make sure you do an interface down/up after the manual setting to refresh the interface. Now his Mac works with a static IP.

Login as root to Terminal:

To stop: 

ifconfig en0 down

To restart: 

ifconfig en0 up

Just sharing!