Thursday, 14 April 2016

Hide Files and View Hidden Files on Mac OS X




Macs offer a way to hide files and folders, just like other operating systems. But Mac OS X hides these options and doesn’t make it as easy as it is on Windows and Linux.

To hide a file or folder, you’ll need to set the “hidden” attribute for it. The Finder and other Mac apps will then ignore and not display this file or folder by default.


Hide a File or Folder on a Mac

Rather than hiding an individual file — although you can do that — you may want to create a hidden folder. We’ll do that for this example, although this trick will also work to hide individual files.

First, open a terminal window — press Command+Space, type Terminal, and press Enter. In the terminal, type the following command, including a space at the end of it:

chflags hidden


Drag-and-drop a folder or file from the Finder into the terminal window.


The path of the file or folder will appear in the terminal. Press Enter to run the command and the file or folder will vanish. It’s still there — it’s just hidden, so the Finder won’t show it by default.



Access a Hidden File or Folder

Want to quickly access a hidden folder from the Finder? The easiest way is to click the Go menu in the Finder and select Go to Folder.



Plug the path of the folder into the dialog box and click Go or press Enter. The ~ stands for your user folder, so if you had a folder named SecretStuff on your desktop, you’d enter ~/Desktop/SecretStuff. If it was in Documents, you’d enter ~/Documents/SecretStuff.

Although the folder is hidden and won’t appear normally in the Finder or save dialogs, you can quickly access it this way. Any files you store in this folder are effectively hidden, too — no one can accidentally click their way to the folder, but they’ll appear in the Finder if you go there directly.


View Hidden Files and Folders in the Open/Dialog

While the Finder doesn’t offer a graphical option to let you find those hidden files and folders, the Open and Save dialog on Mac OS X does.

To view hidden files and folders in the Open/Save dialog, just press Command+Shift+Period (that’s the . key).

You’ll have to click a different folder in the Open/Save dialog after pressing this shortcut. So, if the hidden folder is on the desktop, it won’t appear immediately when you press Command+Shift+Period. You have to press this keyboard shortcut, click over to another folder, and then click the Desktop folder again. Hidden folders and files will appear so you can easily access them from here.



View Hidden Files in the Finder

The Finder does offer an option to view hidden files. However, this isn’t a graphical option — you have to enable it with a terminal command and restart the Finder for your changes to take effect.

To view hidden files in the Finder, open a Terminal window and run the following commands in it, pressing Enter after each one:

defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder

If you’d like to view and hide hidden files and folders with a key press, you could create an Automator script that automatically runs these commands when you press a certain key or click a menu option.


Unhide a File or Folder

Want to unhide a file or folder? Run the same command you ran before, but change “hidden” to “nohidden”. In other words, type the following command into the terminal, typing a space after it:

chflags nohidden

If you remember the exact path of the folder or file, you can type it into the terminal. If you don’t, you can use the above trick to display hidden files and folder in the Finder and drag and drop that hidden file or folder into the Terminal, as you did earlier.




(You can also press the up arrow key at the terminal to cycle through previous commands, locating the command that made the file or folder hidden. Use the left arrow key to go to the “hidden” part of the command and change it to “nohidden”, and then press Enter.)

Type Enter afterward and the file or folder will become unhidden, so you can access it normally.




Hide, Show, and Encrypt Files and Folders the Secure Way



If you really want to hide files or folders completely, encrypt them, and keep them behind a password, you can do it really easily with Hider 2, a great, simple application that lives in your menu bar. Just drag any file straight to the menu bar to hide it. If you want to unhide the file so you can view it or make changes, you can easily flip the radio button over to show or hide it again.

It’s not a free application, but you can download a free trial and test it out before you buy.

You can also hide files or folders by renaming them to begin with a “.”, or period, character. However, Mac OS X won’t let you rename files or folders to this from the Finder window, so you’ll have to do so from the Terminal. You can also run various Terminal commands that will display these files.

This can be useful if you share a computer with someone, but someone who goes looking for these hidden files and folders can easily find them. It’s not a foolproof way to protect your files and folders from others, but encryption is.

Thursday, 19 March 2015

Nginx: 301 Redirect To A Domain Name


I am a new Nginx web server user. How do I redirect to a different domain using nginx (say example.org to example.com) permanently?

You need to use HttpRewriteModule module to change URI using regular expressions (PCRE), and to redirect and select configuration depending on variables.


rewrite directive

This directive changes URI in accordance with the regular expression and the replacement string. Directives are carried out in order of appearance in the configuration file. You can use this directive in the following context:
  • server
  • if
  • location

Nginx 301 rewrite syntax

The syntax is:

rewrite regex replacement [ flag ]

Flags can be any of the following:
  • last - completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.
  • break - completes processing of current rewrite directives and non-rewrite processing continues within the current location block only.
  • redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
  • permanent - returns permanent redirect with code 301

