Pages

C3 Health Services

Visit Official Website 9278982994

Expert Healthcare at Your Doorstep

Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Thursday, 16 January 2014

Linux vs Windows Web Server Hosting: Which one to choose and why?

Linux vs Windows Web Server Hosting: Which one to choose and why?

When you think of hosting your website, the first question strikes in your mind is that which web server hosting should you choose? Should you go for Linux web server hosting or Windows web server hosting. I have tried to compare both Linux and Windows web server hosting. Choosing between Linux and Windows web server hosting mainly depends upon your need and the nature of your website, which programming language and database are you using etc. Let's discuss in detail on choosing between Linux and Windows web server hosting.

1. Which programming language are you using?

You should choose a Linux Web Server Hosting if you are using PHP, Perl, Python, or CGI scripting. On the other hand, if you are using ASP.NET, go for Windows Web Server Hosting.

2. Which database are you using?

Mainly, the Linux Web Server Hosting accounts use MySQL database and Windows Web Server Hosting accounts use SQL Server (MS SQL). 

Advantages of Linux Web Server Hosting over Windows Web Server Hosting

The following are the advantages of using Linux based web server compared to Windows based web server:

Stable: Linux/Unix operating systems have traditionally been believed to be very stable and robust. A web site housed on a Linux operating system will have very high up-time (of the order of 99.9%). Of course, other factors such as power supply, network admin skills, and network load etc. also matter when it comes to maintaining the system uptime.

Low cost: The Linux OS comes free of cost (or at very insignificant cost, usually cost of distribution). Also, it has full fledged server, and desk top applications that comes free along with the OS. These server applications (such as FTP, Web Server, DNS Server, File Server etc.) being free, are also very stable.

Ease of use: When it comes to web hosting, it is easy to host on Linux web servers. The process of uploading and hosting is almost same for both Linux and Windows web servers. 

Easy to move between hosts: A web site designed to be hosted on a Linux based web server can be hosted on a Windows web server easily, where as the reverse is not always true.

Most widely used: Linux/Unix based web hosting is most widely used compared to Windows based web hosting.

Scalability: A web site is dynamic. Usually, a web site starts with a few pages of html and grows over a period of time to suit the customers requirements. It is preferable to design a web site keeping this requirements in mind. A web site designed for compatibility with a Linux/Unix based web server meets the scalability requirement easily without making any site wide design changes.

Disadvantage of Linux Web Server Hosting 

Less ASP and .NET support: Linux based web server is not fully compatible with Microsoft technologies. While there are many software that enable ASP.NET on Linux machines, it is far less common to find a Linux web hosting platform that supports ASP or .NET. If you are using Microsoft based technologies for your web site, it is preferable to host with a Windows based web server. 

Tuesday, 8 October 2013

Linux Server Monitoring and Networking Commands for Linux Server Administrators - Part 2

Linux Server Monitoring and Networking Commands for Linux Server Administrators - Part 2

I have complied a list Linux server monitoring and networking commands for Linux server administrators. These are the very basic monitoring and networking commands which each Linux server administrator should know. I had also compiled a list of basic Linux Networking and Monitoring commands in my previous article. I will talk about iostat, meminfo, free, mpstat, netstat, nmon, pmap, ps, pstree, sar, strace, tcpdump, top, uptime, vmstat, wireshark Linux server monitoring and networking commands for Linux server administrators. These networking and monitoring Linux server commands helps Linux server administrators to identify input/output problems on your Linux server, memory management of Linux server, performance of Linux server, CPU usage, socket usage, routing, interface, protocol, network statistics and lot more.

1. iostat

The iostat command shows in detail what your storage subsystem is up to. You usually use iostat to monitor how well your storage sub-systems are working in general and to spot slow input/output problems before your clients notice that the server is running slowly. 

2. meminfo and free

Meminfo gives you a detailed list of what's going on in memory. Typically you access meminfo's data by using another program such as cat or grep. For example,

cat /proc/meminfo

gives you the details of what's going on in your server’s memory at any given moment.

