Learning Unix

March 29 2015

I’ve been witting code almost every single day for past 4 years, this involves a major chunk of PHP, MySQL, Javascript, and some Python and Rails these days. Along with all these languages I use UNIX commands almost every day, but, an honest confession, I never took out time to really understand why I chose a certain command over the other, I always have to double check the permissions, I fail with basic utility and pipe commands, and am never confident with what am doing (sigh).

As my 2015 resolution, I promised myself to be a better engineer and not just a web developer. I know I can be better, every day, with every line of code that I write. So I’ve decided to start with UNIX. This blog is going to be my documented journey of understanding UNIX OS, how it works, why do I do what I do on a daily basis, and what can I do to better my performance.

I’ll start from the basics — only because thats how I like to learn, I like to be detailed, methodical, and reason my actions. So lets get started!

What is UNIX?

UNIX is a computer Operating System (OS) which is capable of handling activities from multiple users at the same time. By operating system, I mean a suite of programs which make a computer work. It was first developed in the 1960s, and has been under constant development ever since. The main concept that unites all versions of UNIX is the following four basics:

  1. Kernel — The kernel is the heart of the operating system. It interacts with hardware and most of the tasks like memory management, tash scheduling and file management.
  2. Shell — The shell is the utility that processes all unix requests. When we type in a command on terminal, the shell interprets the command and calls the program that we want. The shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are most famous shells which are available with most of the Unix variants.
  3. Commands — There are various command which we use in our day to day activities. cd, cp, mv, ls, cat, and grep etc. are few examples of commands. There are over 250 standard commands plus numerous others provided through 3rd party softwares. All the commands come along with various (optional) options.
  4. Filesystem — All the data in UNIX is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the filesystem.

File Management — Files

A file system is an abstraction that supports the creation, deletion, and modification of files, and organization of files into directories. It also supports control of access to files and directories and manages the disk space accorded to it.

Multiple storage devices are usually attached to a modern computer. Some OS treat the file systems on these devices as independent entities. For example, in MS-DOS (Microsoft Disk Operating System), and systems derived from it, each separate disk partition has a drive letter associated with it, and the file hierarchy on each separate drive or partition is separate from all others attached to the computer. So, DOS has multiple trees whose roots are drive letters. For example, a typical Windows machine may have a directory D:\Users on the “D:” drive and a directory C:\Temp on the “C:” drive but these directories are in two separate trees, not a single tree, and are independent of each other.

This is not the case for UNIX. In UNIX there is a single file hierarchy. Every accessible file is in this single file hierarchy, no matter how many disks are attached. There is no such thing as the “C” drive” or “D” drive” in UNIX. This is because of the concept of mounting (which I’ll hopefully dig into soon). By mounting a file system onto the file hierarchy, the file system becomes a subtree of the hierarchy, making it possible to navigate into the file system from the rest of the file hierarchy. The mount command without arguments displays a list showing all of the file systems currently mounted on the file hierarchy.

We just learnt that all the data in UNIX is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the filesystem. When we work with UNIX, one way or another we spend most of our time working with files. This section would teach us how to create and remove files, copy and rename them, create links to them etc.

  1. Ordinary Files: An ordinary file is a file on the system that contains data, text, or program instructions.
  2. Directories: Directories store both special and ordinary files. For users familiar with Windows or Mac OS, UNIX directories are equivalent to folders.
  3. Special Files: Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names.

Listing Files — To list all files in the directory that match the name. If name is left blank, it will list all of the files in the current directory.

$ ls [options] [names]

ls does two different things, depending on whether the argument is a directory or a non-directory file:
* When the argument is a directory, ls displays its contents.
* When the argument is not a directory, ls displays its name.

Options:

options available for ls command. (source: http://www.techonthenet.com/unix/basic/ls.php)

Some useful examples of the mighty ls command:

  1. Open last edited file using ls -t. ls -t sorts the file by modification time, showing the last edited file first, vi editor command head -1 picks up this first file.
$ vi `ls -t | head -1`

2. Display file size in human readable format using ls -lh. The h stands for human readable form, to display file size in easy to read format. i.e M for MB, K for KB, G for GB.

$ ls -lh
-rw-r----- 1 nupurkapoor dev-team 6.3M Jan 12 15:27 temp.txt.gz

3. Display directory information using ls -ld. ls -l you will get the details of directories content. But if you want the details of directory then you can use -d option.

4. Order files based on last modified time using ls -lt. Sort the file names displayed in the order of last modification time using -t option.

5. Order files based on last modified time, in reverse order, Using ls -ltr.

6. Display hidden files using ls -a.

7. Display file’s inode number using ls -i. Using inode number you can remove files that has special characters in it’s name.

8. Visual classification of files with colors Using ls -F.

The command ls supports the -l option which would help get more information about the listed files:

$ ls -l
total 69056
drwxr-xr-x 11 nupurkapoor staff 4096 May 29 20010 zlib-1.2.3
-rwxr-xr-x 1 nupurkapoor staff 221518 Jan 21 2011 libtool
drwxr-xr-x 2 root staff 68 Feb 18 2011 local
-rw — — — — 1 nupurkapoor staff 1910 Aug 25 2013 logfile
drwxr-xr-x 14 root admin 476 Apr 11 2011 macports
-rw-r — r — 1 nupurkapoor staff 679 Jan 21 2011 octave-core
lrwxr-xr-x 1 nupurkapoor staff 6 Oct 16 2012 php5 -> php5.5
drwxr-xr-x 3 nupurkapoor staff 102 Jan 21 2011 scripts
drwxr-xr-x 7 nupurkapoor staff 238 Oct 14 2012 soapUI-Tutorials
-rw-r — r — 1 nupurkapoor staff 9557 Oct 14 2012 soapui-settings.xml
drwxr-xr-x 6 nupurkapoor staff 204 Jan 30 2011 src
drwxr-xr-x 5 root staff 170 Apr 11 2011 svn
drwxr-xr-x 6 nupurkapoor staff 204 Oct 14 2012 tempLoadUI
-rw-rw-r — 1 nupurkapoor nupurkapoor 20480 Nov 25 2007 webthumb.tar
drwxr-xr-x 5 nupurkapoor staff 170 Apr 11 2011 trunk

Above:

1. First Column: represents file type and permission given on the file. Below is the description of all type of files.

2. Second Column: represents the number of memory blocks taken by the file or directory.

3. Third Column: represents owner of the file. This is the Unix user who created this file.

4. Fourth Column: represents group of the owner. Every Unix user would have an associated group.

5. Fifth Column: represents file size in bytes.

6. Sixth Column: represents date and time when this file was created or modified last time.

7. Seventh Column: represents file or directory name.

Like what you read? Give Nupur Kapoor a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.