Categories
Hosting Tips & Deals Linux Server Administration Tips

Linux: Check how much disk storage each directory takes up (Disk Usage command – du)

The Linux command du stands for disk usage which is used to check the amount of disk storage any particular directory or file is using. By default, the simple command:

du

Would return the disk usage in God-knows-what-unit of each of the directories in the current working directory and those beneath them – in a recursive manner. If you happen to have lots of them, the returned stats would be scrolling down crazily which barely makes it any useful.

Even if you have specified a specific directory such as "somedir":

du somedir

It still works in this uncomfortable way.

The solution is to use the -sh switch, the one switch a beginner will ever need:

du -sh

Which simply returns the amount of disk space the current directory and all those stuff in it are using as a whole, something like:

2.4G

Much much more intuitive and readable.

By:

du -sh somedir

You can find out how much disk storage directory "somedir" is using:

101M    somedir

To get all the subsequent / child directories disk usage from the current directory, simply use the asterisk:

du -sh *

It will then list the disk usage of all of them (but not recursively) one by one in a very readable manner:

8.0K    dir1
1.4G    dir2
135M    dir3
Categories
Linux Server Administration Tips

Linux: How to find all the files containing a particular text string?

At Linux command line, to find a particular text string in all the files from the current directory recursively (that is, including all those files from the child or grandchild directories), use something like this via SSH:

find . -exec grep -l "needle" {} \;

This command searches through all directories from the current directory recursively for the files that contain the string “needle”.

To search only .php files:

find *.php -exec grep -l "needle" {} \;

To search a specific directory:

find dirname -exec grep -l "needle" {} \;

To find all .php files that contain “needle” in all the directories under “somedir”:

find somedir -name *.php -exec grep -l "needle" {} \;

Categories
Hosting Tips & Deals Linux Server Administration Tips

Linux wget Command to Download and Mirror a Website in Static Local Copy

wget is such a wonderful command in Linux you can ever get. Consider blessed to have it in your SSH arsenal. Now, not only does it allow you to download something neatly from the command line to the current working directory – that’s why WordPress always puts the latest version of the blog script at http://www.wordpress.org/latest.zip, that way you don’t even have to visit the site before using wget to get it in no hassle:

wget http://www.wordpress.org/latest.zip

And the latest version of WordPress will be in your local working directory in no time – but it can also help you to create a local mirrored copy of a remote website for backup or for local browsing.

How to Make Mirrors from a Site with wget command?

wget comes equipped with so many useful switches and features and one of them is to mirror stuff off the Internet, from online to offline. For example, you have a blogspot blog and you want a local copy of the entire blog files / web pages, including all those CSS style sheets, images and scripts. No problem, just go with

wget -mk http://yours.blogspot.com

Don’t worry, wget will create a new directory named by the domain, in this case, "yours.blogspot.com", to store all those files. After screens and screens of real-time logging, when it’s done, the local mirror should be ready to use.

The –m switch is short for –mirror and the –k switch translates all absolute URLs in the HTML into relative ones for totally offline browsing.

The problem is that wget works much better with sites using sanitized / clean URLs such as a WordPress blog. Though a blogspot blog is basically in static HTML, it contains quite some complicated URLs here and there that’s filled with parameters. The result doesn’t look too pretty.

However, after trying wget to recursively download and mirror a WordPress blog, it looks fantastic. Try it with a permalink enabled WordPress blog and it will surely amaze you.

It’s best used to generate mirrors of websites completely built in static files or sites with simple URL schemes.

Categories
Linux Server Administration Tips SQL / MySQL Tips and Tutorials

How to Recover or Reset MySQL root Password after You Forgot and Lost It

MySQL has come with a safe mode wherein access privileges are not checked, which essentially enables you to log in anonymously to change anything in any database.

And we are going to get in this safe mode to reset the lost MySQL root password.

  1. First, you need to stop the current MySQL daemon by:
    /etc/init.d/mysql stop
  2. Then you can start the safe mode of MySQL and skip privileges check by:
    mysqld_safe --skip-grant-tables &
    The & in the end gets it to run in the background so we can continue to reset the root password.
  3. Now you can log into the MySQL server anonymously (well not anonymously but without privilege check):
    mysql --user=root mysql
  4. And reset the root password by updating the user table of the mysql database:
    UPDATE user SET Password=PASSWORD('newrootpwd') WHERE user='root';
    FLUSH PRIVILEGES;
  5. Get out of the safe mode and restart the normal MySQL daemon:
    /etc/init.d/mysql restart
  6. At last, you should be able to connect to the database server by your new root password.

As always, you will have to log in as root of the entire server or you won’t be able to stop the MySQL daemon and start the safe mode in the first place.

Note that I’m recovering the root password of MySQL in Ubuntu 9.04 Jaunty with MySQL installed by aptitude. Specific procedures may vary distribution by distribution but I’m sure the fundamental logic remains the same: getting in the safe mode to reset the root password.