For a quick “just the facts” look at memory, you can use the free command. In short, free gives you the overview; meminfo gives you the details.

3. mpstat

The mpstat command reports on the activities of each of the available CPUs on a multi-processor server. These days, thanks to multi-core processors, that’s almost all servers. mpstat also reports on the average activities of all your server's CPUs. It enables you to display overall CPU statistics per system or per processor. This overview can alert you to possible application problems before they get to the point of annoying users.

4. netstat

Netstat, like ps, is a Linux tool that administrators use every day. It displays a lot of network related information, such as socket usage, routing, interface, protocol, network statistics, and more. Some of the most commonly used options are:

-a Show all socket information
-r Show routing information
-i Show network interface statistics
-s Show network protocol statistics

5. nmon

Nmon, short for Nigel's Monitor, is a popular open-source tool to monitor Linux systems performance. Nmon watches the performance information for several subsystems, such as processor utilization, memory utilization, run queue information, disk I/O statistics, network I/O statistics, paging activity, and process metrics. You can then view nmon's real-time system measurements via its curses “graphical” interface.

To run nmon, you start the tool from the shell. Once up, you select the subsystems to monitor by typing in its one-key commands. For example, to get CPU, memory, and disk statistics, you type c, m, and d. You can also use nmon with the -f flag to save performance statistics to a CSV file for later analysis.

For day to day server monitoring I find nmon to be the single most useful program in my Linux system management tool-kit.

6. pmap

The pmap command reports the amount of memory that your server's processes are using. You can use this tool to determine which processes on the server are being allocated memory and whether any of these processes are being piggy with RAM.

7. ps and pstree

The ps and pstree commands are two of the Linux administrator’s best friends. They both provide a list of all currently running processes. Ps tells you how much memory and processor time the server’s programs are using. Pstree shows less information, but highlights which processes are the children of other processes. Armed with this information, you can spot out–of-control processes and kill them off with Linux's “take no prisoners” kill command.

8. sar

The sar program is a Swiss-army knife of a system monitoring tool. The sar command is actually made up of three programs: sar, which displays the data, and sa1 and sa2, which collect and store it. Once installed, sar creates a detailed overview of CPU utilization, memory paging, network I/O and transfer statistics, process creation activity, and storage device activity. The big difference between sar and nmon is that the former is better at long-term system monitoring, while I find nmon to be better at giving me a quick read on my server's status.

9. strace

strace is often thought of a programmer's debugging tool, but it's more than that. It intercepts and records the system calls that are called by a process. This makes it a useful diagnostic, instructional, and debugging tool. For example, you can use strace to find out which configuration file a program is actually using when it starts up.

Strace does have one flaw though. When it's checking out a specific process, that process' performance will fall through the floor. Thus, I only use strace when I already have a darned good reason to think that that program is causing trouble.

10. tcpdump

Tcpdump is a simple, robust network monitoring utility. Its basic protocol analyzing capability enables you to get a rough view of what is happening on your network. To really dig into what's going on with your network however, you'll want to use Wireshark.

11. top

The top command shows what's going on with your active processes. By default, it displays the most CPU-intensive tasks running on the server and updates the list every five seconds. You can sort the processes by PID (Process ID); age, newest first; time, by cumulative time; and resident memory usage and total time it's been using the CPU since startup. I find this a fast and easy way to see if any process is starting to go out of control and about to get into trouble.

12. uptime

Use uptime to see how long the server has been running and how many users are logged on. It also gives you an overview of the average server load. The optimal value of the load is 1 or less, which means that each process has immediate access to the CPU and there are no CPU cycles lost.

14. vmstat

For the most part, you use vmstat to monitor what's going on with virtual memory. Linux constantly uses virtual memory to get the best possible storage performance.

If your applications are taking up too much memory you get excessive page-outs — programs moving from RAM to your system's swap space, which is on the hard drive. Your server can reach a point where it's spending more time managing memory paging than running your applications, a condition called thrashing. When your computer is thrashing, its performance falls through the floor. 

Vmstat, which can display either average data or actual samples, can help you spot memory pig programs and processes before they bring your server to a crawl.

15. Wireshark

Wireshark, formerly known as Ethereal (and still often referred to that way), is tcpdump's big brother, though it is more sophisticated and with far more advanced protocol analyzing and reporting. Wireshark has both a GUI interface and a shell interface. 

If you can master these commands, you'll be well on your way to being a good Linux system administrator.

10 Basic Linux Networking and Monitoring Commands You Should Know

10 Basic Linux Networking and Monitoring Commands You Should Know

I have listed down 10 basic Linux networking and monitoring commands which each Linux user should know. These Linux basic networking and monitoring commands like hostname, ping, ifconfig, iwconfig, netstat, nslookup, traceroute, finger, telnet, ethtool are used for viewing the IP address of the Linux server, managing Linux server network adapter configuration, making network connections among Linux servers over telnet and ethernet, Linux server information etc. Lets have a look on the following Linux networking and monitoring commands.

1. hostname

hostname with no options displays the machines host name

hostname –d displays the domain name the machine belongs to
hostname –f displays the fully qualified host and domain name
hostname –i displays the IP address for the current machine

2. ping

ping sends packets of information to the user-defined source. If the packets are received, the destination device sends packets back. ping can be used for two purposes

1. To ensure that a network connection can be established.
2. Timing information as to the speed of the connection.

If you do ping www.yahoo.com it will display its IP address. Use ctrl+C to stop the test. 

3. ifconfig

View network configuration, it displays the current network adapter configuration. It is handy to determine if you are getting transmit (TX) or receive (RX) errors.

4. iwconfig

The iwconfig tool is like ifconfig and ethtool for wireless cards. You can view and set the basic Wi-Fi network details, such as the SSID, channel, and encryption. There's also many advanced settings you can view and change, including receive sensitivity, RTS/CTS, fragmentation, and retries.

5. nslookup

If you know the IP address it will display hostname. To find all the IP addresses for a given domain name, the command nslookup is used. You must have a connection to the internet for this utility to be useful.

e.g. nslookup blogger.com

You can also use nslookup to convert hostname to IP Address and from IP Address from hostname.

6. traceroute

A handy utility to view the number of hops and response time to get to a remote system or web site is traceroute. Again you need an internet connection to make use of this tool.

7. finger

View user information, displays a user’s login name, real name, terminal name and write status. this is pretty old unix command and rarely used now days.

8. telnet

Connects destination host via telnet protocol, if telnet connection establish on any port means connectivity between two hosts is working fine.

telnet hostname port - will telnet hostname with the port specified. Normally it is used to see whether host is alive and network connection is fine or not.

9. ethtool

Ethtool lets you view and change many different settings for ethernet adapters (which does not include Wi-Fi cards). You can manage many different advanced settings, including tx/rx, checksumming, and wake-on-LAN settings. However, here are more basic commands you might be interested in:

Display the driver information for a specific network adapter, great when checking for software compatibility.

ethtool -i

Initiate an adapter-specific action, usually blinking the LED lights on the adapter, to help you identify between multiple adapters or interface names:

ethtool -p

Display network statistics:

ethtool -s

Set the connection speed of the adapter in Mbps:

ethtool speed <10|100|1000>

10. netstat

Most useful and very versatile Linux command for finding connection to and from the host. You can find out all the multicast groups (network) subscribed by this host by issuing "netstat -g"

netstat -nap | grep port will display process id of application which is using that port
netstat -a  or netstat –all will display all connections including TCP  and UDP  
netstat --tcp  or netstat –t will display only TCP  connection
netstat --udp or netstat –u will display only UDP  connection
netstat -g will display all multicast network subscribed by this host.

Tuesday, 11 June 2013

Unix Basic Commands Interview Questions and Answers

Unix Basic Commands Interview Questions and Answers

