3 Handy MySQL/Linux Commands

I have finally thrown in the towel, I don’t think at the moment I am able to complete fully fledged tutorials. I always fail at the last one 😦 As a result I have decided to be doing short posts highlighting some of the functions/commands that I frequently use. The main reason I am posting them is so that I can have a place I can quickly refer as I always end up spending some time looking for some particular command. I have listed them in no particular order

Importing a database from the terminal

Most of the time I deal with MySQL datasets that break the PHP file upload limit. As a result I can’t import the sql file from phpmyadmin. The following command ran in the terminal usually comes in handy

  • mysql -u mysql_user -p database_name < file_to_import.sql

where mysql_user is the mysql username, database_name is the name of the database I am exporting to and file_to_import.sql is the sql to import from. For the above command to work you have to be in the root directory where the .sql file is

Changing file and folder permissions recursively

Most Linux servers are strict on file and folder permissions. If the permissions are not correct then it normally throws error 500. You can use FileZilla to recursively change file and folder permissions but this normally takes a very long time. I ran across the following Linux command which greatly helps

  • sudo find path_to_folder -type f -print -exec chmod 644 {} \;

We are using sudo as changing file permissions is a privileged command. We are using the find command to locate the files then change their permissions. path_to_folder is the location of your files. -type is used to indicate whether we are looking for files or folders, f for files and d for directories/folders. -print informs the system to print out all the files it has found on the terminal. -exec tells the system to execute the command chmod 644 to the files which have been located. The command below works on  for files

  • sudo find path_to_folder -type d -print -exec chmod 755 {} \;

You can change the permissions to some other like 444 or something. I am not going to cover folder and file permissions in this post. A modification of this command that allows us to search for files/folders with particular permissions and change them to some other permission is as follows

  • find file_path -type d -perm 777 -exec chmod 755 {} \;

The above command looks for folders in the specified path with the permission 777 and changes it to 755. This is achieved using the -perm switch. Find is a very useful command, try playing around with it.

Changing File Ownership

File ownership and file permissions are two very different things. Normally I work on a project on localhost then at some stage transfer it to the actual server. On localhost the files usually belong to root or me(the currently logged in user). When I transfer this to the on-line server by default they belong to root. This limits actions the Apache user (www-data) can do. You will try to upload a component(this is for Joomla) and it fails. You can use the chown command to change file ownership

  • sudo chown -R www-data:www-data path_to_project

-R tells the system to recurse through all files and folders. www-data is the default Apache user. The first www-data indicates that the files and folders will be owned by www-data. The www-data after the colon indicates that the files and folders will be in the group www-data. Keep in mind that you can use any other user or group other than www-data. path_to_project is the absolute path to your files.

That will be it for today, I hope the above commands will be helpful. Cheers!



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s