Categories
Linux Server Administration Tips PHP Tips & Tutorials

Linux, PHP: Differences between File Modification Time: filemtime() and File Change Time: filectime()

In most Unix file systems, the modification time and the change time of a file may not necessarily be the same, they are actually 2 very distinct concepts to deal with:

  1. File modification time represents when the data blocks or content are changed or modified, not including that of meta data such as ownership or ownergroup.
  2. File change time represents the time when the meta data or inode data of a file is altered, such as the change of permissions, ownership or group.

Accordingly, PHP function filemtime() returns the time the content is modified or updated while function filectime() returns the time when the meta data is updated or changed.

When you need to display a web page last modification time, for example, you should use filemtime() because you want to show the time when the page content is updated.

Categories
Linux Server Administration Tips PHP Tips & Tutorials

Where is php.ini located?

Well it depends on the Linux distribution you are using, the version of php and the way you install it with Apache web server. Php.ini may be here:

/etc/php.ini

Or here:

/etc/php/php.ini
/etc/php5/php.ini

Or here:

/usr/bin/php5/bin/php.ini

Anyway, you can always find any file named php.ini in this manner

find / -name php.ini

The simplest yet most powerful usage of the renowned find command. By its help I was able to locate the php.ini on my Ubuntu 9.04 Apache2 and PHP5:

/etc/php5/apache2/php.ini

Use phpinfo() to find out

Write a simple PHP file with the following content:

<?php phpinfo();

And upload it to your server and access it in your web browser to get a page like this:

PHP configuration file php.ini path
PHP configuration file php.ini path
Categories
Linux Server Administration Tips

Accidental Ctrl+S Locks and Freezes Linux Terminal / SSH / Telnet

Ctrl+S happens to be a rather handy and popular combination as it’s used in Windows applications to save your current working data. I accidentally used it several times in Vim and it keeps locking the screen up and halting the interactivity – basically, after Ctrl+S I can’t do anything to the terminal (window). I use PuTTY in Windows to access my Linux hosting via SSH.

A few lookups in Linux manuals reveal the secrets, the Ctrl+S key combination temporarily freezes the screen but the input is recorded though not dealt with. Once you press Ctrl+Q, it is unlocked and all the flood of input you have provided to the terminal takes immediate effect.

So, don’t panic when you accidentally pressed Ctrl+S and seemingly freezes everything, just press Ctrl+Q to exit it.

Categories
Linux Server Administration Tips

How to change Vim syntax highlighting colors?

Unbuntu has auto-configured Vim to use syntax highlighting for text (mostly, programs and configuration files of course) editing, the problem however, is that some of the colors appear to be darker than wanted on SSH console and it’s a little hard to recognize comfortably.

So how can we change the default colors of the syntax highlighting?

  1. Locate Vim configuration file which is located at /etc/vim/vimrc, add a line after ‘syntax on’: colorscheme desert
  2. Now that we are prescribing Vim to use the syntax color scheme desert, we will want to find the scheme file to change the colors that we don’t feel comfortable with. The desert color scheme file is located at: /usr/share/vim/vimcurrent/colors/desert.vim
  3. Proceed to open it with vi and replace the color values with new ones. For example, the default comment color may be a little too dark on a black screen, let’s make it lighter by changing
    hi Comment      ctermfg=darkcyan

    to

    hi Comment      ctermfg=blue
  4. Reload the vimrc configuration file by performing ‘source /etc/vim/vimrc’ at shell.
Categories
Linux Server Administration Tips PHP Tips & Tutorials

PHP: Back tick `

Back tick ` in PHP is used to read the output from another program, usually external commands in Linux shell, much like the way a pipe does the job with bash.

In PHP, Backtick “ embraced string is treated a command and executed externally in shell, returning the execution results from the standard output. For example,

$files = `ls -l`

And $files now contain the files and directory listing by the bash command ‘ls -l’.

Categories
Linux Server Administration Tips PHP Tips & Tutorials

Linux: The differences between file times: atime (accessed time), ctime (changed time) and mtime (modified time).

Unless you have explicitly opted out with a noatime option mounting your Linux file system, there are generally 3 types of time on each and every file of Linux: atime or access time, ctime or change time, and mtime or modification time.

These are the differences between the 3 file system times:

  1. atime – access time, or the last accessed time of a file or directory, whenever you accessed, opened or touched it, the atime is updated to the current time. You can get the file atime value in PHP.
  2. ctime – change time, or the last changed time of the file or directory, whenever you change and update the file such as changing the file ownership or permissions or modifying the file content, the ctime of the file is updated to the current time. You can get the file ctime in PHP.
  3. mtime – modification time, or the last modified time of the file or directory, whenever you modify and update the content of the file. Modifying file ownerships or permissions doesn’t change mtime of the file. To get mtime of a file in PHP, you need a the php function filemtime.