Following is the list of unix basic commands interview questions and answers. These are very basic unix commands and you must know these commands if you are going to appear in a unix interview. I have listed down 31 unix interview questions with answers which cover basic unix commands like ls, banner, cat, date, tee, who, man, head, pg, cmp, diff, grep, kill, echo, su etc. So lets have a look on following unix interview questions and commands.

1. Construct pipes to execute the following jobs.

1. Output of who should be displayed on the screen with value of total number of users who have logged in displayed at the bottom of the list.

2. Output of ls should be displayed on the screen and from this output the lines containing the word ‘poem’ should be counted and the count should be stored in a file.

3. Contents of file1 and file2 should be displayed on the screen and this output should be appended in a file. From output of ls the lines containing ‘poem’ should be displayed on the screen along with the count.

4. Name of cities should be accepted from the keyboard . This list should be combined with the list present in a file. This combined list should be sorted and the sorted list should be stored in a file ‘newcity’.

5. All files present in a directory dir1 should be deleted any error while deleting should be stored in a file ‘errorlog’.

2. Explain the following commands.

$ ls > file1
$ banner hi-fi > message
$ cat par.3 par.4 par.5 >> report
$ cat file1>file1
$ date ; who
$ date ; who > logfile
$ (date ; who) > logfile

3. What is the significance of the “tee” command?

It reads the standard input and sends it to the standard output while redirecting a copy of what it has read to the file specified by the user.

4. What does the command “ $who | sort –logfile > newfile” do?

The input from a pipe can be combined with the input from a file . The trick is to use the special symbol “-“ (a hyphen) for those commands that recognize the hyphen as std input. In the above command the output from who becomes the std input to sort , meanwhile sort opens the file logfile, the contents of this file is sorted together with the output of who (rep by the hyphen) and the sorted output is redirected to the file newfile.

5. What does the command “$ls | wc –l > file1” do?

ls becomes the input to wc which counts the number of lines it receives as input and instead of displaying this count , the value is stored in file1.

6. Which of the following commands is not a filter man , (b) cat , (c) pg , (d) head

Ans: man
A filter is a program which can receive a flow of data from std input, process (or filter) it and send the result to the std output. 

7. How is the command “$cat file2 “ different from “$cat >file2 and >> redirection operators?

8. Explain the steps that a shell follows while processing a command.

After the command line is terminated by the key, the shell goes ahead with processing the command line in one or more passes. The sequence is well defined and assumes the following order.

Parsing: The shell first breaks up the command line into words, using spaces and the delimiters, unless quoted. All consecutive occurrences of a space or tab are replaced here with a single space.

Variable evaluation: All words preceded by a $ are avaluated as variables, unless quoted or escaped. 

Command substitution: Any command surrounded by backquotes is executed by the shell which then replaces the standard output of the command into the command line.

Wild-card interpretation: The shell finally scans the command line for wild-cards (the characters *, ?, [, ]). Any word containing a wild-card is replaced by a sorted list of filenames that match the pattern. The list of these filenames then forms the arguments to the command.

PATH evaluation: It finally looks for the PATH variable to determine the sequence of directories it has to search in order to hunt for the command.

9. What difference between cmp and diff commands?

cmp - Compares two files byte by byte and displays the first mismatch
diff - tells the changes to be made to make the files identical

11. What is the use of ‘grep’ command?

‘grep’ is a pattern search command. It searches for the pattern, specified in the command line with appropriate option, in a file(s).
Syntax : grep
Example : grep 99mx mcafile

12. What is the difference between cat and more command?

Cat displays file contents. If the file is large the contents scroll off the screen before we view it. So command 'more' is like a pager which displays the contents page by page.

13. Write a command to kill the last background job?

Kill $!

14. Which command is used to delete all files in the current directory and all its sub-directories?

rm -r *

15. Write a command to display a file’s contents in various formats?

$od -cbd file_name
c - character, b - binary (octal), d-decimal, od=Octal Dump.

16. What will the following command do?

$ echo *
It is similar to 'ls' command and displays all the files in the current directory.

17. Is it possible to create new a file system in UNIX?

