Indispensible Unix commands

The first lab session of my Computer Science course consisted primarily of learning the following Unix commands: pwd, ls, cd, cp, mv, rm, mkdir, chmod, more, man. Although I’ve been using nothing but Windows throughout my career – from Windows 95 to 2000 to XP – these commands have proven to be indispensible for those occasions when I need to remotely administer a Linux box.

Let’s go through them one by one.

You log in to a Linux server and all you get is a dollar sign and a cursor. The first thing you want to know is: where am I? So you want to print the working directory:

pwd

Then you want to list all the contents of the current directory in a long listing format:

ls -al

To get around, you change directory:

cd /another/directory

Then, to copy a file from one directory to another:

cp /some/directory/somefile.txt /another/directory

To move it instead:

mv /some/directory/somefile.txt /another/directory

There is no command to rename, you just move it:

mv oldname.txt newname.txt

To remove a file:

rm filename.txt

To remove a directory and its contents recursively:

rm -r dirname (use with extreme caution!)

To make a directory:

mkdir dirname

To change the mode of a file or directory to make it writeable by everyone:

chmod ugo+rwx file_or_directory

ugo+rwx means, for (user, group, others), add (read, write, execute) permissions. Use the minus sign instead of the plus sign to remove permissions. ugo and rwx can be used in any combination, e.g., go-w.

Finally, to get to know more about the contents of a file:

more filename.txt

Press space to scroll to the next page, and q to quit. (Note: less is better than more. With less, you could go both backwards and forwards.)

Finally: when in doubt, consult the manual!

man command

Update 13 February 2009

See also Commands that you must know in Linux… by dirn.name.

Moonlight: Microsoft Silverlight in Linux

moonlight

Need to view Microsoft Silverlight enabled sites, but you’re on Linux, and running Firefox as the web browser? Have no fear, Moonlight is here.

Moonlight is an open source implementation of Microsoft Silverlight for Unix or Linux platform.

The current release version is 1.0. All you have to do is, just go to their site, and install the plugin/add-on from your Firefox.

Enjoy!

Detect ISP Throttling with Google’s M-Lab

M-Lab
Need to test your Internet connection? Wonder if your ISP is truly capped or throttling your Torrent downloads? How to be sure if TMNET Streamyx (Malaysian ISP, if you’re wondering) really give it’s “best effort” speed? Get your answers via Measurement Lab (M-Lab) by Google.

Announcement were made from the Official Google Blog, introducing the measurement utility.

Measurement Lab (M-Lab) is an open platform for researchers to deploy Internet measurement tools. By enhancing Internet transparency, we aim to help sustain a healthy, innovative Internet.

Continue reading

Automated scp on Linux

Scenario

You would like to automate the export of SQL dump files from one Linux server to another using scp. These dump files are to be generated and exported once every two weeks. You also wish to archive these dump files on the source server.

So let’s say the servers and directories are as follows:

Source

Hostname: perak
Username: nazham
Dump file directory: /home/nazham/data_export/current
Archive directory: /home/nazham/data_export/archive/yyyymmdd

Target

Hostname: selangor
Username: romantika
Target directory: /home/romantika/data_import

Passwordless scp

First you need to set it up so that you don’t need to enter a password for scp. The overall picture is as follows:

  1. At the source server, generate a pair of public and private keys [1] using RSA.
  2. Store the private key in a specific place in the source server.
  3. Store the public key in a specific place in the destination server.
  4. And that’s it! Now you’ll no longer be prompted for a password when using scp.

One-off steps

# login to perak with username nazham
ssh-keygen -t rsa
# choose default location, no passphrase - just press enter at all prompts
scp ~/.ssh/id_rsa.pub romantika@selangor:/home/romantika/.ssh/authorized_keys
# I'm assuming the file authorized_keys does not exist!
# otherwise, you need to append the contents of id_rsa.pub to it.
crontab -e
# add the following:
# 1 0 1,15 * * nazham /home/nazham/run_data_export >> /dev/null 2>&1
# which means, run at 12:01 am every 1st and 15th of the month
mkdir ~/data_export
mkdir ~/data_export/current
mkdir ~/data_export/archive

The run_data_export script

todaysDate=`date +%Y%m%d`
oldDate=`date -d '1 year ago' +%Y%m%d`
exportDir=data_export
mysqldump -u username -ppassword -r ~/$exportDir/current/filename dbname tablenames
echo $todaysDate > ~/$exportDir/current/importdate.txt
scp ~/$exportDir/current/* romantika@selangor:/home/romantika/data_import
mkdir ~/$exportDir/archive/$todaysDate
rm ~/$exportDir/current/importdate.txt
mv ~/$exportDir/current/* ~/$exportDir/archive/$todaysDate
if [ -d ~/$exportDir/archive/$oldDate ]; then rm -r ~/$exportDir/archive/$oldDate; fi

The Linux date command

I’m very impressed by the Linux date command. This might be old news to some, but the fact that I’m able to say date -d '1 year ago' or date -d '30 days ago' completely blows the mind.

Notes

[1] From Pass on Passwords with scp: “If you’re not familiar with public key cryptography, here’s the 15-second explanation. In public key cryptography, you generate a pair of mathematically related keys, one public and one private. You then give your public key to anyone and everyone in the world, but you never ever give out your private key. The magic is in the mathematical makeup of the keys; anyone with your public key can use it to encrypt a message, but only you can decrypt it with your private key.”

Red Hat Linux Training and Certification

rh1Red Hat Training provides classroom-based courses at convenient worldwide locations and in a variety of topics. I’ve just finished my training for RH033 Red Hat Linux Essentials, last week. Among Linux Red Hat training you can get here in Malaysia are:

RH033 Red Hat Linux Essentials
RH131 Red Hat Linux System Administration
RH253 Red Hat linux Networking and Security Administration
RH142 Linux Troubleshooting Techniques & Tools
RH184 Red hat Enterprise Linux Virtualization

… and many more. Just contact your nearest training center.

rh3

For Kuala Lumpur, training center below offers Red Hat Courses. The center classroom and facilities are good, and the trainers are well experienced and knowledgable :

Iverson Associates Sdn. Bhd.
Suite T113 – 114, 3rd Floor,
Centrepoint, Lebuh Bandar Utama,
47800 Bandar Utama, Selangor Darul Ehsan.
Tel: 03-77262678
http://www.iverson.com.my

rh2

For more info, feel free to check Linux Red Hat courses and certification listings:

Red Hat Training and Certification

Red Hat Courses