Posts tagged Linux
echo on Linux does not need quotes
The echo command in Linux does not require quotes:
echo Hi there blog readers…will output the same as:
echo "Hi there blog readers"The obvious exception is when the string to be printed contains special characters that would confuse echo itself, in which case quotations may be necessary.
How I use ls on Linux
When I run ls, I run it as ls -lAhog:
lsays to put each file or folder on its own lineAsays to list all files – hidden files, backups, etc. – but omit the current directory (.) and the parent directory (..)hsays to use human readable file sizes –1.5Ginstead of1628688384gsays to omit the userosays to omit the group
Occasionally I use the -R parameter, which lists folders and subfolders recursively.
Location of the systemwide crontab on CentOS
The systemwide crontab file on CentOS can be found at
/etc/crontabYou can edit it with your editor of choice, e.g.
sudo vi /etc/crontabLocation of Wordpress' php.ini file in Ubuntu
/etc/php5/apache2/php.iniVagrant cheat sheet
Developing robust software requires an accurate testing and staging environment. Vagrant provides just that. It lets you mimic your production server map on your computer.
Bash cheat sheet
Technically, this is a bash cheat sheet; however, many of the commands will work with Bourne shell descendants, such as zsh.
The only way that actually works to clear the screen in tmux
It was like pulling teeth to figure out a way to clear the screen in tmux, such as CMD+K does in Terminal.app and iTerm. Add this line to your tmux.conf. It Just Works™:
bind -n C-k send-keys -R \; send-keys C-l \; clear-historytmux cheat sheet
Since switching to vim, I’ve found myself really enjoying keyboard-based user interfaces. So instead of managing multiple terminal sessions in iTerm, I have started using tmux. I’m writing this cheat sheet as I learn. Check back often.
How to get a list of open files for a process
lsof -p PID_NUMBERMoving around in Linux with pushd, popd, and dirs
Often when administering a Linux server, you move around between the same few directories. You cd here, you cd there, and then you cd back here. Instead of cd‘ing so much, Linux has 3 powerful commands that can help you: pushd, popd, and dirs.
My ssh config file template
If you ssh into servers, you must set up a .ssh/config file. It will allow you to type ssh myd instead of ssh myuser@mydomain.com -i ~/.ssh/mykey.pem -p 1234.
reverse-i-search'ing in the Linux shell
When you press ctrl+r in the shell, you activate a feature called “reverse-i-search.” Reverse-i-search’ing lets you search through your shell history commands, from most recent to oldest.
VIM Cheat Sheet
After 9 years of TextMate, I’m switching to vim. I’m writing this cheat sheet as I learn. Check back often.
How to check if Bash is vulnerable to Heartbleed
Run this line in your Bash console:
env x='() { :;}; echo vulnerable' bash -c "only this line should appear"If you see this, your Bash is vulnerable to Heartbleed:
vulnerable
only this line should appearIf you see this, your Bash is secure from Heartbleed:
only this line should appearThe location of Postfix's main config file in Linux
On Linux, Postfix’s main configuration file, main.cf, is located at:
/etc/postfix/main.cfGet Safari's Reading List programmatically with Ruby
I wanted to export my Safari Reading List on OS X, so I wrote some code to do it. I leveraged Ruby, but this methodology could be easily ported to other languages like Bash, Python, or Objective C.
How to make a ruby file executable
In the ruby file:
#!/usr/bin/env ruby
puts 'Hello world'At the command line:
chmod +x ruby.rbThen you can execute it like this:
./ruby.rbHow to dump all MySQL databases to a gzip file from inside a bash script
Often as part of an hourly, daily, or weekly server backup bash script, you will wish to dump all MySQL databases to disk as a gzip file. This way, in the case that any of your databases suffer from corruption or user error, you will have a backup.
Simply put this command inside your cron-scheduled backup bash script, substituting the USERNAME, PASSWORD, and the destination path with your info:
#!/bin/bash
mysqldump -u USERNAME -pPASSWORD --all-databases --routines| gzip > /mysqlbackups/MySQLDB_`date '+%m-%d-%Y'`.sql.gzHow to start and stop Apache 2 in Ubuntu Linux
How to start Apache 2:
sudo service apache2 startHow to stop Apache 2:
sudo service apache2 stopHow to restart Apache 2:
sudo service apache2 restartHow to reload Apache 2:
sudo service apache2 reloadReloading is useful if you change a virtualhost file, since it will not actually stop Apache. Therefore, your websites will not go offline (even for a few seconds).
How to add a user to a group in Linux
Simply replace USERNAME and GROUPNAME with the user you wish to add to a group:
sudo adduser USERNAME GROUPNAMEHow to add all new files in a folder to SVN
If you add a bunch of new files to an SVN-managed folder, you do not need to svn add... each file manually. Just cd into the folder and type:
svn add ./ --forceHow to change a line into a shape in Illustrator
In Illustrator, there are line objects and shape objects. Lines are made of points, and shapes are made of boundaries. Since line objects are not easily resized or manipulated, you will want to convert your line into a shape.
How to change ownership recurisively in Linux
Run this command, replacing USER and GROUP with the user and group to which you would like the files changed; substitute DIRECTORY for the folder whose ownership you would like to change:
chown -R USER:GROUP ./DIRECTORYHow to download Wordpress in Linux
Every time you set up a new Wordpress site in Linux, you will need to download a fresh copy of Wordpress. Here’s the fastest way:
wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gzHow to dump a MySQL database
There are many cases where you will want to dump a MySQL database. Examples include: backing up a website, migrating a website to a new server, moving from a development machine to a production server, and more.
Simply run this command, substituting the USERNAME, DATABASE_NAME, and FILENAME with your info. When you run this command, it will prompt you for the MySQL password for the USERNAME you provided:
mysqldump -u USERNAME -p DATABASE_NAME > FILENAME.sqlHow to get the Ubuntu version from the commandline
lsb_release -aLook for the line that starts with Release:. The following number is your version number. As of this writing, mine says Release: 12.04, which means I am running Ubuntu version 12.04.
How to get your Postfix version
postconf mail_versionYou will see something like this: mail_version = 2.9.6. In this case, your Postfix version is 2.9.6.
How to import a .sql file into a MySQL database
First, create a new, empty MySQL database from the MySQL console:
create database DATABASE_NAME;Then, import the .sql file into it:
mysql -u root -p -h localhost DATABASE_NAME < FILE_TO_IMPORT.sqlHow to list all users in Linux
cat /etc/passwdHow to open the MySQL console
The MySQL console allows you to directly execute SQL commands, without having to use a programming language adapter such as PHP, Ruby, or Python.
Run this command, replacing USERNAME with your MySQL username; you will be prompted for the password.
mysql -u USERNAME -p -h localhostHow to restart postfix in Ubuntu Linux
sudo /etc/init.d/postfix restartN.B. I am running Ubuntu 12.04 LTS and Postfix 2.9.6.
How to set folder permissions and ownership for Apache website data
Whenever I upload a new folder to my website, I set its permissions and ownership:
sudo chmod -R g+rw /var/www/my-website.com/new-folder/
sudo chown -R www-data:www-data /var/www/my-website.com/new-folder/How to set up a new virtualhost domain in Apache 2
This guide will show you how to quickly set up a new website in Apache 2 using virtualhosts. Follow these instructions exactly as written, down to the character: trailing slashes, case, line breaks, etc.
Start by firing up a terminal, and creating a folder for the new website:
mkdir /var/www/my-new-website.comHow to unzip a file on the Linux commandline
In order for this to work, the unzip command must be installed. If you do not have unzip on your system, install it using the package manager for your distro (apt-get, etc.).
unzip ./FILE_NAME.zipMy minimal Apache2 virtualhost file
I have written at length about how administering a Linux server is a nightmare. It does not matter how smart, experienced, or nerdy you are. Everyone who uses Linux bangs their head against a wall from time to time.