Monday, 20 August 2012

Basic Linux Commands

1. BASH ( Bourne-Again SHell ) is the most common shell in Linux. It’s freeware shell.
cat /etc/shells -> To find the available shells in your system
/bin/shell-name  -> For temporary changing of the shell
/bin/sh :  To change to the sh shell.
To return back to the bash shell : /bin/bash
2. To print your home directory location:
echo $HOME
3. To print colouful text
echo -e ” \ 033[31m Hello  World” Note : There is no space between the \ and 0
Output :
Hello World ( in Red )
Set foreground color:
echo -e ” \ 033[3xm” ( where x = 0 – 7 ) Note : There is no space between the \ and 0
Set background color :
echo -e ” \ 033[4ym” ( where y = 0 – 7 )
Note : There is no space between the \ and 0
4. The sort command can be used to sort lines of text files. It can be used for sorting of both numbers and characters or words.
For example if your file myfile.txt contains the following data :
Raju
Khanal
aayush
If you want to sort it the following command gives you the sorted list
sort myfile.txt
Output :
aayush
Khanal
Raju
5. The cut command is used for removing sections from each line of files. For instance consider a file myfile.txt with the following data
IIT2009009:Raju:Khanal
IIT2009010:Pankaj:Bhansali
cut -c1-10 myfile.txt
Output :
IIT2009009
IIT2009010
cut -c4,8 myfile.txt
Output:
20
20
cut -d: -f2 myfile.txt
Output:
Raju
Pankaj
6. The paste command is used for the merging the lines of files.
paste <  file1 > < file2 >
Output shows that the two files are combined line-by-line with tabs separating the two.
7. The join Command is used for joining lines of two files on a common field.
File1 data
IIT2009009 30
IIT2009010 40
File2 data :
IIT2009009 40
IIT2009010 50
join File1 File2
Output :
IIT2009009 30 40
IIT2009010 40 50
Note : Make sure the files are in sorted order.
8. uniq command is used to omit the repeated lines.
Myfile.txt data
Raju Khanal
Raju Khanal
Aayush Khanal
uniq -u myfile.txt -> For non-duplicated lines use the -u flag.
Output :
Aayush Khanal
uniq -c myfile.txt -> For counting the number of times each one appears -c flag is used.
Output :
2 Raju Khanal
1 Aayush Khanal
9. spell : It is a spell checking program which prints each misspelled word on a line of its own. When used with the -n option it gives the line numbers before lines.
spell -n myfile..txt gives the line number and the misspelled word in the myfile..txt file.
10. finger command : The finger command displays information about the system users.
11. Say you want to add a user but don’t know the command to do it. Don’t worry with the power of the Linux command line you don’t have to mug up the commands. You can use the man command with the option -k to search by keyword user for relevant commands.
Note : The man -k is equivalent to apropos command which is used to search the short manual page descriptions for keywords and display any matches.
12. The general forms of the sed command are as follows:
Substitution sed:  ‘s/regexp/replacement/g’ < file >
Deletion sed:  ‘< start >,< end > d’ < file >
sed ‘s/raju/aayush/g’ myfile.txt :
=> To change all occurrences of the word ‘raju’ to ‘aayush’ in the myfile.txt.txt filewhere “s” means substitute, and the “g” means make a global  change. If you leave off the “g” only the first occurrence on each line is changed
sed ’2,3d’ myfile.txt
=> To remove lines starting from line number 2 and ending in line number 3 including the third line
13. With the ‘awk’ command, you can substitute words from an input file’s lines for words in a template or perform calculations on numbers within a file.
Ex : Consider a file with the following data
Raju is 57 Kg and Aayush is 60 Kg.
Pankaj is 60 Kg and Pramod is 55 Kg.
awk ‘{ print $1 ” and ” $6  }’ < filename >
Output :
Raju and Aayush
Pankaj and Pramod

awk ‘{ print “Average of both are ” ( $3 + $8 ) / 2 }’ < filename >
Output :
Average of both are 58.5
Average of both are 57.5

14. expr is used for evaluating expressions.
Ex: expr 10 + 3 gives 13.
Note: For multiplication it is \* and not *.
15. bc , the , Linux calculator program can be used to perform arithmetic calculation.
For instance if you want to multiply 10 and 3, just use the bc command and then give the expression 10 * 3 to get the desired output.
Note : In this case the multiplication symbol is the traditional “*” instead of “\*” as we used in expr .
16. whatis is a command that displays the manual page descriptions. If you explore your Linux system, you will find a lot of programs and you may not know what they do.
Ex : whatis cat
Output :
cat (1) – concatenate files and print on the standard output.
Note : man -f cat gives the same output.
17. The tee command reads from standard input and write to standard output and files.
ls -l | tee dir_list => For directing the output of ls -l to dir_list file.
When used with the -a option it is used for appending ( remember the >> operator )
18. zcat will read gzipped files without needing to uncompress them first.
zcat myfile.txt.gz => To read the gzipped file myfile.txt.gz
19. The file command is used to determine the file type.
A directory known as abc if cannot be identified from its color, can be identified from the file command.

file abc
Output :
abc: directory

20. The diff command is used to compare files line by line. When used with the -y option ( side by side ) the output is obtained in two columns differentiating the two files increasing readability.
Note : The sign “|” in the output indicates that there is a difference whereas the sign “>” and “<” indicate what is left out or added.
21. The cat command provides three useful options -v for displaying non-printing characters , -t prints “^I” for each Tab in the file and -e prints a “$” at the end of each line to indicate a NULL character.

cat -vet < filename >
22. mkdir used with the -p option creates nested directories.
23. The tac command concatenates and prints files in reverse.
24. The dict command can be used to find out the meaning of any word.
dict encumbered
gives you the meaning, thesarus words and sample sentences of the word encumbered.
Note: The dict package is neccessary for the usage of the dict command.
25. The style command analyses the surface characteristics of a document giving the sentence info, word usage, sentence beginnings and so on.
26. The locate command can be used to find files by name. It is an alternative to the find command and can be really useful.
locate < filename > gives the path of that filename.

40 Basic Linux Commands

1.  Everything in Linux is a file including the hardware and even the directories.
2. # : Denotes the super(root) user
3.  $ : Denotes the normal user
4.  /root: Denotes the super user’s directory
/home: Denotes the normal user’s directory.
5.  Switching between Terminals
§  Ctrl + Alt + F1-F6: Console login
§  Ctrl + Alt + F7: GUI login
6.  The Magic Tab: Instead of typing the whole filename if the unique pattern for a particular file is given then the remaining characters need not be typed and can be obtained automatically using the Tab button.
7.   ~(Tilde): Denotes the current user’s home directory
8.   Ctrl + Z: To stop a command that is working interactively without terminating it.
9.  Ctrl + C: To stop a command that is not responding. (Cancellation).
10.  Ctrl + D: To send the EOF( End of File) signal to a command normally when you see ‘>’.
11.  Ctrl + W: To erase the text you have entered a word at a time.
12.  Up arrow key: To redisplay the last executed command. The Down arrow key can be used to print the next command used after using the Up arrow key previously.
13.  The history command can be cleared using a simple option –c (clear).
14.  cd :   The cd command can be used trickily in the following ways:
cd : To switch to the home user
cd * : To change directory to the first file in the directory (only if the first file is a directory)
cd .. : To move back a folder
cd - : To return to the last directory you were in
15.  Files starting with a dot (.) are a hidden file.
16.   To view hidden files: ls -a
17.   ls: The ls command can be use trickily in the following ways:
ls -lR : To view a long list of all the files (which includes directories) and their subdirectories recursively .
ls *.* : To view a list of all the files with extensions only.
18.   ls -ll: Gives a long list in the following format
drwxr-xr-x 2 root root 4096 2010-04-29 05:17 bin where
drwxr-xr-x : permission where d stands for directory, rwx stands for owner privilege, r-x stands for the group privilege and r-x stands for others permission respectively.
Here r stands for read, w for write and x for executable.
2=> link count
root=>owner
root=>group
4096=> directory size
2010-04-29=>date of creation
05:17=> time of creation
bin=>directory file(in blue)

The color code of the files is as follows:
Blue: Directory file
White: Normal file
Green: Executable file
Yellow: Device file
Magenta: Picture file
Cyan: link file
Red: Compressed file
File Symbol
-(Hyphen) : Normal file
d=directory
l=link file
b=Block device file
c=character device file
19.  Using the rm command: When used without any option the rm command deletes the file or directory ( option -rf) without any warning. A simple mistake like rm / somedir instead of rm /somedir can cause major chaos and delete the entire content of the /(root) directory. Hence it is always advisable to use rm command with the -i(which prompts before removal) option. Also there is no undelete option in Linux.
20.  Copying hidden files: cp .* (copies hidden files only to a new destination)
21. dpkg -l : To get a list of all the installed packages.
23. Use of ‘ > ‘ and ‘ >> ‘ : The ‘ > ‘ symbol ( input redirector sign) can be used to add content to a file when used with the cat command. Whereas ‘ >> ‘ can be used to append to a file. If the ‘ >> ‘ symbol is not used and content is added to a file using only the ‘>’ symbol the previous content of the file is deleted and replaced with the new content.
e.g: $ touch text (creates an empty file)
$ cat >text
This is text’s text. ( Save the changes to the file using Ctrl +D)
$cat >> text
This is a new text. (Ctrl + D)
Output of the file:
This is text’s text.
This is a new text.

23.  To count the number of users logged in : who |wc –l

24.  cat:  The cat command can be used to trickly in the following way:
- To count no. of lines from a file : cat <filename> |wc -l
- To count no. of words from a file : cat <filename> |wc -w
- To count no. of characters from a file : cat <filename> |wc –c

25.  To search a term that returns a pattern: cat <filename> |grep [pattern]

26.  The ‘tr’ command: Used to translate the characters of a file.
tr ‘a-z’ ‘A-Z’ <text >text1 : The command for example is used to translate all the characters from lower case to upper case of the ‘text’ file and save the changes to a new file ‘text1′.
27.  File permission using chmod: ‘chmod’ can be used directly to change the file permission of files in a simple way by giving the permission for root, user and others in a numeric form where the numeric value are as follows:
r(read-only)=>4
w(write)=>2
x(executable)=>1
e.g. chmod 754 text will change the ownership of owner to read, write and executable, that of group to read and executable and that of others to read only of the text file.
28.  more: It is a filter for paging through text one screenful at a time.
Use it with any of the commands after the pipe symbol to increase readability.
e.g. ls -ll |more
29.  cron : Daemon to execute scheduled commands. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates.
1 * * * * echo “hi” >/dev/tty1 displays the text “hi” after every 1 minute in tty1
.—————- minute (0 – 59)
| .————- hour (0 – 23)
| | .———- day of month (1 – 31)
| | | .——- month (1 – 12) OR jan,feb,mar,apr …
| | | | .—– day of week (0 – 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
* * * * * command to be executed
Source of example: Wikipedia
30.  fsck: Used for file system checking. On a non-journaling file system the fsck command can take a very long time to complete. Using it with the option -c displays a progress bar which doesn’t increase the speed but lets you know how long you still have to wait for the process to complete.
e.g. fsck -C
31.  To find the path of the command: which command
e.g. which clear
32. Setting up alias: Enables a replacement of a word with another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command
e.g. alias cls=’clear’ => For buffer alias of clear
33.  The du (disk usage) command can be used with the option -h to print the space occupied in human readable form. More specifically it can be used with the summation option (-s).
e.g. du -sh /home summarizes the total disk usage by the home directory in human readable form.
34.  Two or more commands can be combined with the && operator. However the succeeding command is executed if and only if the previous one is true.
e.g. ls && date lists the contents of the directory first and then gives the system date.
35.  Surfing the net in text only mode from the terminal: elinks [URL]
e.g: elinks www.google.com
Note that the elinks package has to be installed in the system.
36.  The ps command displays a great more deal of information than the kill command does.
37.  To extract a no. of lines from a file:
e.g head -n 4 abc.c is used to extract the first 4 lines of the file abc.c
e.g tail -n 4 abc.c is used to extract the last 4 lines of the file abc.c
38.  Any changes to a file might cause loss of important data unknowingly. Hence    Linux creates a file with the same name followed by ~ (Tilde) sign without the recent changes. This comes in really handy when playing with the configuration files as some sort of a backup is created.
39.   A variable can be defined with an ‘=’ operator. Now a long block of text can be assigned to the variable and brought into use repeatedly by just typing the variable name preceded by a $ sign instead of writing the whole chunk of text again and again.
e.g ldir=/home/my/Desktop/abc
cp abcd $ldir copies the file abcd to /home/my/Desktop/abc.
40. To find all the files in your home directory modified or created today:
e.g. find ~ -type f -mtime 0

Server Control View State

View state is a fancy name for ASP.NET storing some state data in a hidden input field inside the generated page. When the page is posted back to the server, the server can parse, validate, and apply this view state data back to the page's tree of controls. View state is a very powerful capability since it allows state to be persisted with the client and it requires no cookies or server memory to save this state. Many ASP.NET server controls use view state to persist settings made during interactions with elements on the page, for example, saving the current page that is being displayed when paging through data.
There are a number of drawbacks to the use of view state, however. First of all, it increases the total payload of the page both when served and when requested. There is also an additional overhead incurred when serializing or deserializing view state data that is posted back to the server. Lastly, view state increases the memory allocations on the server.
Several server controls, the most well known of which is the DataGrid, tend to make excessive use of view state, even in cases where it is not needed. The default behavior of the ViewState property is enabled, but if you don't need it, you can turn it off at the control or page level. Within a control, you simply set the EnableViewState property to false, or you can set it globally within the page using this setting:
<%@ Page EnableViewState="false" %>
If you are not doing postbacks in a page or are always regenerating the controls on a page on each request, you should disable view state at the page level.

Use Gzip Compression

While not necessarily a server performance tip (since you might see CPU utilization go up), using gzip compression can decrease the number of bytes sent by your server. This gives the perception of faster pages and also cuts down on bandwidth usage. Depending on the data sent, how well it can be compressed, and whether the client browsers support it (IIS will only send gzip compressed content to clients that support gzip compression, such as Internet Explorer 6.0 and Firefox), your server can serve more requests per second. In fact, just about any time you can decrease the amount of data returned, you will increase requests per second.
The good news is that gzip compression is built into IIS 6.0 and is much better than the gzip compression used in IIS 5.0. Unfortunately, when attempting to turn on gzip compression in IIS 6.0, you may not be able to locate the setting on the properties dialog in IIS. The IIS team built awesome gzip capabilities into the server, but neglected to include an administrative UI for enabling it. To enable gzip compression, you have to spelunk into the innards of the XML configuration settings of IIS 6.0 (which isn't for the faint of heart). By the way, the credit goes to Scott Forsyth of OrcsWeb who helped me figure this out for the www.asp.net severs hosted by OrcsWeb.
Rather than include the procedure in this article, just read the article by Brad Wilson at IIS6 Compression. There's also a Knowledge Base article on enabling compression for ASPX, available at Enable ASPX Compression in IIS. It should be noted, however, that dynamic compression and kernel caching are mutually exclusive on IIS 6.0 due to some implementation details.

Run IIS 6.0 (If Only for Kernel Caching)

If you're not running IIS 6.0 (Windows Server 2003), you're missing out on some great performance enhancements in the Microsoft Web server. In Tip 7, I talked about output caching. In IIS 5.0, a request comes through IIS and then to ASP.NET. When caching is involved, an HttpModule in ASP.NET receives the request, and returns the contents from the Cache.
If you're using IIS 6.0, there is a nice little feature called kernel caching that doesn't require any code changes to ASP.NET. When a request is output-cached by ASP.NET, the IIS kernel cache receives a copy of the cached data. When a request comes from the network driver, a kernel-level driver (no context switch to user mode) receives the request, and if cached, flushes the cached data to the response, and completes execution. This means that when you use kernel-mode caching with IIS and ASP.NET output caching, you'll see unbelievable performance results. At one point during the Visual Studio 2005 development of ASP.NET, I was the program manager responsible for ASP.NET performance. The developers did the magic, but I saw all the reports on a daily basis. The kernel mode caching results were always the most interesting. The common characteristic was network saturation by requests/responses and IIS running at about five percent CPU utilization. It was amazing! There are certainly other reasons for using IIS 6.0, but kernel mode caching is an obvious one.

Page Output Caching and Proxy Servers

ASP.NET is your presentation layer (or should be); it consists of pages, user controls, server controls (HttpHandlers and HttpModules), and the content that they generate. If you have an ASP.NET page that generates output, whether HTML, XML, images, or any other data, and you run this code on each request and it generates the same output, you have a great candidate for page output caching.
By simply adding this line to the top of your page
<%@ Page OutputCache VaryByParams="none" Duration="60" %> 
you can effectively generate the output for this page once and reuse it multiple times for up to 60 seconds, at which point the page will re-execute and the output will once be again added to the ASP.NET Cache. This behavior can also be accomplished using some lower-level programmatic APIs, too. There are several configurable settings for output caching, such as the VaryByParams attribute just described. VaryByParams just happens to be required, but allows you to specify the HTTP GET or HTTP POST parameters to vary the cache entries. For example, default.aspx?Report=1 or default.aspx?Report=2 could be output-cached by simply setting VaryByParam="Report". Additional parameters can be named by specifying a semicolon-separated list. Many people don't realize that when the Output Cache is used, the ASP.NET page also generates a set of HTTP headers that downstream caching servers, such as those used by the Microsoft Internet Security and Acceleration Server or by Akamai. When HTTP Cache headers are set, the documents can be cached on these network resources, and client requests can be satisfied without having to go back to the origin server.
Using page output caching, then, does not make your application more efficient, but it can potentially reduce the load on your server as downstream caching technology caches documents. Of course, this can only be anonymous content; once it's downstream, you won't see the requests anymore and can't perform authentication to prevent access to it.

Background Processing in ASP.NET

The path through your code should be as fast as possible, right? There may be times when you find yourself performing expensive tasks on each request or once every n requests. Sending out e-mails or parsing and validation of incoming data are just a few examples.
When tearing apart ASP.NET Forums 1.0 and rebuilding what became Community Server, we found that the code path for adding a new post was pretty slow. Each time a post was added, the application first needed to ensure that there were no duplicate posts, then it had to parse the post using a "badword" filter, parse the post for emoticons, tokenize and index the post, add the post to the moderation queue when required, validate attachments, and finally, once posted, send e-mail notifications out to any subscribers. Clearly, that's a lot of work.
It turns out that most of the time was spent in the indexing logic and sending e-mails. Indexing a post was a time-consuming operation, and it turned out that the built-in System.Web.Mail functionality would connect to an SMTP server and send the e-mails serially. As the number of subscribers to a particular post or topic area increased, it would take longer and longer to perform the AddPost function.
Indexing e-mail didn't need to happen on each request. Ideally, we wanted to batch this work together and index 25 posts at a time or send all the e-mails every five minutes. We decided to use the same code I had used to prototype database cache invalidation for what eventually got baked into Visual Studio® 2005.
The Timer class, found in the System.Threading namespace, is a wonderfully useful, but less well-known class in the .NET Framework, at least for Web developers. Once created, the Timer will invoke the specified callback on a thread from the ThreadPool at a configurable interval. This means you can set up code to execute without an incoming request to your ASP.NET application, an ideal situation for background processing. You can do work such as indexing or sending e-mail in this background process too.
There are a couple of problems with this technique, though. If your application domain unloads, the timer instance will stop firing its events. In addition, since the CLR has a hard gate on the number of threads per process, you can get into a situation on a heavily loaded server where timers may not have threads to complete on and can be somewhat delayed. ASP.NET tries to minimize the chances of this happening by reserving a certain number of free threads in the process and only using a portion of the total threads for request processing. However, if you have lots of asynchronous work, this can be an issue.