Monday, November 14, 2011

Smart UNIX commands

The power of UNIX commands is that each of them come with an array of parameters and options to support. But practically speaking it becomes difficult to remember them all and use them effectively.

Mostly in the UNIX world, some options have the common meaning across most commands :

Option NameOption Meaning
-iIgnore text case.
-vVerbose mode. Echoes the steps of operation.
-aShow all objects.
-cProduce a count.
-dSpecify a directory.
-fSpecify a file to read from.
-hDisplay a help message for the command.
-lProduce a long format of output.
-oSpecify an output file.
-qRun in quiet mode. Doesn't echo messages and warnings.
-rProcess directories and files recursively.
-sRun in silent mode.
-yAnswer yes to all questions

Some commands which are used frequently have some useful option set, of which I will be talking here.

1) ls - The first command that we use on opening the terminal. It has a lot of options that make it one of the most powerful tools. But remembering them is a task in itself and only few are used frequently.

A handy pack is called the "-sail" set (Go ahead and attach meaning to it)

This set of options generally produces the most effective listing with the most common details.
  • -s = Block size of files
  • -a = list all files
  • -i = lists the file serial no popularly known as the inode number. which is a unique representation of the file at the system level. This value can be used in all machine level implementations and internal representation scenarios. (Go ahead and think of ways you can use it )
  • -l = long format, which produces the classical listing of files with their extended information.

A snapshot helps in understanding (with the normal -l option)

And combined with the above options :-

2) touch - Used to create an empty file of size 0 bytes. Useful when we want to write to it later.

Observe the "0" after the "staff" which indicates the size of the file

3) cp - copy command. Some useful options are
  • -p = preserve file access and modification times of the original file. Basically keeps the same timestamp as the original file instead of marking the timestamp for the creation of the new file
A snapshot without the -p option: (Pay close attention to the timestamps of the files)

    With the -p option

Also, rcp is a command used for remote file copy. Of course, one needs to have at least read access to the remote path. That is a different story altogether.
4) ps - process listing. This is another powerful command with a whole bunch of options to choose from. But it becomes again difficult to remember all of them and their meanings. So a concise set is -ef if we want to see everything running on our system, where
  • -e = show all processes
  • -f = show formatted listing for processes.
Without the -e option one gets a very minimal set of processes(those belonging to the current user and running on the current terminal)

With the -ef option
Observe the difference.

The columns displayed with the full listing have the following meaning:-

  • UID = User Id. (assigned by the system to each user)
  • PID = Process Id. (assigned by the system to each running process)
  • PPID = Parent Process Id. (Id of the process which spawned the current process. 0 if it is an independent process)
  • C = CPU utilization over the lifetime of the process
  • STIME = system time when the process started
  • TTY = Terminal device from which the process was launched. For most processes it is ?? as they are system processes and not launched from any terminal. For processes that were launched by users explicitly or indirectly one can get values for this column.
  • TIME = Total CPU time needed by the process to run.
  • CMD = Command name that started this process. As everything in UNIX based OS is a process.

1 comment: