Friday, January 7, 2011

How to change MAC address of a network interface in Linux

Type the following commands with sudo prefix or under superuser/root:

ifconfig eth0 down
ifconfig eth0 hw ether 00:24:21:3e:05:ee 

ifconfig eth0 up 

eth0 – is hardware name of your first network interface.
00:24:21:3e:05:ee is new MAC address you’d like to apply to the NIC.

These commands should be added into startup scripts if you require them to appear after Linux system reboots.

Thursday, January 6, 2011

ls command

List information about the FILEs (the current directory by default).

ls [OPTION]... [FILE]... 
-a    Shows you all files, even files that are hidden (these files begin with a . dot.)
-A    List all files including the hidden files. However, does not display the working directory (.) or the parent directory (..).
-b    Force printing of non-printable characters to be in octal \ddd notation.
-c    Use time of last modification of the i-node (file created, mode changed, and so forth) for sorting (-t) or printing (-l or -n).
-C    Multi-column output with entries sorted down the columns. Generally this is the default option.
-d    If an argument is a directory it only lists its name not its contents.
-f   Force each argument to be interpreted as a directory and list the name found in each slot. This option turns off -l, -t, -s, and -r, and turns on -a; the order is the order in which entries appear in the directory.
-F    Mark directories with a trailing slash (/), doors with a trailing greater-than sign (>), executable files with a trailing asterisk (*), FIFOs with a trailing vertical bar (|), symbolic links with a trailing at-sign (@), and AF_Unix address family sockets with a trailing equals sign (=).
-g    Same as -l except the owner is not printed.
-h     print sizes in human readable format. (e.g., 1K, 234M, 2G, etc.)
-i    For each file, print the i-node number in the first column of the report.
-l    long listing of a file or a directory contents (permissions, owners, size, and when last modified.)
-L    If an argument is a symbolic link, list the file or directory the link references rather than the link itself.
-m    Stream output format; files are listed across the page, separated by commas.
-n    The same as -l, except that the owner's UID and group's GID numbers are printed, rather than the associated character strings.
-o    The same as -l, except that the group is not printed.
-p    Displays a slash ( / ) in front of all directories.
-q    Force printing of non-printable characters in file names as the character question mark (?).
-r    Reverses the order of how the files are displayed.
-R    Includes the contents of subdirectories.
-s    Give size in blocks, including indirect blocks, for each entry.
-S   Sort by file size
-t    Shows you the files in modification time.
-u    Use time of last access instead of last modification for sorting (with the -t option) or printing (with the -l option).
-x    Displays files in columns.
-Z  Print any SELinux security context of each file
-1   Print one entry per line of output.
pathnames    File or directory to list.


ls -l
-rw-r----- 1 lalit admin 7277202 Jan 06 14:29 go2linuxworld.txt
1st CharacterFile Type: First character specifies the type of the file. 
In the example above the hyphen (-) in the 1st character indicates that this is a normal file. Following are the possible file type options in the 1st character of the ls -l output.
          - normal file
          d directory
          s socket file
          l link file 
Field 1 – File Permissions: Next 9 character specifies the files permission. Each 3 characters refers to the read, write, execute permissions for user, group and world In this example, -rw-r—– indicates read-write permission for user, read permission for group, and no permission for others.
Field 2 – Number of links: Second field specifies the number of links for that file. In this example, 1 indicates only one link to this file. 
Field 3 – Owner: Third field specifies owner of the file. In this example, this file is owned by username ‘lalit’.
Field 4 – Group: Fourth field specifies the group of the file. In this example, this file belongs to 'admin’ group.
Field 5 – Size: Fifth field specifies the size of file. In this example, ’7277202′ indicates the file size.
Field 6 – Last modified date & time: Sixth field specifies the date and time of the last modification of the file. In this example, ‘Jan 06 14:29′ specifies the last modification time of the file.
Field 7 – File name: The last field is the name of the file. In this example, the file name is go2linuxworld.txt

ls ~
List the contents of your home directory by adding a tilde after the ls command.

ls /
List the contents of your root directory.

ls ../
List the contents of the parent directory.

ls */
List the contents of all sub directories.

ls -d */

Only list the directories in the current directory.

Wednesday, January 5, 2011

Editing files with the vi editor : An Introduction

The vi program is a text editor that you can use to edit any text and particularly programs.The following are some of the important keystroke commands to get around in vi.

Adding Text in vi
i       Notifies vi to insert text before the cursor
I       Insert text at the beginning of the current line
a       Notifies vi to append text after the cursor
A       Add text to the end of the current line.
o       Open a new line below the current line and add text
O       Open a new line above the current line and add text
s       Substitute the letter underneath the cursor with letter you type, and insert text
S or c   Delete the current line and substitute it with text you type
R or C   Replace current text with text you type

Cutting and Pasting Text in vi
D        Delete from the cursor to the end of the line
dd      Notifies vi to delete the current line
#dd    Delete the following number of lines, including the current line (where # is a number)
yy      yank (i.e. copy) one line into a general buffer
#yy   Copy or "yank" the following number of lines, including the current line (where # is a number)
x        Notifies vi to delete the current character
dw      Delete the letter beneath the cursor and the rest of the word
#dw    Delete the following number of words, including the current word (where # is a number)
p        Paste data that was cut with x or dd commands or copy with yy command
J        Join the next line with the current line (erases a carriage return)
u        Notifies vi to undo the last command
      Redo the last editing command

Moving Around in a vi Text File
Ctrl+f   Scroll up one page
Ctrl+b    Scroll down one page
/string  Search forward for string

Saving or Quitting vi
:q     Quit editor
:q!   Quit editor without saving changes
:w    Save the current file without quitting
:wq    Save changes and exit editor

:f    Display filename and current line number
Esc  Notifies vi to end the insert or append mode

How to list or find the largest files, directories and Free disk space

♦ Find biggest top 10 directories in your disk

du -hs */ | sort -nr | head

♦ Find the biggest files inside directories

ls -lhS | head

♦ Find biggest files in any directory recursively

We’ll now use find, to find only files, and then sort, to have only the biggest files listed.

find -type f -ls | sort -k 7 -r -n | head -5

find helps us list only files and not directories, then sort using the column 7 (the column with the file size) we sort using -n numeric order and -r reverse order (from biggest to smallest), and finally only the first 5 files in the current directory, and sub-directory.

You can use this way

find / -type f -ls | sort -k 7 -r -n | head -5
And that will work, for all disk from root. 

du Summarize disk usage of each FILE, recursively for directories.
sort Write sorted concatenation of all FILE(s) to standard output.
head Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input.


Tuesday, January 4, 2011

How to find the largest file in a directory

The best way is to use ls, sorted by size and to get the biggest one, use head

ls -S | head -1

How to count how many processes each user is running in Linux

There are lots of ways to do this, we can use pipes here.

ps hax -o user | sort | uniq -c

ps will list the processes
h will remove the header
-o user prints only the user column
sort sorts in alphabetical order
uniq command can eliminate or count duplicate lines in a presorted file.