Yes, ‘mkfs’ is used to create a new file system.

18. Is it possible to restrict incoming message?

Yes, using the ‘mesg’ command.

19. What is the use of the command "ls -x chapter[1-5]"

ls stands for list; so it displays the list of the files that starts with 'chapter' with suffix '1' to '5', chapter1, chapter2, and so on.

20. Is ‘du’ a command? If so, what is its use?

Yes, it stands for ‘disk usage’. With the help of this command you can find the disk capacity and free space of the disk.

21. Is it possible to count number char, line in a file; if so, How?

Yes, wc-stands for word count.
wc -c for counting number of characters in a file.
wc -l for counting lines in a file.

22. Name the data structure used to maintain file identification?

‘inode’, each file has a separate inode and a unique inode number.

23. How many prompts are available in a UNIX system?

Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).

24. How does the kernel differentiate device files and ordinary files?

Kernel checks 'type' field in the file's inode structure.

25. How to switch to a super user status to gain privileges?

Use ‘su’ command. The system asks for password and when valid entry is made the user gains super user (admin) privileges.

26. What are shell variables?

Shell variables are special variables, a name-value pair created and maintained by the shell.
Example: PATH, HOME, MAIL and TERM

27. What is redirection?

Directing the flow of data to the file or from the file for input or output.
Example : ls > wc

28. How to terminate a process which is running and the specialty on command kill 0?

With the help of kill command we can terminate the process.
Syntax: kill pid
Kill 0 - kills all processes in your system except the login shell.

29. What is a pipe and give an example?

A pipe is two or more commands separated by pipe char '|'. That tells the shell to arrange for the output of the preceding command to be passed as input to the following command.

Example : ls -l | pr

The output for a command ls is the standard input of pr.
When a sequence of commands are combined using pipe, then it is called pipeline.

30. Explain kill() and its possible return values.

There are four possible results from this call:

‘kill()’ returns 0. This implies that a process exists with the given PID, and the system would allow you to send signals to it. It is system-dependent whether the process could be a zombie.

‘kill()’ returns -1, ‘errno == ESRCH’ either no process exists with the given PID, or security enhancements are causing the system to deny its existence. (On some systems, the process could be a zombie.)

‘kill()’ returns -1, ‘errno == EPERM’ the system would not allow you to kill the specified process. This means that either the process exists (again, it could be a zombie) or draconian security enhancements are present

(e.g. your process is not allowed to send signals to *anybody*).

‘kill()’ returns -1, with some other value of ‘errno’ you are in trouble! The most-used technique is to assume that success or failure with ‘EPERM’ implies that the process exists, and any other error implies that it
doesn't.

An alternative exists, if you are writing specifically for a system (or all those systems) that provide a ‘/proc’ filesystem: checking for the existence of ‘/proc/PID’ may work.

31. What is relative path and absolute path.

Absolute path : Exact path from root directory.
Relative path : Relative to the current path.

Friday, 24 May 2013

10 Interesting Unix Commands You Should Know


10 Interesting Unix Commands You Should Know

Following is the collection of some of the interesting unix commands which you should know. These unix commands are based on network connections, SVN, find, list, history, lsof, sudo, until, git and cat. If you know some extra unix commands like following, please share them. Here goes the list of unix commands which I thought I must share.

1. How to get your top 100 unix commands?

history | sed "s/^[0-9 ]*//" | sed "s/ *| */\n/g" | awk '{print $1}' | sort | uniq -c | sort -rn | head -n 100 > commands.txt

2. How to copy a file to all subfolders of a directory using unix command?

find . -type d -exec cp pathtofile {}/ \;

3. How to delete all .svn folders from current directory using unix command?

find . -type d -name '.svn' -print -exec rm -rf {} \;

4. How to list all network connections (including which app they belong to)?

lsof -i -nP

5. How to run the last command as root?

sudo !!

6. How to execute the previous command until it is successful?

until !!; do :; done

7. How to display summary of git commit ids and messages for a given branch?

