Monthly Archives: February 2011

Command Line Essentials: Navigating the Filesystem

Table of Content, Text and Pipeline

The filesystem contains files and directories. Files contain data and directories contain files and other directories. There is one single special directory – the root directory – that is inside no other directory. It is the root, where everything starts. Now give three commands:

essentials@kvaser:~$ cd /
essentials@kvaser:/$ pwd
/
essentials@kvaser:/$ ls
bin   dev  home  lost+found  mnt  proc  sbin     srv  tmp  var
boot  etc  lib   media       opt  root  selinux  sys  usr

cd: change directory (the / is the root)
pwd: print working directory
ls: list files and folders

Now, we will list the contents of the root directory and get a little more information:


essentials@kvaser:/$ ls -l
total 109
drwxr-xr-x  2 root root  4096 Feb 18 06:51 bin
drwxr-xr-x  3 root root  1024 Feb 18 07:03 boot
drwxr-xr-x 13 root root  2880 Feb 18 07:06 dev
drwxr-xr-x 78 root root  4096 Feb 28 22:11 etc
drwxr-xr-x  8 root root  4096 Feb 28 21:39 home
drwxr-xr-x 10 root root  8192 Feb 18 06:51 lib
drwxr-xr-x  2 root root 49152 Feb  2 21:01 lost+found
drwxr-xr-x  3 root root  4096 Feb 17 21:39 media
drwxr-xr-x  2 root root  4096 Jan 16 21:45 mnt
drwxr-xr-x  2 root root  4096 Feb  2 21:27 opt
dr-xr-xr-x 83 root root     0 Jan  1  1970 proc
drwxr-xr-x  4 root root  4096 Feb  3 21:04 root
drwxr-xr-x  2 root root  4096 Feb 18 06:52 sbin
drwxr-xr-x  2 root root  4096 Sep 16  2008 selinux
drwxr-xr-x  2 root root  4096 Feb  2 21:27 srv
drwxr-xr-x 11 root root     0 Jan  1  1970 sys
drwxrwxrwt  3 root root  4096 Feb 28 22:17 tmp
drwxr-xr-x 10 root root  4096 Feb  2 21:27 usr
drwxr-xr-x 14 root root  4096 Feb  2 22:38 var

The “d” first in the line means “directory”. Ignore the rest of the columns for now. Lets list the contents of the folder bin:

essentials@kvaser:/$ ls -l bin
total 4996
-rwxr-xr-x 1 root root 790844 Apr 10  2010 bash
-rwxr-xr-x 1 root root 502968 Nov 15 16:46 busybox
-rwxr-xr-x 1 root root  38996 Apr 28  2010 cat
-rwxr-xr-x 1 root root  51244 Apr 28  2010 chgrp
  (many lines)
-rwxr-xr-x 1 root root   2015 Jan 20  2010 zforce
-rwxr-xr-x 1 root root   5597 Jan 20  2010 zgrep
-rwxr-xr-x 1 root root   1733 Jan 20  2010 zless
-rwxr-xr-x 1 root root   2416 Jan 20  2010 zmore
-rwxr-xr-x 1 root root   4952 Jan 20  2010 znew

Here, the number before the date is the size of the file, in bytes.
The “x” means “executable” – that is a program. Confusingly, also directories are considered executable here.

Spend some time exploring your filesystem with cd, pwd, ls, for example:

essentials@kvaser:/$ cd etc/
essentials@kvaser:/etc$ pwd
/etc
essentials@kvaser:/etc$ cd ..
essentials@kvaser:/$ pwd
/
essentials@kvaser:/$ cd usr/bin/
essentials@kvaser:/usr/bin$ pwd
/usr/bin
essentials@kvaser:/usr/bin$ cd ..
essentials@kvaser:/usr$ ls
bin  games  include  lib  local  sbin  share  src
essentials@kvaser:/usr$ 

Do play around more!

