Monday, May 27, 2013

UNIX: Terminal, File System, Users and Editors


1.  Command-line "Terminal"

You can provide additional options to a command; you can pipe the output of one command into another command; you can put a set of commands in a script to automate a task.
bash (Bourne Again Shell)
$ pwd  //print working dir 
.......

2.  Unix File System

Root Directory
Everything in Unix is a file - data files. Files are organized in directories (aka folders). The directories are organized in a hierarchical tree structure, starting from the root directory. 
"/usr/lib/jvm/jdk1.7.0_07/bin/javac". The leading "/" (forward slash) denotes the root directory. The sub-directories are also separated by a "/".
There is only one root directory for the entire Unix's file system.
Notes: Windows use "\" (back slash) as the directory separator, and may contain multiple root directories - one for each drive (e.g., c:\d:\).
one root dir in UNIX
multiple root dir in Windows, 
Home Directory
Unix is a multi-user operating system. home directory (for each user). The users' home directories are allocated under /home for Ubuntu.  The home directory of the current login user is denoted as "~". It contains sub-directories such as ~/Desktop~/Downloads~/Documents~/Movies,~/Music, and etc.
Pathname and Filename
To reference a file, you need to provide the pathname (directory and sub-directories names) and the filename. For example, in "/usr/lib/jvm/jdk1.7.0_07/bin/javac", the pathname is "/usr/lib/jvm/jdk1.7.0_07/bin/" and the filename is "javac".
The pathname can be specified in two ways:
  1. Absolute Pathname: An absolute path begins from the root directory (starts with "/"), and contains all the sub-directories (separated with "/") leading to the file, e.g., "/usr/lib/jvm/jdk1.7.0_07/bin/" (starting with root). An absolute path can also begin with the current user's home directory (starts with "~"), e.g., "~/Downloads/jdk" (starting with home dir). Suppose the current user is "peter", it maps to "/home/peter/Downloads/jdk" (in Linux).
  2. Relative Pathname: A relative path is relative to the so-called current working directory (pwd). A relative path does not begin with "/" or "~". For example, if the current working directory is "/usr/lib/jvm", then the relative pathname "jdk1.7.0_07/bin" refer to "/usr/lib/jvm/jdk1.7.0_07/bin".
Unix system is case sensitive, a rose is NOT a Rose, and is NOT a ROSE.

3.  Basic Commands

3.1  pwd (Print Current Working Directory)

3.2  cd (Change Working Directory)

To change current working directory, issue command "cd new-pathname" (change directory). You can specify new pathname in two ways: absolute or relative. An absolute path begins with a "/" (root directory) or "~" (home directory). A relative path is relative to the current working directory and does NOT begin with "/" or "~". For example,

3.3  ls (List Directory's Contents)

You can use command ls to list the contents of the current working directory, e.g.,
// List contents of current working directory in short format
$ ls
Desktop    Downloads         Music     Public     Videos
Documents  examples.desktop  Pictures  Templates
 
// List in long format
$ ls -l
total xx
drwxr-xr-x 2 myuser myuser 1024 Mar 22 21:32 Desktop
drwxr-xr-x 2 myuser myuser 1024 Mar 22 21:32 Documents
drwxr-xr-x 2 myuser myuser 1024 Mar 22 21:32 Downloads
-rw-r--r-- 1 myuser myuser 8445 Mar 22 17:30 examples.desktop
......
 
// Append file indicator (e.g., trailing / indicates a directory)
$ ls -F
Desktop/    Downloads/        Music/     Public/     Videos/
Documents/  examples.desktop  Pictures/  Templates/
Wildcard *
You can list selected files using wildcard *, which matches 0 or more (any) characters. For example,
$ ls *.java     // List files ending with ".java" in short format (default)
$ ls -l *.java  // List files ending with ".java" in long format
$ ls -ld my*     // List files and directories beginning with "my" in long format


3.4  catless (Viewing File Contents)

You can use commands catless or more to display contents of a text file on console. cat shows the entire file, which can be inconvenient for long file. less shows one page of the file. For example,
$ cat /proc/cpuinfo
  // Display the entire file
  // Use window's scroll-bar to scroll
 
$ less /proc/cpuinfo
  // Display one page of the file
  // Use Up|Down|PgUp|PgDown key to scroll, and press "q" to quit


3.5  Shortcut Keys - IMPORTANT

Previous Commands: You can use the up/down arrow keys to retrieve the previous/next command in the command history.
Copy/Paste:
  • Copy: shift+ctrl+c
  • Copy: shift+ctrl+v
  • middle-button: copy, paste, highlight
  • interrupt signial: ctrl+c
  • end of text input: ctrl+d
  • suspending current process: ctrl+z 

3.6  Other Frequently-used Commands

$ clear                // Clear Screen
$ who                  // List all login users
$ whoami               // Show the current login user
$ which program-name   // Show the location of the program, e.g., "which javac"

3.7  Getting Helps

$ help                // Show all the available commands
$ help command-name   // Show help for a specific command, e.g., "help cd"
$ man command-name    // Show the manual page for a specific command, e.g., "man cd"

4.  If you need to Install Software or Perform System tasks...

4.1  Regular Users vs. Superuser


To enable a user to sudo:
  • In Ubuntu: System Settings ⇒ User Accounts ⇒ Select the user ⇒ Unlock ⇒ Set the account type to "Administrator" (instead of "Standard User"), which adds the user to "sudo" group.

4.2  Processes

Unix is a multi-process, multi-user operating system. It can run many processes concurrently.
You can use GUI applications to view all running processes and terminate a particular process (similar to "Task Manager" in Windows).
  • In Mac OS X: launch "Activity Monitor" (Under /Applications/Utilities) and select "All Processes".
On command-line, you can use command ps to list all the processes and kill to terminate a particular process:
$ ps            // Print processes of current user
$ ps -e         // Print all processes
$ ps -ef        // Print all processes in full-listing
$ ps -ef | grep "mysql"  // Print processes containing mysql
$ ps aux | grep "mysql"  // same as above
$ top           // Display resource usage and top processes
 
$ kill pid      // Kill a particular possess with the given processID
$ kill -9 pid   // Force kill
in Windows: taskkill

5.2  Display Text files in Command-line - catlesshead and tail

If possible, use a graphical text editor to display a text file. Nonetheless, you can use the following commands to display a text file in command-line:
  • cat filename: Concantenate file or print the file to console.
  • less filename: Display the file in pages. You can scroll the display with up, down, pageup, pagedown keys. less replaces the legacy more.
  • head|tail filename: Display the first|last part of the file.
For example, let's display the /proc/cpuinfo file with these commands:
$ cat /proc/cpuinfo
   // Show entire file
   // Use the window's scroll-bar to scroll
 
$ less /proc/cpuinfo
   // Show one page
   // Use the Up|Down|PgUp|PgDown key to scroll
 
$ head /proc/cpuinfo
   // Show first page of the file
 
$ tail /proc/cpuinfo
   // Show last page of the file
More on cat
The command cat has many usages. You can use cat to concatenate (join) a few files. For example,
$ cat file1 file2 file3
   // display file1, file2 and file3 on the console
 
$ cat file1 file2 file3 > file4
   // Concatenate file1, file2 and file3 as file4
You can also use cat command to create a small text file. cat without argument uses standard input as its input. You could use ctrl+D to signal EOF. For example:
$ cat > out.txt
The quick brown fox jumps over the lazy dog[ctrl+D]
$ cat out.txt
The quick brown fox jumps over the lazy dog

done 

No comments:

Post a Comment