git log --pretty='format:%Cgreen%H %Cred%ai %Creset- %s'

8. How to list all file extensions present in the current directory?

ls | perl -lne '++$x{lc $1} if /[.](.+)$/ }{ print for keys %x'

9. How to get your top 10 commands with number of times you have used it?

cat .bash_history | cut -f 1 -d\ | sort | uniq -c | sort -r | head

10. How to generate list of usernames from an svn logs (Run from top level of the svn project)?

svn log | grep -E "r[0-9]+ \| .+ \|" | awk -F"|" '{print $2}' | sort | uniq > ~/authors.txt

Friday, 28 December 2012

35 Tricky and Complex Unix Interview Questions and Commands (Part 1)

35 Tricky and Complex Unix Interview Questions and Commands (Part 1)

Here is the list of 35 complex and tricky unix interview questions and answers. A lot of complex unix commands which are asked in unix interviews are SED, AWK, DU, HEAD, TAIL, WATCH, GREP, CUT, PS, ZIP, UNZIP etc. A lot of tips and tricks are asked about these unix commands during interview. Following questions and unix commands might help you in your unix interview.

1. How do you find which processes are using a particular file?

By using lsof command in UNIX. It will list down PID of all the processes which are using a particular file.

2. How do you find which remote hosts are connecting to your host on a particular port say 10123?

By using netstat command

For example: execute netstat -a | grep "port" and it will list the entire hosts which are connected to this host on port 10123.
 
3. How to tell if my process is running in Unix?

You can list down all the running processes using [ps] command. Then you can “grep” your user name or process name to see if the process is running.

4. What is ephemeral port in UNIX?

Ephemeral ports are port used by Operating system for client sockets. There is a specific range on which OS can open any port specified by ephemeral port range.

5. How to list down file/folder lists alphabetically?

Normally [ls –lt] command lists down file/folder list sorted by modified time. If you want to list then alphabetically, then you should simply specify: [ls –l]

6. If one process is inserting data into your MySQL database? How will you check how many rows inserted into every second?

By using "watch" command in UNIX

7. There is a file Unix_Test.txt which contains words "Unix". How will you replace all Unix to UNIX?

By using SED command in UNIX

For example: you can execute sed s/Unix/UNIX/g fileName.
 
8. You have a tab separated file which contains Name, Address and Phone Number. List down all Phone Number without their name and addresses?

By using either AWK or CUT command.

9. How to check if the last command was successful in Unix?

To check the status of last executed command in UNIX, you can check the value of an inbuilt bash variable [$?]. See the below example:

$> echo $?

10. How to check all the running processes in Unix?

The standard command to see this is [ps]. But [ps] only shows you the snapshot of the processes at that instance. If you need to monitor the processes for a certain period of time and need to refresh the results in each interval, consider using the [top] command.

$> ps –ef

If you wish to see the % of memory usage and CPU usage, then consider the below switches:

$> ps aux

If you wish to use this command inside some shell script, or if you want to customize the output of [ps] command, you may use “-o” switch like below. By using “-o” switch, you can specify the columns that you want [ps] to print out.

$>ps -e -o stime,user,pid,args,%mem,%cpu
 
11 Your application home directory is full? How will you find which directory is taking how much space?

By using disk usage (DU) command in Unix

For example du –sh . | grep G  will list down all the directories which have GIGS in Size.
 
12. How do you find for how many days your Server is up?

By using uptime command in UNIX

13. How to check if a file is present in a particular directory in Unix?

Using command, we can do it in many ways. Based on what we have learnt so far, we can make use of [ls] and [$?] command to do this. See below:

$> ls –l file.txt; echo $?

If the file exists, the [ls] command will be successful. Hence [echo $?] will print 0. If the file does not exist, then [ls] command will fail and hence [echo $?] will print 1.
 
14. You have an IP address in your network. How will you find hostname and vice versa?

By using nslookup command in UNIX

15. How to execute a database stored procedure from Shell script?