All those files and folders are not for you to modify. If you want to make your own folders and fill them with files, first go home:

essentials@kvaser:/$ cd
essentials@kvaser:~$ mkdir MyFirstFolder
essentials@kvaser:~$ touch MyFirstFile
essentials@kvaser:~$ ls -l
total 4
-rw-r--r-- 1 essentials essentials    0 Feb 28 22:39 MyFirstFile
drwxr-xr-x 2 essentials essentials 4096 Feb 28 22:39 MyFirstFolder
essentials@kvaser:~$ pwd
/home/essentials

Several things to note here…
cd without arguments takes you home
touch creates an empty file (size = zero bytes), not executable
ls -l outputs essentials, the user (and group) who owns the file. There is a special user (and group) named root who owns most other files in the filesystem. The root user is a completely different thing than the root directory.

You can clean up again:

essentials@kvaser:~$ rm MyFirstFile 
essentials@kvaser:~$ rmdir MyFirstFolder/
essentials@kvaser:~$ ls
essentials@kvaser:~$ 

If you run cygwin (under Windows) you can try

$ cd /cygdrive/c

which takes you to your Windows c-drive.

Finally, end your session:

essentials@kvaser:~$ exit
exit

Command Line Essentials: Introduction

I have decided to try to write a Command Line Essentials guide. I intend to start from the beginning and make it very easy.

Table of contents

  1. Navigating the file system
  2. Text and Pipeline

Getting started
The primary platform will be Linux (Debian). Most things will apply to other Linux distributions, Mac OS X as well as Cygwin.

Linux: just start a terminal.
Mac OS X: start Terminal application, under Application/Utilities.
Windows: install and start cygwin.

What matters more than the operating system is the shell (more on this later – I will use bash). Bash looks something like this:

essentials@kvaser:~$ 

Here essentials is my username and kvaser is the name of the computer. To make sure it is Bash, print the value of the variable SHELL:

essentials@kvaser:~$ echo $SHELL
/bin/bash

Note: case is essential.

If you have been given remote access to a Linux system and you are on Windows; install putty. You will need host address, username and password. First thing you should do when you log in this way is to change your password.

essentials@kvaser:~$ passwd

Pulling your Märklin Z 86501

Isn’t it annoying? You just set up your new Märklin Z-layout, but the tracks are a little bit dirty, rusty or whatever, and the the trains stop all the time.

You bring out your item 86501 Gleisreinigungswagen, but it is too heavy to pull up your long 3% slopes. And it is not so practical to just go downhill.

This is what I did:

How to pull your 86501

Several advantages:

  • Double pulling power
  • If one engine loses electrical contact, the other one takes over
  • Looks good!

Hope you found this helpful!

DOS flashing in 2011

Still BIOS updates and other firmware flash operations mostly need to be done from DOS. Seriously, DOS, in 2011?

I found a decent strategy. When you install your computer, make a small (256Mb) FAT partition with a FAT16 filesystem on it first on your harddrive (with some skills and luck you can use it also as linux /boot partition). Whenever you need to flash BIOS or anything else, put the DOS flash utility on this partition.

Make yourself a bootable FreeDOS CD. When you boot into freedos you just do:

  a:\> c:
  c:\> dir
  (dir output - to find the name of the update program)
  c:\> BIOS1234.exe

When everything is done you can just reset the computer with the reset-button, eject the CD, and boot your normal OS.

So, what is clever about this? It is not so easy to make FreeDOS read USB-keys or access the network, and the bootable CD is naturally read-only. But a little FAT16 filesystem first, on your first hard drive, is very easy to access. And you can easily put any upgrade files there, from any OS you like to run normally.

On a brand new computer (or hard drive), the first thing you do is boot FreeDOS (or your Linux live cd of choice) and create the little FAT partition. Then you can install your other OSs. If you got the computer with Windows already installed, and you intend to keep it, you probably dont need to flash anything anyway.