Wednesday, September 28, 2011

Linux file system

1. Standard directory structure
  • / - the topmost
  • /dev - all the devices are accessible as files
  • /var - "variable" data such as mails, log files, databases
  • /usr - almost all the packages installed
  • /etc - con?guration files
  • /home - home directories for all the users
  • /root - home directory of the privileged user root
  • /mnt - used to mount other directories/partitions
2. File Attributes
  • To see the file attributes type ls -l on your terminal
ls -l
total 156-rw-r--r--  1 root root   1053 Sep 22  2010 anaconda-ks.cfg-rw-r--r--  1 root root 127004 Sep 22  2010 CHECKSUMSdrwxr-xr-x  2 root root   4096 Aug  3 00:02 Desktop-rw-r--r--  1 root root    100 Jun  6 22:14 svn-checkout-command
    •  It has 10 characters, first character is d if its directory and - if its file.
    • Next 9 characters are divided into three groups with a set of 3 characters each
    • First 3 characters - Owner of the file or directory
    • Next 3 characters - Group
    • Last 3 characters - Others
    • r - Read i.e. File or directory is readable
    • w - Write i.e. File or directory is writable
    • x - Execute i.e. File or directory is executable
    • -rw-r–r– means it has read, write but not execute permissions for the owner of the file, only read permissions for the group and only read permissions for others.
    • The third column of the command ls -l tells about the owner of the file, next column tells to which group it belongs
3. Changing the File attributes
chmod Changing the permissions of the file
Usage: 
$ chmod o+x Testing.java
$ chmod 655 Testing.java

4. Changing ownership
chown command is used for changing the ownership and also group of the file
Usage:
$ chown guest Testing.java
$ chown guest:guest Testing.java

5. File system commands
  • Deleting Files - rm
  • Copying and moving files - cp, mv
  • Creating directories - mkdir
  • Deleting Empty Directory - rmdir
$ rm Testing.java
//deletes the file Testing.java
$ cp Testing.java Copy.java
//creates the copy of Testing.java
$ mv Testing.java Test.java
//renames the file Testing.java to Test.java
$ mkdir newDir
//Creates directory newDir
$ rmdir newDir
//deletes directory newDir newDir should be empty.

General Purpose utilities commands in Linux


1. Calender
cal: Command to see calender for any specific month or a complete year
Usage: 
$ cal [ [month] year]
Example:
$ cal april 2009
Output:
April 2009
Su Mo Tu We Th Fr Sa
 1  2  3  4
5   6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

2. Date
date: displays the current date
Usage:
$ date
Output:
Tue Apr 21 21:33:49 IST 2009
user$ date +"%D %H:%M:%S"
04/21/09 21:35:02
Options:
d - The da of the month (1-31)
y - The last two digits of the year
H,M,S - Hour Minute and second respectively
D - the date in mm/dd/yy

3. echo
echo: Print message on the terminal
Usage:
echo ""
Example:
$ echo "Welcome to the workshop"
Output:
Welcome to the workshop

4. printf
printf: Print the formatted message on the terminal
Usage:
printf ""
Example:
$ printf "the amount is %d\n" 100
Output:
the amount is 100

5. Calculator
bc: A text based calculator
Usage:
$ bc
2*10+20-9+4/2 [Input]
33 [Output]
[ctrl+d] [Quit]

xcalc is graphical based calculator

6. script
script command records your session and stores it in a file
Usage:
$ script
Script started, file is typescript
$ echo "this is a sample script"
this is a sample script
$ [ctrl+d]
Script done, file is typescript

By default if you dont specify any file name the contents will be stored in file name typescipt
Usage:
$ cat typescript
Script started on Tuesday 21 April 2009 10:07:00
$ echo "this is a sample script"
this is a sample script
$
Script done on Tuesday 21 April 2009 10:07:34 PM

7. passwd
passwd command allows you to change your password
Usage:
$ passwd
Changing password for user123.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

8. WHO: Who are the users?
who command tells you the users currently logged on to the system
Usage:
$ who
root     :0           Sep  4 22:47
root     pts/1        Sep  4 22:48 (:0.0)

9. man - The reference Manual
man displays the documentation for a command
Usage:
man
Example:
$ man ls
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the
none of -cftuvSUX nor --sort.