$> SqlReturnMsg=`sqlplus -s username/password@database<<EOF
BEGIN
Proc_Your_Procedure(… your-input-parameters …);
END;
/
EXIT;
EOF`
$> echo $SqlReturnMsg

16. How to check the command line arguments in a UNIX command in Shell Script?

In a bash shell, you can access the command line arguments using $0, $1, $2, … variables, where $0 prints the command name, $1 prints the first input parameter of the command, $2 the second input parameter of the command and so on.

17. How to fail a shell script programmatically?

Just put an [exit] command in the shell script with return value other than 0. This is because the exit code of successful Unix program is zero. So, suppose if you write exit -1 inside your program, then your program will throw an error and exit immediately.

18. How to print/display the first line of a file?

There are many ways to do this. However the easiest way to display the first line of a file is using the [head] command.

$> head -1 file.txt

If you specify [head -2] then it would print first 2 records of the file.
 
Another way can be by using [sed] command. [Sed] is a very powerful text editor which can be used for various text manipulation purposes like this.

$> sed '2,$ d' file.txt

How does the above command work? The 'd' parameter basically tells [sed] to delete all the records from display from line 2 to last line of the file (last line is represented by $ symbol). Of course it does not actually delete those lines from the file, it just does not display those lines in standard output screen. So you only see the remaining line which is the 1st line.]

19. How to print/display the last line of a file?

The easiest way is to use the [tail] command.

$> tail -1 file.txt

If you want to do it using [sed] command, here is what you should write:
 
$> sed -n '$ p' test

From our previous answer, we already know that '$' stands for the last line of the file. So '$ p' basically prints (p for print) the last line in standard output screen. '-n' switch takes [sed] to silent mode so that [sed] does not print anything else in the output.
 
20. How to display n-th line of a file?

The easiest way to do it will be by using [sed]. Based on what we already know about [sed] from our previous examples, we can quickly deduce this command:

$> sed –n '<n> p' file.txt

You need to replace <n> with the actual line number. So if you want to print the 4th line, the command will be
 
$> sed –n '4 p' test

Of course you can do it by using [head] and [tail] command as well like below:
 
$> head -<n> file.txt | tail -1

You need to replace <n> with the actual line number. So if you want to print the 4th line, the command will be
 
$> head -4 file.txt | tail -1

21. How to remove the first line / header from a file?

We already know how [sed] can be used to delete a certain line from the output – by using the'd' switch. So if we want to delete the first line the command should be:

$> sed '1 d' file.txt

But the issue with the above command is, it just prints out all the lines except the first line of the file on the standard output. It does not really change the file in-place. So if you want to delete the first line from the file itself, you have two options.
 
Either you can redirect the output of the file to some other file and then rename it back to original file like below:

$> sed '1 d' file.txt > new_file.txt
$> mv new_file.txt file.txt

Or, you can use an inbuilt [sed] switch '–i' which changes the file in-place. See below:
 
$> sed –i '1 d' file.txt

22. How to remove the last line/ trailer from a file in Unix script?

Always remember that [sed] switch '$' refers to the last line. So using this knowledge we can deduce the below command:

$> sed –i '$ d' file.txt

23. How to remove certain lines from a file in Unix?

If you want to remove line <m> to line <n> from a given file, you can accomplish the task in the similar method shown above. Here is an example:

$> sed –i '5,7 d' file.txt

The above command will delete line 5 to line 7 from the file file.txt
 
24. How to remove the last n-th line from a file?

This is bit tricky. Suppose your file contains 100 lines and you want to remove the last 5 lines. Now if you know how many lines are there in the file, then you can simply use the above shown method and can remove all the lines from 96 to 100 like below:

$> sed –i '96,100 d' file.txt   # alternative to command [head -95 file.txt]

But not always you will know the number of lines present in the file (the file may be generated dynamically, etc.) In that case there are many different ways to solve the problem. There are some ways which are quite complex and fancy. But let's first do it in a way that we can understand easily and remember easily. Here is how it goes:

$> tt=`wc -l file.txt | cut -f1 -d' '`;sed –i "`expr $tt - 4`,$tt d" test

As you can see there are two commands. The first one (before the semi-colon) calculates the total number of lines present in the file and stores it in a variable called “tt”. The second command (after the semi-colon), uses the variable and works in the exact way as shown in the previous example.

25. How to check the length of any line in a file?

We already know how to print one line from a file which is this:

$> sed –n '<n> p' file.txt

Where <n> is to be replaced by the actual line number that you want to print. Now once you know it, it is easy to print out the length of this line by using [wc] command with '-c' switch.

$> sed –n '35 p' file.txt | wc –c

The above command will print the length of 35th line in the file.txt.
 
26. How to get the nth word of a line in Unix?

Assuming the words in the line are separated by space, we can use the [cut] command. [cut] is a very powerful and useful command and it's real easy. All you have to do to get the n-th word from the line is issue the following command:

cut –f<n> -d' '

'-d' switch tells [cut] about what is the delimiter (or separator) in the file, which is space ' ' in this case. If the separator was comma, we could have written -d',' then. So, suppose I want find the 4th word from the below string: “A quick brown fox jumped over the lazy cat”, we will do something like this:

$> echo “A quick brown fox jumped over the lazy cat” | cut –f4 –d' '

And it will print “fox”
 
27. How to reverse a string in unix?

Pretty easy. Use the [rev] command.

$> echo "unix" | rev

xinu
 
28. How to get the last word from a line in Unix file?

We will make use of two commands that we learnt above to solve this. The commands are [rev] and [cut]. Here we go.

Let's imagine the line is: “C for Cat”. We need “Cat”. First we reverse the line. We get “taC rof C”. Then we cut the first word, we get 'taC'. And then we reverse it again.

$>echo "C for Cat" | rev | cut -f1 -d' ' | rev

Cat
 
29. How to get the n-th field from a Unix command output?

We know we can do it by [cut]. Like below command extracts the first field from the output of [wc –c] command

$>wc -c file.txt | cut -d' ' -f1

But I want to introduce one more command to do this here. That is by using [awk] command. [awk] is a very powerful command for text pattern scanning and processing. Here we will see how may we use of [awk] to extract the first field (or first column) from the output of another command. Like above suppose I want to print the first column of the [wc –c] output. Here is how it goes like this:

$>wc -c file.txt | awk ' ''{print $1}'

The basic syntax of [awk] is like this:

awk 'pattern space''{action space}'

The pattern space can be left blank or omitted, like below:

$>wc -c file.txt | awk '{print $1}'
 
In the action space, we have asked [awk] to take the action of printing the first column ($1).

30. How to replace the n-th line in a file with a new line in Unix?

This can be done in two steps. The first step is to remove the n-th line. And the second step is to insert a new line in n-th line position. Here we go.
 
Step 1: remove the n-th line

$>sed -i'' '10 d' file.txt       # d stands for delete

Step 2: insert a new line at n-th line position
 
$>sed -i'' '10 i This is the new line' file.txt     # i stands for insert

31. How to show the non-printable characters in a file?

Open the file in VI editor. Go to VI command mode by pressing [Escape] and then [:]. Then type [set list]. This will show you all the non-printable characters, e.g. Ctrl-M characters (^M) etc., in the file.

32. How to zip a file in Linux?

Use inbuilt [zip] command in Linux

33. How to unzip a file in Linux?

Use inbuilt [unzip] command in Linux.

$> unzip –j file.zip

34. How to test if a zip file is corrupted in Linux?

Use “-t” switch with the inbuilt [unzip] command

$> unzip –t file.zip

35. How to check if a file is zipped in Unix?

In order to know the file type of a particular file use the [file] command like below:
$> file file.txt
file.txt: ASCII text
If you want to know the technical MIME type of the file, use “-i” switch.
$>file -i file.txt
file.txt: text/plain; charset=us-ascii
If the file is zipped, following will be the result
$> file –i file.zip
file.zip: application/x-zip

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.