Example: Redirect to a different domain with nginx

In this example, redirect all traffic as follows:

Edit nginx.conf or your virtual host config and update server context as follows:

server {
  server_name .example.org;
  return 301 $scheme://example.com$request_uri;
}
server {
  server_name example.com;
  # rest of config, if any goes below ... #
}
Finally, restart/reload your nginx server, type:
# nginx -t && nginx -s reload

How do I test and verify HTTP 301 redirect?
You need to use curl command:
$ curl -I http://example.org/
$ curl -I http://example.org/foo/bar.html

Monday, 16 March 2015

Create Excel Spreadsheets in PHP

3 Easy Steps to Create Excel Spreadsheets in PHP
Spreadsheet

Generating Spreadsheets

Since computers and automation have become second nature, spreadsheet generation isn’t out of the ordinary. With MySQL databases, it becomes even more imperative, as spreadsheets are perfect to represent the data located in these structures.
Upon searching for how to create excel spreadsheets using PHP, most of the results came back with libraries that do the job. This seemed to be too much of a hassle. That’s why I’ve created 3 easy steps to create excel spreadsheets without any libraries.

Step 1: The Content-Type

The first thing you have to do is add an application/vnd.ms-excel content-type to your PHP file:
header("Content-Type: application/vnd.ms-excel");
Easy enough? Let’s move on to the actual data.

Step 2: Adding the Data

Data is just as simple. Separate cells/columns with tabs (“\t” in PHP) and move to the next row with a newline (“\n” in PHP).
Here are a few PHP echo statements that will do the trick:
echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";
I’ve separated the tabs (\t) and the new lines (\n) so you can easily see the structure. The above would create a spreadsheet that looks like this:
First Name     Last Name     Phone
John           Doe           555-5555

Step 3: Downloading the Spreadsheet

Now that you’ve set the content-type and created the data, just open up the PHP file in a browser. You’ll be asked to download the spreadsheet. If you would like to give a name to the spreadsheet, just add the following:
header("Content-disposition: attachment; filename=spreadsheet.xls");
All you need to do is change spreadsheet.xls to the name of your spreadsheet. Note that the above header also ensures that Internet Explorer will ask you to download the file rather than trying to display it.

Conclusion

That’s really all there is to create a spreadsheet. For those of you who are using Microsoft Excel 2007, you might see the following message when opening the spreadsheet:
The file you are trying to open, ‘filename.xls’, is in a different format than specified by the file extension… [message condensed]
If so, just hit the “Yes” button and you’re good to go!

Always open Google Chrome in Incognito: Mac

Most Chrome users use the Incognito browsing mode every now and then, while others use it quite often. If you prefer incognito browsing over normal browsing sessions (we’re not judging) but are vexed with the fact that there is no one-click method to launch Chrome in Incognito by default, you can do so with a little script. This particular method is meant to work in OS X only, as Windows users can always directly launch Chrome in Incognito mode from its Jumplist (just as Mac users can do so from the Dock icon’s options). The script is meant to launch Chrome in Incognito mode with one click using a dedicated shortcut created in form of an AppleScript app.
Open AppleScript Editor and paste the following script in it:
do shell script "open -a /Applications/Google\\ Chrome.app --args --incognito"
Save the script as an application and move it to Launchpad for easier access. If you want to be able to search and run it from Spotlight search, you should add it to the Applications folder. If you’re only ever going to use Chrome in Incognito mode, all you need to do now is to remove the app’s icon from the Launchpad and keep only this script there.
Chrome not running
This script has a little catch to it though – it won’t work if Chrome is already running. So if you’re looking for a method that will open Chrome in Incognito mode regardless of whether it’s already running in normal mode or not, open AppleScript Editor and save the following script as an application. The catch with this method is that it will close all other Chrome windows when launched.

tell application "Google Chrome"
    close windows
    make new window with properties {mode:"incognito"}
    activate
end tell
Chrome incognito
Both methods should give you an application that can be added to Launchpad, but the apps’ icons will be the default script one, and that’ll make it hard to be recognized. What you can do is modify the icon to something that is easier to identify. Find a nice PNG image, preferably one that’s small in size, that you can identify with as Chrome’s Incognito mode. Open that image in Preview and from the Edit menu, click ‘Select All’, followed by ‘Copy’. You can select the image by clicking and dragging your cursor over it, though there is a chance you might not select all of it.
Right-click the application you created using AppleScript Editor and select ‘Get Info’. In the window that opens, click the icon at the very top next to the script’s name and hit Ctrl+V. Make sure the padlock icon at the bottom right is unlocked so that the changes take effect. Enter your password if you are prompted to do so.
script icon
You now have a perfectly functioning and easily recognisable app that opens Chrome in Incognito mode.