Beginners UNIX Interview Questions Answers
1. Write command to list all the links from a directory?
In this UNIX command interview questions interviewer is generally checking whether user knows basic use of "ls" "grep" and regular expression etc
You can write command like:
ls -lrt | grep "^l"
2. Create a read-only file in your home directory?
This is a simple UNIX command interview questions where you need to create a file and change its parameter to read-only by using chmod command you can also change your umask to create read only file.
touch file
chmod 400 file
3. How will you find which operating system your system is running on in UNIX?
By using command "uname -a" in UNIX
4. How will you run a process in background? How will you bring that into foreground and how will you kill that process?
For running a process in background use "&" in command line. For bringing it back in foreground use command "fg jobid" and for getting job id you use command "jobs", for killing that process find PID and use kill -9 PID command. This is indeed a good Unix Command interview questions because many of programmer not familiar with background process in UNIX.
5. How do you know if a remote host is alive or not?
You can check these by using either ping or telnet command in UNIX. This question is most asked in various Unix command Interview because its most basic networking test anybody wants to do it.
6. How do you see command line history in UNIX?
Very useful indeed, use history command along with grep to find any relevant command you have already executed. Purpose of this Unix Command Interview Questions is probably to check how familiar candidate is from available tools in UNIX operation system.
7. How do you copy file from one host to other?
Many options but you can say by using "scp" command. You can also use rsync command to answer this UNIX interview question or even sftp would be ok.
8. How do you find which process is taking how much CPU?
By using "top" command in UNIX, there could be multiple follow-up UNIX command interview questions based upon response of this because “TOP” command has various interactive options to sort result based upon various parameter.
9. How do you check how much space left in current drive ?
By using "df" command in UNIX. For example "df -h ." will list how full your current drive is. This is part of anyone day to day activity so I think this Unix Interview question will be to check anyone who claims to working in UNIX but not really working on it.
10. What is the difference between Swapping and Paging?
Swapping:
Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.
Paging:
Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory. It provides greater flexibility in mapping the virtual address space into the physical memory of the machine. Allows more number of processes to fit in the main memory simultaneously. Allows the greater process size than the available physical memory. Demand paging systems handle the memory more flexibly.
Intermediate UNIX Interview Questions Answers
1. What is difference between ps -ef and ps -auxwww?
This is indeed a good Unix Interview Command Question and I have faced this issue while ago where one culprit process was not visible by execute ps –ef command and we are wondering which process is holding the file.
ps -ef will omit process with very long command line while ps -auxwww will list those process as well.
2. How do you find how many cpu are in your system and there details?
By looking into file /etc/cpuinfo for example you can use below command:
cat /proc/cpuinfo
3. What is difference between HardLink and SoftLink in UNIX?
I have discussed this Unix Command Interview questions in my blog post difference between Soft link and Hard link in Unix
4. What is Zombie process in UNIX? How do you find Zombie process in UNIX?
When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in case the parent might need it - for example, the parent may need to check the child's exit status. To be able to get this information, the parent calls 'wait()'; In the interval between the child terminating and the parent calling 'wait()', the child is said to be a 'zombie' (If you do 'ps', the child will have a 'Z' in its status field to indicate this.)
Zombie : The process is dead but have not been removed from the process table.
5. What is "chmod" command? What do you understand by this line “r-- -w- --x?
6. There is a file some where in your system which contains word "UnixCommandInterviewQuestions” How will find that file in Unix?
By using find command in UNIX for details see here 10 example of using find command in Unix
7. In a file word UNIX is appearing many times? How will you count number?
grep -c "Unix" filename
8. How do you set environment variable which will be accessible form sub shell?
By using export for example export count=1 will be available on all sub shell.
9. How do you check if a particular process is listening on a particular port on remote host?
By using telnet command for example “telnet hostname port”, if it able to successfully connect then some process is listening on that port. To read more about telnet read networking command in UNIX
10. How do you find whether your system is 32 bit or 64 bit ?
Either by using "uname -a" command or by using "arch" command.
Advanced UNIX Interview Questions and Answers
1. How do you find which processes are using a particular file?
By using lsof command in UNIX. It wills list down PID of all the process which is 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 execute netstat -a | grep "port" and it will list the entire host which is connected to this host on port 10123.
3. What is nohup in UNIX?
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. If one process is inserting data into your MySQL database? How will you check how many rows inserted into every second?
Purpose of this Unix Command Interview is asking about "watch" command in UNIX which is repeatedly execute command provided with specified delay.
6. There is a file Unix_Test.txt which contains words Unix, how will you replace all Unix to UNIX?
You can answer this Unix Command Interview question by using SED command in UNIX for example you can execute sed s/Unix/UNIX/g fileName.
7. You have a tab separated file which contains Name, Address and Phone Number, list down all Phone Number without there name and Addresses?
To answer this Unix Command Interview question you can either you AWK or CUT command here. CUT use tab as default separator so you can use
cut -f3 filename.
8. 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 directory which has GIGS in Size.
9. How do you find for how many days your Server is up?
By using uptime command in UNIX
10. You have an IP address in your network how will you find hostname and vice versa?
This is a standard UNIX command interview question asked by everybody and I guess everybody knows its answer as well.
By using nslookup command in UNIX, you can read more about networking command in UNIX here.
I hope this UNIX command interview questions and answers would be useful for quick glance before going for any UNIX or Java job interview. Please share any interesting UNIX command interview you have come across and I will add into this list. If you are going for any Unix interview on brokerage firm or stock trading company or any Investment bank you can have a quick look here, though most of questions you might already know but its good to review it.
10 tips on find command in UNIX
Here I am listing down some of the way I use find command regularly, I hope this would help some one who is new in UNIX find command or any developer who has started working on UNIX environment. this list is by no means complete and just some of my favorites , if you have something to share please share via commenting.
1) Running the last executed find command in Unix:
!find
This will repeat the last find command executed. It saves lot of time if you re searching for something and you need to execute same command again and again. In fact "!" can be used with any command to invoke previous run of that command.
2) Finding files which has been modified less than one day in Unix:
find . -mtime -1
This is my favorite find command tips while looking out some issue just to check which files have been modified recently which could be likely cause of issue, believe me it helps a lot and many a times gives you enough hint of any problem due to intended or unintended file change.
3) List all the files and directories in the box which holds the 777 permission in Unix?
find . -perm 777 –print
Here I am listing down some of the way I use find command regularly, I hope this would help some one who is new in UNIX find command or any developer who has started working on UNIX environment. this list is by no means complete and just some of my favorites , if you have something to share please share via commenting.
1) Running the last executed find command in Unix:
!find
This will repeat the last find command executed. It saves lot of time if you re searching for something and you need to execute same command again and again. In fact "!" can be used with any command to invoke previous run of that command.
2) Finding files which has been modified less than one day in Unix:
find . -mtime -1
This is my favorite find command tips while looking out some issue just to check which files have been modified recently which could be likely cause of issue, believe me it helps a lot and many a times gives you enough hint of any problem due to intended or unintended file change.
3) List all the files and directories in the box which holds the 777 permission in Unix?
find . -perm 777 –print
I use this find command example to find out all the executable files , you can also modify it to find all the read only files or files having write permission etc by changing permissions e.g. to find all read only files in current directory : find . –perm 555
Here "." or period denotes current directory. You can replace it with any directory you want.
4) How to do case insensitive search using find command in Unix? Use option “-i" with name, by default find searches are case sensitive.
find . –iname "error" –print
Here "." or period denotes current directory. You can replace it with any directory you want.
4) How to do case insensitive search using find command in Unix? Use option “-i" with name, by default find searches are case sensitive.
find . –iname "error" –print
UNIX find command and xargs
Now we will see some unix find command example combined with xargs command, xargs can be used to do whatever witch each file found by find command for example we can delete that file, list content of that file or can apply any comment on that file.
5) How to delete temporary files using find command in Unix?
find . -name "*.tmp" -print | xargs rm –f
Use of xargs along with find gives you immense power to do whatever you want with each search result. See another example below , also its worth considering use of -print0 to avoid problems with white space in the path when piping to xargs (use with the xargs -0 option) as suggested by Ebon Elaza.
6) How to find all text file which contains word Exception using find command in Unix ?
find . –name "*.txt" –print | xargs grep “Exception”
find . –name "*.java" –print | xargs grep “MemoryCache”, this will search all java files starting from current directory for word "MemoryCache". we can also leave -print option in all cases because its default for UNIX find command as pointed out by Ben in comments.
7) Finding files only in current directory not searching on sub directories:
While using find command I realized that some time I only need to find files and directories that are new , only in the current directory so I modified the find command as follows.
find . -maxdepth 1 -type f -newer first_file
Another way of doing it is below:
find . -type f -cmin 15 -prune
Means type file, last modified 15 minutes ago, only look at the current directory. (No sub-directories)
8) Find all files in current directory and subdirectory, greater than some size using find command in Unix:
find . -size +1000c -exec ls -l {} \;
Always use a c after the number, and specify the size in bytes, otherwise you will get confuse because find -size list files based on size of disk block. to find files using a range of file sizes, a minus or plus sign can be specified before the number. The minus sign means "less than," and the plus sign means "greater than." Suppose if you want to find all the files within a range you can use find command as below
find . -size +10000c -size -50000c -print
This find command example lists all files that are greater than 10,000 bytes, but less than 50,000 bytes:
9) Find files which are some days old and greater than some size in Unix. Very common scenario where you want to delete some large old files to free some space in your machine. You can use combination of "-mtime" and "-size" to achieve this.
find . -mtime +10 -size +50000c -exec ls -l {} \;
This command will find which are more than 10 days old and size greater than 50K.
10) You can use "awk" in combination of find to print a formatted output e.g. next command will find all of the symbolic links in your home directory, and print the files your symbolic links points to:
find . -type l -print | xargs ls -ld | awk '{print $10}'
"." says starts from current directory and include all sub directory
"-type l" says list all links
Hope you find this useful , please share how you are using find commands and we can benefit from each others experience and work more efficiently in UNIX.
5) How to delete temporary files using find command in Unix?
find . -name "*.tmp" -print | xargs rm –f
Use of xargs along with find gives you immense power to do whatever you want with each search result. See another example below , also its worth considering use of -print0 to avoid problems with white space in the path when piping to xargs (use with the xargs -0 option) as suggested by Ebon Elaza.
6) How to find all text file which contains word Exception using find command in Unix ?
find . –name "*.txt" –print | xargs grep “Exception”
find . –name "*.java" –print | xargs grep “MemoryCache”, this will search all java files starting from current directory for word "MemoryCache". we can also leave -print option in all cases because its default for UNIX find command as pointed out by Ben in comments.
7) Finding files only in current directory not searching on sub directories:
While using find command I realized that some time I only need to find files and directories that are new , only in the current directory so I modified the find command as follows.
find . -maxdepth 1 -type f -newer first_file
Another way of doing it is below:
find . -type f -cmin 15 -prune
Means type file, last modified 15 minutes ago, only look at the current directory. (No sub-directories)
8) Find all files in current directory and subdirectory, greater than some size using find command in Unix:
find . -size +1000c -exec ls -l {} \;
Always use a c after the number, and specify the size in bytes, otherwise you will get confuse because find -size list files based on size of disk block. to find files using a range of file sizes, a minus or plus sign can be specified before the number. The minus sign means "less than," and the plus sign means "greater than." Suppose if you want to find all the files within a range you can use find command as below
find . -size +10000c -size -50000c -print
This find command example lists all files that are greater than 10,000 bytes, but less than 50,000 bytes:
9) Find files which are some days old and greater than some size in Unix. Very common scenario where you want to delete some large old files to free some space in your machine. You can use combination of "-mtime" and "-size" to achieve this.
find . -mtime +10 -size +50000c -exec ls -l {} \;
This command will find which are more than 10 days old and size greater than 50K.
10) You can use "awk" in combination of find to print a formatted output e.g. next command will find all of the symbolic links in your home directory, and print the files your symbolic links points to:
find . -type l -print | xargs ls -ld | awk '{print $10}'
"." says starts from current directory and include all sub directory
"-type l" says list all links
Hope you find this useful , please share how you are using find commands and we can benefit from each others experience and work more efficiently in UNIX.
Tip:
$* : $* is one of the special bash parameter which is used to expands positional parameters from position one.
if you give double quotes and expansion is done within double quotes, it only expands to a single word and corresponding value of each parameter will be separated by the first letter of the IFS environment variable defined in bash. Do let me know how do you find these find examples .
$* : $* is one of the special bash parameter which is used to expands positional parameters from position one.
if you give double quotes and expansion is done within double quotes, it only expands to a single word and corresponding value of each parameter will be separated by the first letter of the IFS environment variable defined in bash. Do let me know how do you find these find examples .
How to use UNIX find command on file names with space:
I have received lot of comments from my readers on not mentioning about find -print0 and xargs -0 on find examples, so I thought to include this as well. When we don't specify any expression after find command the default option is -print which prints the name of each found files followed by \n or newline.since we mostly pipe output of find command to xargs -print could cause problem if filename itself contain new line or any form of white space. To resolve this issue instead of -print use -print0. Difference between find -print and find -print0 is, print0 display file name on the stdout followed by a "NUL" character and then you can use xargs -0 command to process file names with null character. let's see UNIX find command example with file name having space in them:
stock_trading@electronic_trading_system:~/test find . -name "*equity*" -print
./cash equity trading ./equity~
./cash equity trading ./equity~
You see here "cash equity trading" has space in there name
stock_trading@electronic_trading_system:~/test find . -name "*equity*" -print | xargs ls -l
ls: cannot access ./cash: No such file or directory
ls: cannot access equity: No such file or directory
ls: cannot access trading: No such file or directory
-r--r--r-- 1 stock_trading cash_domain trading 0 Jul 15 11:42 ./equity~
Now if we pass this to xargs, xargs treat them as three separate files.
stock_trading@electronic_trading_system:~/test find . -name "*equity*" -print0 | xargs ls
xargs: WARNING: a NUL character occurred in the input. It cannot be passed through in the argument list. Did you mean to use the --null option?
ls: cannot access ./cash: No such file or directory
ls: cannot access equity: No such file or directory
ls: cannot access trading: No such file or directory
Now to solve this we have used find command with -print0 which appends NUL character on file name but without xargs -0, xargs will not able to handle those inputs.
stock_trading@electronic_trading_system:~/test find . -name "*equity*" -print0 | xargs -0 ls -l
-rw-r--r-- 1 stock_trading cash_domain trading 0 Jul 21 09:54 ./cash equity trading
-r--r--r-- 1 stock_trading cash_domain trading 0 Jul 15 11:42 ./equity~
Now you can see with find -print0| xargs -0 it looks good
In conclusion always use find -print0 along with xargs -0 if you see slightest possibilities of file names containing space in UNIX or Linux.
"grep" one of the most frequently used UNIX command stands for "Global Regular Expression Print". This grep command tutorial is not about theory of UNIX grep but to practical use of grep in UNIX and here I am sharing my experience on use of grep command in Linux with an aim that this would serve as quick guide or tutorial for using grep in UNIX for new beginners and help them to understand the grep command better and its thoughtful usage in UNIX or Linux. Many people use grep just for finding words in a file and missed the real potential of grep by not using all its powerful command line options and its regular expression capability which could not only save a lot of time but also works as a great and powerful tool while analyzing large set of data or log files. Also find command in UNIX can be used in place of grep at many places.
10 examples of grep command in UNIX and Linux
Following examples on grep command in UNIX are based on my experience and I use them on daily basis in my work. These examples are by no means complete so please contribute your grep command tips or how you are using grep in Linux to make it more useful and allow all of us to benefit from each others experience and work efficiently in UNIX or Linux.
So here we go
1) Finding relevant word and exclusion irrelevant word. Most of the time I look for Exception and Errors in log files and some time I know certain Exception I can ignore so I use grep -v option to exclude those Exceptions
grep Exception logfile.txt | grep -v ERROR
2) If you want to count of a particular word in log file you can use grep -c option to count the word. Below command will print how many times word "Error" has appeared in logfile.txt
grep -c "Error" logfile.txt
3) Sometime we are not just interested on matching line but also on lines around matching lines particularly useful to see what happens before any Error or Exception. grep --context option allows us to print lines around matching pattern. Below example of grep command in UNIX will print 6 lines around matching line of word "successful" in logfile.txt
grep --context=6 successful logfile.txt
Show additional six lines after matching very useful to see what is around and to print whole message if it splits around multiple lines. You can also use command line option "C" instead of "--context" for example
grep -C 2 'hello' *
Prints two lines of context around each matching line.
4) egrep stands for extended grep and it is more powerful than grep command in Unix and allows more regular exception like you can use "|" option to search for either Error or Exception by executing just one command.
egrep 'Error|Exception' logfile.txt
5) If you want to do case insensitive search than use -i option from grep command in UNIX. Grep -i will find occurrence of both Error, error and ERROR and quite useful to display any sort of Error from log file.
grep -i Error logfile
6) zgrep is another great version of grep command in Unix which is used to perform same operation as grep does but with .gz files. Many a times we gzip the old file to reduce size and later wants to look or find something on those file. zgrep is your man for those days. Below command will print all files which have "Error" on them.
zgrep -i Error *.gz
7) Use grep -w command in UNIX if you find whole word instead of just pattern.
grep -w ERROR logfile
Above grep command in UNIX searches only for instances of 'ERROR' that are entire words; it does not match `SysERROR'.
For more control, use `\<' and `\>' to match the start and end of words. For example:
grep 'ERROR>' *
Searches only for words ending in 'ERROR', so it matches the word `SysERROR'.
8) Another useful grep command line option is "grep -l" which display only the file names which matches the given pattern. Below command will only display file names which have ERROR?
grep -l ERROR *.log
grep -l 'main' *.java will list the names of all Java files in the current directory whose contents mention `main'.
9) If you want to see line number of matching lines you can use option "grep -n" below command will show on which lines Error has appeared.
grep -n ERROR log file.
10) If you want to do recursive search using grep command in Unix there are two options either use "-R" command line option or increase directory one by one as shown below.
Now I have two bonus examples of grep command in unix:
11) grep command in UNIX can show matching patter in color which is quite useful to highlight the matching section , to see matching pattern in color use below command.
grep Exception today.log --color
12) There are three version of grep command in UNIX `grep, fgrep, egrep'. `fgrep' stands for Fixed `grep', `egrep' Extended `grep'
These examples of grep command in UNIX are something which I use on daily basis; I have seen more sophisticated use of grep with regular expression. I will list some more examples of grep command in UNIX as I come across and find useful to share. As per my experience having good hold on grep and UNIX find command with knowledge of regular expression will be great for you day to day life if you need to look log files or config files or need to do production support on electronic trading systems or any other kind of system which is running on UNIX. This list of grep command in UNIX is by no means complete and I look forward from you guys to share how you are using grep command in UNIX.
This article is in continuation of my earlier article Top 10 basic networking Commands in Unix and Top 10 most useful CVS command in Unix to read more see here 10 tips of using find command in linux.
These are most useful commands in my list while working on Linux server , this enables you to quickly troubleshoot connection issues e.g. whether other system is connected or not , whether other host is responding or not and while working for FIX connectivity for advanced trading system this tools saves quite a lot of time .
• finding host/domain name and IP address - hostname
• test network connection – ping
• getting network configuration – ifconfig
• Network connections, routing tables, interface statistics – netstat
• query DNS lookup name – nslookup
• communicate with other hostname – telnet
• outing steps that packets take to get to network host – traceroute
• view user information – finger
• checking status of destination host - telnet
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
ping
It 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.
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.
netstat
Most useful and very versatile 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.
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
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.
finger
View user information, displays a user’s login name, real name, terminal name and write status.
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.
These are most useful commands in my list while working on Linux server , this enables you to quickly troubleshoot connection issues e.g. whether other system is connected or not , whether other host is responding or not and while working for FIX connectivity for advanced trading system this tools saves quite a lot of time .
• finding host/domain name and IP address - hostname
• test network connection – ping
• getting network configuration – ifconfig
• Network connections, routing tables, interface statistics – netstat
• query DNS lookup name – nslookup
• communicate with other hostname – telnet
• outing steps that packets take to get to network host – traceroute
• view user information – finger
• checking status of destination host - telnet
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
ping
It 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.
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.
netstat
Most useful and very versatile 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.
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
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.
finger
View user information, displays a user’s login name, real name, terminal name and write status.
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.
10 tips for working fast in UNIX
Have you ever amazed to see someone working very fast in UNIX, firing commands and doing things in mille seconds? Yes I have seen and I have always inspired to learn from those gems of guys. This article or tutorial or whatever you call it I have dedicated to share UNIX command practices I follow to work fast, quick or efficiently in UNIX. I work for financial services industry and my work involves development and support of online stock and futures trading application in Electronic trading, Derivatives etc. all our services runs of UNIX servers so its very important for me to work efficiently and quickly in Linux machine.
This article is in continuation of my earlier article Top 10 basic networking Commands in Unix and Top 10 most useful CVS command in Unix and 10 examples of using find command in UNIX .
if you have not read those you can see if you find them interesting and useful. In this UNIX command tutorial I am going to share my experience on how to work quick, fast and efficiently in UNIX.
If your server also resides in UNIX machine and your day 2 day work involves lots of searching and playing around UNIX commands. Below tips are of my years of experience in UNIX which I have summarized as 10 tips to work fast in UNIX :) What I am looking forward is to get some more tips from you guys to enhance my arsenal so please share how you work in UNIX, how you make most of powerful commands and shell utilities provided by UNIX?
Please share your experience by posting comments to make this post useful and get most of it and benefit from each others experience.
1) Use ! For executing last command
This has saved my 30% time on average. It always happens that you fire same UNIX command multiple times within a fraction of seconds, before knowing this trick I used to use up and down arrow for finding my command and then executing them which takes some of my time but after
Knowing this trick I just have to remember command name e.g. !ls will execute your last "ls -lrt" , !vim will open your last file without
Typing full commands. Use it and experience it , It definitely save loads of time and its also useful on shell other than bash where up and down arrow generally doesn't give you previous commands.
For example After doing ls –l stocks.txt if you want to open stocks.txt you can use vi !$ (last argument)
2) use !! for executing last command
This is an extension of previous tip which is used to execute the very last command you have executed. Since it just involves two keystrokes and that too for same key it’s amazingly fast. This will also works on the shells in which up and down arrow don’t work.This is extremely useful if you are stopping or starting your trading application for debug purpose frequently
3) Use "CRTL+R" for repeating the last matching command
Best out of lot if you remember your last command executed sometime back and just want to find that command with same argument and execute
This is the tip you need to remember. just press "CRTL+R" and type words that you had in your last command and UNIX will find that command for you then just press enter.
All above three tips will save lot of your time if you execute commands frequently and percentage of repetition is quite high. for me
I have saved almost 50-60% time by following above three tips. let me know how it works for you guys.
4) Using history command for getting some of the most frequently used UNIX command
This is the first thing I learn when I started working on UNIX :) This is your most helpful command in UNIX or shell. In most of the
Cases there are certain command like starting, stopping, checking log files, making build or doing release. Which we need to execute very frequently and if you don't remember exact command no need to worry, just do history | grep "keyword" and you will get that command from
UNIX history. There are certain environment variable e.g. HISTSIZE which defines how many command UNIX history can store, so have it big
Enough to save your time and avoid referencing your command booklet now and then.
5) Using regular expression in grep and find.
Have you ever amazed to see someone working very fast in UNIX, firing commands and doing things in mille seconds? Yes I have seen and I have always inspired to learn from those gems of guys. This article or tutorial or whatever you call it I have dedicated to share UNIX command practices I follow to work fast, quick or efficiently in UNIX. I work for financial services industry and my work involves development and support of online stock and futures trading application in Electronic trading, Derivatives etc. all our services runs of UNIX servers so its very important for me to work efficiently and quickly in Linux machine.
This article is in continuation of my earlier article Top 10 basic networking Commands in Unix and Top 10 most useful CVS command in Unix and 10 examples of using find command in UNIX .
if you have not read those you can see if you find them interesting and useful. In this UNIX command tutorial I am going to share my experience on how to work quick, fast and efficiently in UNIX.
If your server also resides in UNIX machine and your day 2 day work involves lots of searching and playing around UNIX commands. Below tips are of my years of experience in UNIX which I have summarized as 10 tips to work fast in UNIX :) What I am looking forward is to get some more tips from you guys to enhance my arsenal so please share how you work in UNIX, how you make most of powerful commands and shell utilities provided by UNIX?
Please share your experience by posting comments to make this post useful and get most of it and benefit from each others experience.
1) Use ! For executing last command
This has saved my 30% time on average. It always happens that you fire same UNIX command multiple times within a fraction of seconds, before knowing this trick I used to use up and down arrow for finding my command and then executing them which takes some of my time but after
Knowing this trick I just have to remember command name e.g. !ls will execute your last "ls -lrt" , !vim will open your last file without
Typing full commands. Use it and experience it , It definitely save loads of time and its also useful on shell other than bash where up and down arrow generally doesn't give you previous commands.
For example After doing ls –l stocks.txt if you want to open stocks.txt you can use vi !$ (last argument)
2) use !! for executing last command
This is an extension of previous tip which is used to execute the very last command you have executed. Since it just involves two keystrokes and that too for same key it’s amazingly fast. This will also works on the shells in which up and down arrow don’t work.This is extremely useful if you are stopping or starting your trading application for debug purpose frequently
3) Use "CRTL+R" for repeating the last matching command
Best out of lot if you remember your last command executed sometime back and just want to find that command with same argument and execute
This is the tip you need to remember. just press "CRTL+R" and type words that you had in your last command and UNIX will find that command for you then just press enter.
All above three tips will save lot of your time if you execute commands frequently and percentage of repetition is quite high. for me
I have saved almost 50-60% time by following above three tips. let me know how it works for you guys.
4) Using history command for getting some of the most frequently used UNIX command
This is the first thing I learn when I started working on UNIX :) This is your most helpful command in UNIX or shell. In most of the
Cases there are certain command like starting, stopping, checking log files, making build or doing release. Which we need to execute very frequently and if you don't remember exact command no need to worry, just do history | grep "keyword" and you will get that command from
UNIX history. There are certain environment variable e.g. HISTSIZE which defines how many command UNIX history can store, so have it big
Enough to save your time and avoid referencing your command booklet now and then.
5) Using regular expression in grep and find.
grep and find is two best tools UNIX provide to us. almost everybody needs to search something in UNIX e.g. a file , a directory , certain words in file e.g. ERROR or Exception and if you know how to use grep and find with regular expression you will save lot of your time by typing less commands.
For example by knowing about egrep you can fire egrep "ERROR|Exception" *.xml instead of firing two grep command for finding ERROR and Exception individually.
6) Using pipe instead of firing two commands
Just shown above this nice and little tip I guess everybody knows :)
7) Using aliases and defining them in bash profile or bashrc file.
Have you seen some strange commands working in someone's machine and not yours, which might be aliases he would have setup in either his .bashrc or .profile file. Always do such kind of setup for commonly used command. There are lots of usage of .bashrc and .profile file but
One of the most important one is setting up aliases e.g. "l." which finds all hidden files. "ls" which includes all useful option e.g. -lrtH to show all relevant information.
8) Using pushd, popd , cd - , ~ for moving across directory.
Based on my experience navigation in UNIX shell takes almost 50% times of people and if you are going to write directory path every now and then just forget about working fast. so instead of typing full name use all above tips and make best use of pushd, popd, cd - and cd ~ command. cd - is best if your switching between two directory location in UNIX.
9) Minimize the key strokes or increase the speed of typing.
That I guess you know isn't it the less you type the more fast you work so make use of your last typed command, make use of tab in bash so that let the UNIX bash shell complete your command, use Ctrl+R if the last command you have typed is very long and you want to change just few lines.
10) Try to learn more commands and their options and usage this will reduce thinking time for a particular task and use ctrl+z and fg and bg to suspend a process. it saves almost 10% time if you are viewing multiple files or log files so instead of every now and then executing vim commands just do Ctrl+Z to suspend it and fg 1 or fg 2 to bring it on foreground.
I hope these examples, tips on UNIX command will help you to do more in less time and enhance your productivity and experience while working in UNIX.
For example by knowing about egrep you can fire egrep "ERROR|Exception" *.xml instead of firing two grep command for finding ERROR and Exception individually.
6) Using pipe instead of firing two commands
Just shown above this nice and little tip I guess everybody knows :)
7) Using aliases and defining them in bash profile or bashrc file.
Have you seen some strange commands working in someone's machine and not yours, which might be aliases he would have setup in either his .bashrc or .profile file. Always do such kind of setup for commonly used command. There are lots of usage of .bashrc and .profile file but
One of the most important one is setting up aliases e.g. "l." which finds all hidden files. "ls" which includes all useful option e.g. -lrtH to show all relevant information.
8) Using pushd, popd , cd - , ~ for moving across directory.
Based on my experience navigation in UNIX shell takes almost 50% times of people and if you are going to write directory path every now and then just forget about working fast. so instead of typing full name use all above tips and make best use of pushd, popd, cd - and cd ~ command. cd - is best if your switching between two directory location in UNIX.
9) Minimize the key strokes or increase the speed of typing.
That I guess you know isn't it the less you type the more fast you work so make use of your last typed command, make use of tab in bash so that let the UNIX bash shell complete your command, use Ctrl+R if the last command you have typed is very long and you want to change just few lines.
10) Try to learn more commands and their options and usage this will reduce thinking time for a particular task and use ctrl+z and fg and bg to suspend a process. it saves almost 10% time if you are viewing multiple files or log files so instead of every now and then executing vim commands just do Ctrl+Z to suspend it and fg 1 or fg 2 to bring it on foreground.
I hope these examples, tips on UNIX command will help you to do more in less time and enhance your productivity and experience while working in UNIX.
Vim or VI editor tutorial in UNIX
VI Editor is like notepad in UNIX but it’s extremely powerful and have sophisticated feature to work as complete IDE. No matter which version of UNIX you are working or which flavor you are using you always find either VI editor or VIM there. VI is a very large topic and I am not covering every aspect of it what I am sharing here is some examples of using VI editor in UNIX or Linux. most of the time we use only 10% of VI editor and never know 90% of it , to be frank I also don't know many of VI feature by heart but I always search for it , see people working in VI and learn from there experience as well. after working on VI editor what I found that every day we learn something new in VI editor but we forget something equally important and there I thought lets document whatever VI commands I have learned so far and using regularly. Idea is to keep this VI Editor tutorial updated with every single useful VI commands I learn and I also ask you guys to contribute with something useful. If you see 10 peoples working in VI editor you realize that they use it differently then use some new VI commands which you are not familiar of and which could improve your productivity as well. so if you would like to know more and more about VI editor I would suggest observe people around you when they work in VIM or VI editor and learn from there experience. Don’t confuse with both VIM and VI, VIM stands for VI improved and has some more feature than VI editor. Many a times you will find that VI command is aliased to VIM to make it default editor in UNIX.
Long story short let get into VI command examples:
Long story short let get into VI command examples:
Opening file in Read only mode in VI Editor
If you are in production server then you definitely want to open a config file in read only mode to avoid any accidental change. So if you are not using less you can use "-R" option of VI editor:
vim -R config.xml
And if you want to save any change from read only mode you can do by ":wq!".
vim -R config.xml
And if you want to save any change from read only mode you can do by ":wq!".
Navigation commands in Vim or VI
One secret of being efficient in VI editor is to be fluent in navigating between different parts of file. Any time you should be able to go to start of file, end of file, start of line, end of line, and search for a particular word from up and down and then move around. Navigational commands are executed from inside VI editor, here are some of must know examples:
gg -- goes to start of file
shift g -- goes to end of file
0 -- goes to beginning of the line
$ -- goes to end of the line
nG -- goes to nth line
:n -- another way of going to nth line
gg -- goes to start of file
shift g -- goes to end of file
0 -- goes to beginning of the line
$ -- goes to end of the line
nG -- goes to nth line
:n -- another way of going to nth line
Editing in VI Editor in UNIX
What do we do most in VI editor I would say editing and to become efficient and quick this is an area where you need to practice and become fluent.
yy -- equivalent to cut also called yank
p -- paste below line
shift p -- paste above line
dd -- deletes the current line
5dd -- deletes 5 lines
u -- undo last change
Ctrl + R - Re do last change
yy -- equivalent to cut also called yank
p -- paste below line
shift p -- paste above line
dd -- deletes the current line
5dd -- deletes 5 lines
u -- undo last change
Ctrl + R - Re do last change
Searching pattern in VI Editor
Most of you guys familiar with grep command in UNIX and UNIX find command which is used for searching in Unix, here we will see how to search inside VI. If you have open your log file in VI editor and looking for any word you can search for a particular word or ID from either top to bottom or bottom to top, if you are interested in first occurrence then I would suggest to search from top to bottom on the other hand if you are interested on last occurrence then its better to search from bottom to top
/Exception -- will search for word "Exception" from top to bottom and stop when it got first match, to go to next match type "n" and for coming back to previous match press "Shift + N"
?Exception -- will search for word "Exception" from bottom to top and stop when it got first match, to go to next match type "n" and for coming back to previous match press "Shift + N", remember for next match it will go towards top of file.
/Exception -- will search for word "Exception" from top to bottom and stop when it got first match, to go to next match type "n" and for coming back to previous match press "Shift + N"
?Exception -- will search for word "Exception" from bottom to top and stop when it got first match, to go to next match type "n" and for coming back to previous match press "Shift + N", remember for next match it will go towards top of file.
Running Shell command from inside VI Editor
Some times we are editing something and we wanted to execute some shell command to get some information, normally we need to close the VI editor then execute shell command and then again open the VI editor this is not a fast way of doing it, if we want to save time and work faster we need to directly execute shell command from VI and we can do this by using "!command" from command mode of VI editor. For example doing “ls” from VI editor we can type:
:!ls
if you wants to go directly to shell without quitting from VI editor you can go by executing !sh from VI and then come back to VI editor by just executing command "exit" from shell.
:!ls
if you wants to go directly to shell without quitting from VI editor you can go by executing !sh from VI and then come back to VI editor by just executing command "exit" from shell.
Some Useful VI Editor options
VI editor is full of options but we don't really use most of them here I am listing some of the option which I use most frequently and found quite useful.
:set nu -- This will display line number in front of each line quite useful if you want line by line information. You can turn it off by executing "set nonu". Remember for turning it off put "no" in front of option, like here option is "nu" so for turning it off use "nonu".
:set hlsearch -- This will highlight the matching word when we do search in VI editor, quite useful but if you find it annoying or not able to see sometime due to your color scheme you can turn it off by executing set nohlsearch.
:set wrap -- If your file has contains some long lines and you want them to wrap use this option, if its already on and you just don't want them to wrap use set nowrap.
:colorscheme -- color scheme is used to change color of VIM editor, my favorite color scheme is murphy so if you want to change color scheme of VI editor you can do by executing "colorscheme murphy ".
:syntax on -- syntax can be turn on and off based on your need , if its on it will display color syntax for .xml, .html and .perl files.
:set ignorecase : This VI editor option allows you do case insensitive search because if its set VI will not distinguish between two words which are just differ in case.
:set smartcase : Another VI editor option which allows case-sensitive search if the word you are searching contains an uppercase character.
:set nu -- This will display line number in front of each line quite useful if you want line by line information. You can turn it off by executing "set nonu". Remember for turning it off put "no" in front of option, like here option is "nu" so for turning it off use "nonu".
:set hlsearch -- This will highlight the matching word when we do search in VI editor, quite useful but if you find it annoying or not able to see sometime due to your color scheme you can turn it off by executing set nohlsearch.
:set wrap -- If your file has contains some long lines and you want them to wrap use this option, if its already on and you just don't want them to wrap use set nowrap.
:colorscheme -- color scheme is used to change color of VIM editor, my favorite color scheme is murphy so if you want to change color scheme of VI editor you can do by executing "colorscheme murphy ".
:syntax on -- syntax can be turn on and off based on your need , if its on it will display color syntax for .xml, .html and .perl files.
:set ignorecase : This VI editor option allows you do case insensitive search because if its set VI will not distinguish between two words which are just differ in case.
:set smartcase : Another VI editor option which allows case-sensitive search if the word you are searching contains an uppercase character.
Opening multiple files in VI Editor
Sometime we just want to open 2 or 3 files in one go and then wanted to navigate on those. we can do this easily in VI editor by just giving file name separated with space while executing Vim or VI command e.g. vim file1 file2 file3 . For navigating between those file we can use option ":n" for going to next file. You can also open any file at any time from VI editor by executing ":e filename" or reload the same file by just executing ":e" (without file name).
Saving and Quitting from VI Editor in Unix
Once we done with our change in VI editor we either wants to save or quite from the file, here are the commands to do so.
:w -- to save file anytime
:wq -- most used to save data and quit
:q! -- quit without saving
:wq! -- to save change in file opened in read-only mode.
:w -- to save file anytime
:wq -- most used to save data and quit
:q! -- quit without saving
:wq! -- to save change in file opened in read-only mode.
Checking history and getting help in VI editor
if you ask me list one command you always want to remember I would say "history" because by using "history" I can get all commands I have executed in past. Same way by typing "!history" we can get all commands we have executed from VI editor. This is immensely powerful and I use it a lot. Another command which is important to remember is ":help" by typing this you can access the help system provided by VI. If you are interested on any particular command you can type ":help command" and it will display help information for that command.
find and replace in vi editor
don't confuse this with find command in UNIX , VI editor provides easy commands to search and replace, perform global search and local search. I would suggest practice it a few times to get hold of it and understand its working. we use ":s" (substitute) command for search and replace in VI editor.
The “:s” command look for a particular pattern and replaces with provide substitution, here are some examples of using search and replace in VI editor.
In Vi editor search and replace, "g" stands for global search here which means all occurrence of a particular word in a line changed rather than just first word, which is default behavior if you don't use global search
:%s/Stock/Equity/g
This is an example of global search it will replace all occurrence of word "Stock" in file with word "Equity". Its also equivalent to following command ": 0,$ s/Stock/Equity/g" which actually tells that search from fist to last line.
:%s/Stock/Equity/gc
This is similar to first command but with the introduction of "c" it will ask for confirmation
:%s/Stock/Equity/gci
This is command is global, case insensitive and ask for confirmation. to make it case Sensitive use "I"
The “:s” command look for a particular pattern and replaces with provide substitution, here are some examples of using search and replace in VI editor.
In Vi editor search and replace, "g" stands for global search here which means all occurrence of a particular word in a line changed rather than just first word, which is default behavior if you don't use global search
:%s/Stock/Equity/g
This is an example of global search it will replace all occurrence of word "Stock" in file with word "Equity". Its also equivalent to following command ": 0,$ s/Stock/Equity/g" which actually tells that search from fist to last line.
:%s/Stock/Equity/gc
This is similar to first command but with the introduction of "c" it will ask for confirmation
:%s/Stock/Equity/gci
This is command is global, case insensitive and ask for confirmation. to make it case Sensitive use "I"
Recording and replaying command in VI editor
Some times we need to perform some repetitive task and we need to execute same command again and again, you might want to consider using VI editor's recording functionality. Recoding in vim or VI editor can be done by using q and the executing recorded comment by using q@1
This list of examples of VI editor is not complete and just a collection of VI commands from top of my head. Please let me know how you are using VI editor in your day 2 day life and share some useful tips here for benefit of all. I also found vim wiki as most detailed and useful resource for VI editor and vim tips so go and learn few tips.
This list of examples of VI editor is not complete and just a collection of VI commands from top of my head. Please let me know how you are using VI editor in your day 2 day life and share some useful tips here for benefit of all. I also found vim wiki as most detailed and useful resource for VI editor and vim tips so go and learn few tips.
Many times in shell scripts we would have witnessed two different forms of notations being used against variables: $x and ${x}. Are they both same or is there any difference between them? Let us discuss in this article:
Let us assign a value to the variable, x:
$ x=10
$ echo $x
10
Displaying using the second notation:
$ echo ${x}
10
As seen above, it does not look like there is any difference. But it is not so. The actual difference we will see now:
Let us try to join or concatenate the literal "y" along with the value of $x:
Let us try to join or concatenate the literal "y" along with the value of $x:
$ echo $xy
Nothing got printed for "echo $xy". Now, let us the second notation to display:
$ echo ${x}y
10y
What happened? When we said $xy, the shell interpreted it as being asked to print the value of a variable named "xy". Since there is no such variable "xy" being defined, a blank output. However, the second notation ${x}y is being interpreted as : print the value of x and concatenate it with the literal "y", and hence the result "10y".
find in Unix, as we know, is a command which nobody can live without. In this article, we are going to discuss only about finding the files with respect to the modification time, say files modified in the last X mins or Xhours. These can be done with the find command options available. However, in cases where the time is not a full hour, say to find files modified in the last 30hours, some Unix flavors do not have a direct option. Let us see in this article how to get these things done.
The basic syntax of the find command is:
find path options
where path is the path to search
and options are the options given to find command.
and options are the options given to find command.
In all our below examples, the path is our current directory and hence we use .(dot).
1. To find files modified in the last 5 days:
find . -mtime -5
2. To find files modified before 5 days:
find . -mtime +5
Note: Developers, be aware. + is not default in find. If you omit the '+', it has a different meaning. It means to find files modified exactly before 5 days.
3. To find files modified in the last 40mins:
find . -mmin -40
4. To find files modified before 40mins:
find . -mmin +40
The above commands will find both files and directories modifying the criteria. If you want to find only files, use the -type option.
find . -type f -mmin -40
This will find only the files modified in the last 40 mins, not directories.
5. By the way, not in all Unix flavours one will find the -mmin option. If you dont have the mmin option, use the following:
First create a dummy file whose timestamp is the time you are looking for:
touch -d "40 mins ago" temp
The above touch command will create a temp file whose timstamp is 40mins before. For example, if the time now is 10hours 50mins, the temp file timstamp will be 10hours 40mins.
If your Unix flavor does not have the "-d" option in the touch command, you can use the following method to set the timestamp:
touch -t 1008211020 temp
This creates a temp file whose time stamp is 2010,Aug 21, 10hours 20mins. [YYMMDDHHMM]
Second, search files which are modified after this file temp has been modified. The below command will display all the files modified after the temp has been modified OR in other words find files which are newer than temp file:
find . -newer temp
Similarly, to find files which are modified before 40 mins. In other words to negate the above search, use the exclamation:
find . ! -newer temp
In the same way, we can find files modified from any time we need.
6. One of the frequent requirement a sys admin gets is to find files modified before say last 2 days and 10 hours 30mins and move them to a backup directory. It can be achieved by the below commands:
#touch -d "2days 10 hours 30 mins ago" temp
#find / -type f ! -newer temp -exec mv '{}' ~/backup \;
The above example does 2 things:
a. First create a temp file whose timstamp is 2 days 10 hours and 30mins ago.
b. Finds all the files under root which are older than the temp file and moves them to the backup directory.
The same thing using "touch -t" will be like assuming todays date is 21 Aug 2010, 15hours,45mins:
#touch -t 1008190515 temp
#find / -type f ! -newer temp -exec mv '{}' ~/backup \;
1008190715 indicates 2010(10), Aug(08), 19th(19), 5 hours(05) and 15mins
Any file present or created in Unix has 3 different time stamps associated with it . The 3 different timings are:
1. Last modified time - This refers to the timing when the file has been modified last. For a newly created file, this refers to the time when the file is created.
2. Last accessed time - This refers to the timing when the file has been accessed last. Access can be either when the file is read, or the file is written to or even if the file has been executed. The last accessed time gets updated when any of the above file operations is being done on a file.
3. Last changed time - This refers to the timing when the file status has been changed last. The change in file status could be due to any of the following reasons:
i) File ownership being changed.
ii) Group to which the file belongs to is changed.
iii) File permissions such as read, write or execute permissions has been applied or removed from the file.
iv) Advanced file permissions such as SUID, SGID or Sticky bit has been applied or removed on the file.
v) When the file content is updated.
As seen in one of our earlier inode articles, all these 3 time stamps are saved as part of the inode information.
Example:
Let us try to understand the above time stamps with an example by creating a file, say f1:
$ touch f1
Now, let us see the different time stamps:
The timing shown in the ls -l command is the last modified time of a file. In this case, it is the file creation time since the file is new:
The timing shown in the ls -l command is the last modified time of a file. In this case, it is the file creation time since the file is new:
$ ls -l f1
-rw-r--r-- 1 guru None 0 Jun 25 16:40 f1
The last accessed time can be seen by using the command: ls -lu
$ ls -lu f1
-rw-r--r-- 1 guru None 0 Jun 25 16:40 f1
The last changed time can be seen using: ls -lc
$ ls -lc f1
-rw-r--r-- 1 guru None 0 Jun 25 16:40 f1
As seen above, in this case, all the time stamps are equal since the file is new.
1. Let us update the file contents and see how these 3 timings changes:
1. Let us update the file contents and see how these 3 timings changes:
$ date
Sat Jun 25 16:45:45 IST 2011
Writing the "welcome" word to the file:
$ echo "welcome" > f1
The last modified time:
$ ls -l f1
-rw-r--r-- 1 guru None 8 Jun 25 16:45 f1
The last accessed time:
$ ls -lu f1
-rw-r--r-- 1 guru None 8 Jun 25 16:45 f1
The last changed time:
$ ls -lc f1
-rw-r--r-- 1 guru None 8 Jun 25 16:45 f1
As shown above, when the file contents are changed, all the 3 time stamps get updated. The last modification time gets changed since the file is being modified. The last access time is changed since the file is being written. The last changed time is updated since the file status is being changed. Hence, all the 3 time stamps get updated when the file content is being updated.
2. Let us try to read the file f1:
2. Let us try to read the file f1:
$ date
Sat Jun 25 16:50:34 IST 2011
$ cat f1
welcome
The last modified time:
$ ls -l f1
-rw-r--r-- 1 guru None 8 Jun 25 16:48 f1
The last accessed time:
$ ls -lu f1
-rw-r--r-- 1 guru None 8 Jun 25 16:50 f1
The last changed time:
$ ls -lc f1
-rw-r--r-- 1 guru None 8 Jun 25 16:48 f1
As above, when the file content is being read, only the file access time is changed since the file is just being read. The same would have happened even if we would have executed the file.
3. Let us try to change the file permission of the file f1 by giving write permission to the group:
3. Let us try to change the file permission of the file f1 by giving write permission to the group:
$ date
Sat Jun 25 16:53:34 IST 2011
$ chmod g+w f1
The last modified time:
$ ls -l f1
-rw-rw-r-- 1 guru None 8 Jun 25 16:48 f1
The last accessed time:
$ ls -lu f1
-rw-rw-r-- 1 guru None 8 Jun 25 16:50 f1
The last changed time:
$ ls -lc f1
-rw-rw-r-- 1 guru None 8 Jun 25 16:53 f1
As shown, only the last changed time is being updated since only file status(file permission in this case) is being changed.
What are various IDs associated with a process?
Unix identifies each process with a unique integer called ProcessID. The process that executes the request for creation of a process is called the 'parent process' whose PID is 'Parent Process ID'. Every process is associated with a particular user called the 'owner' who has privileges over the process. The identification for the user is 'UserID'. Owner is the user who executes the process. Process also has 'Effective User ID' which determines the access privileges for accessing resources like files.
getpid() -process id
getppid() -parent process id
getuid() -user id
geteuid() -effective user id
Unix identifies each process with a unique integer called ProcessID. The process that executes the request for creation of a process is called the 'parent process' whose PID is 'Parent Process ID'. Every process is associated with a particular user called the 'owner' who has privileges over the process. The identification for the user is 'UserID'. Owner is the user who executes the process. Process also has 'Effective User ID' which determines the access privileges for accessing resources like files.
getpid() -process id
getppid() -parent process id
getuid() -user id
geteuid() -effective user id
Explain fork() system call.
The `fork()' used to create a new process from an existing process. The new process is called the child process, and the existing process is called the parent. We can tell which is which by checking the return value from `fork()'. The parent gets the child's pid returned to him, but the child gets 0 returned to him.
The `fork()' used to create a new process from an existing process. The new process is called the child process, and the existing process is called the parent. We can tell which is which by checking the return value from `fork()'. The parent gets the child's pid returned to him, but the child gets 0 returned to him.
Predict the output of the following program code
main()
{
fork();
printf("Hello World!");
}
Answer:
Hello World!Hello World!
Explanation:
The fork creates a child that is a duplicate of the parent process. The child begins from the fork().All the statements after the call to fork() will be executed twice.(once by the parent process and other by child). The statement before fork() is executed only by the parent process.
{
fork();
printf("Hello World!");
}
Answer:
Hello World!Hello World!
Explanation:
The fork creates a child that is a duplicate of the parent process. The child begins from the fork().All the statements after the call to fork() will be executed twice.(once by the parent process and other by child). The statement before fork() is executed only by the parent process.
Predict the output of the following program code
main()
{
fork(); fork(); fork();
printf("Hello World!");
}
Answer:
"Hello World" will be printed 8 times.
Explanation:
2^n times where n is the number of calls to fork()
main()
{
fork(); fork(); fork();
printf("Hello World!");
}
Answer:
"Hello World" will be printed 8 times.
Explanation:
2^n times where n is the number of calls to fork()
List the system calls used for process management:
System calls Description
fork() To create a new process
exec() To execute a new program in a process
wait() To wait until a created process completes its execution
exit() To exit from a process execution
getpid() To get a process identifier of the current process
getppid() To get parent process identifier
nice() To bias the existing priority of a process
brk() To increase/decrease the data segment size of a process
System calls Description
fork() To create a new process
exec() To execute a new program in a process
wait() To wait until a created process completes its execution
exit() To exit from a process execution
getpid() To get a process identifier of the current process
getppid() To get parent process identifier
nice() To bias the existing priority of a process
brk() To increase/decrease the data segment size of a process
How can you get/set an environment variable from a program?:
Getting the value of an environment variable is done by using `getenv()'. Setting the value of an environment variable is done by using `putenv()'.
Getting the value of an environment variable is done by using `getenv()'. Setting the value of an environment variable is done by using `putenv()'.
How can a parent and child process communicate?
A parent and child can communicate through any of the normal inter-process communication schemes (pipes, sockets, message queues, shared memory), but also have some special ways to communicate that take advantage of their relationship as a parent and child. One of the most obvious is that the parent can get the exit status of the child.
A parent and child can communicate through any of the normal inter-process communication schemes (pipes, sockets, message queues, shared memory), but also have some special ways to communicate that take advantage of their relationship as a parent and child. One of the most obvious is that the parent can get the exit status of the child.
What is a zombie?
When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in case the parent might need it - for example, the parent may need to check the child's exit status. To be able to get this information, the parent calls `wait()'; In the interval between the child terminating and the parent calling `wait()', the child is said to be a `zombie' (If you do `ps', the child will have a `Z' in its status field to indicate this.)
When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in case the parent might need it - for example, the parent may need to check the child's exit status. To be able to get this information, the parent calls `wait()'; In the interval between the child terminating and the parent calling `wait()', the child is said to be a `zombie' (If you do `ps', the child will have a `Z' in its status field to indicate this.)
What are the process states in Unix?
As a process executes it changes state according to its circumstances. Unix processes have the following states:
Running : The process is either running or it is ready to run .
Waiting : The process is waiting for an event or for a resource.
Stopped : The process has been stopped, usually by receiving a signal.
Zombie : The process is dead but have not been removed from the process table.
As a process executes it changes state according to its circumstances. Unix processes have the following states:
Running : The process is either running or it is ready to run .
Waiting : The process is waiting for an event or for a resource.
Stopped : The process has been stopped, usually by receiving a signal.
Zombie : The process is dead but have not been removed from the process table.
What Happens when you execute a program?
When you execute a program on your UNIX system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system. Each process has process context, which is everything that is unique about the state of the program you are currently running. Every time you execute a program the UNIX system does a fork, which performs a series of operations to create a process context and then execute your program in that context. The steps include the following:
Allocate a slot in the process table, a list of currently running programs kept by UNIX.
Assign a unique process identifier (PID) to the process.
iCopy the context of the parent, the process that requested the spawning of the new process.
Return the new PID to the parent process. This enables the parent process to examine or control the process directly. After the fork is complete, UNIX runs your program.
When you execute a program on your UNIX system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system. Each process has process context, which is everything that is unique about the state of the program you are currently running. Every time you execute a program the UNIX system does a fork, which performs a series of operations to create a process context and then execute your program in that context. The steps include the following:
Allocate a slot in the process table, a list of currently running programs kept by UNIX.
Assign a unique process identifier (PID) to the process.
iCopy the context of the parent, the process that requested the spawning of the new process.
Return the new PID to the parent process. This enables the parent process to examine or control the process directly. After the fork is complete, UNIX runs your program.
What Happens when you execute a command?
When you enter 'ls' command to look at the contents of your current working directory, UNIX does a series of things to create an environment for ls and the run it: The shell has UNIX perform a fork. This creates a new process that the shell will use to run the ls program. The shell has UNIX perform an exec of the ls program. This replaces the shell program and data with the program and data for ls and then starts running that new program. The ls program is loaded into the new process context, replacing the text and data of the shell. The ls program performs its task, listing the contents of the current directory.
When you enter 'ls' command to look at the contents of your current working directory, UNIX does a series of things to create an environment for ls and the run it: The shell has UNIX perform a fork. This creates a new process that the shell will use to run the ls program. The shell has UNIX perform an exec of the ls program. This replaces the shell program and data with the program and data for ls and then starts running that new program. The ls program is loaded into the new process context, replacing the text and data of the shell. The ls program performs its task, listing the contents of the current directory.
What is a Daemon?
A daemon is a process that detaches itself from the terminal and runs, disconnected, in the background, waiting for requests and responding to them. It can also be defined as the background process that does not belong to a terminal session. Many system functions are commonly performed by daemons, including the sendmail daemon, which handles mail, and the NNTP daemon, which handles USENET news. Many other daemons may exist. Some of the most common daemons are:
init: Takes over the basic running of the system when the kernel has finished the boot process.
inetd: Responsible for starting network services that do not have their own stand-alone daemons. For example, inetd usually takes care of incoming rlogin, telnet, and ftp connections.
cron: Responsible for running repetitive tasks on a regular schedule.
A daemon is a process that detaches itself from the terminal and runs, disconnected, in the background, waiting for requests and responding to them. It can also be defined as the background process that does not belong to a terminal session. Many system functions are commonly performed by daemons, including the sendmail daemon, which handles mail, and the NNTP daemon, which handles USENET news. Many other daemons may exist. Some of the most common daemons are:
init: Takes over the basic running of the system when the kernel has finished the boot process.
inetd: Responsible for starting network services that do not have their own stand-alone daemons. For example, inetd usually takes care of incoming rlogin, telnet, and ftp connections.
cron: Responsible for running repetitive tasks on a regular schedule.
What is 'ps' command for?
The ps command prints the process status for some or all of the running processes. The information given are the process identification number (PID),the amount of time that the process has taken to execute so far etc.
The ps command prints the process status for some or all of the running processes. The information given are the process identification number (PID),the amount of time that the process has taken to execute so far etc.
How would you kill a process?
The kill command takes the PID as one argument; this identifies which process to terminate. The PID of a process can be got using 'ps' command.
The kill command takes the PID as one argument; this identifies which process to terminate. The PID of a process can be got using 'ps' command.
What is an advantage of executing a process in background?
The most common reason to put a process in the background is to allow you to do something else interactively without waiting for the process to complete. At the end of the command you add the special background symbol, &. This symbol tells your shell to execute the given command in the background.
Example: cp *.* ../backup& (cp is for copy)
The most common reason to put a process in the background is to allow you to do something else interactively without waiting for the process to complete. At the end of the command you add the special background symbol, &. This symbol tells your shell to execute the given command in the background.
Example: cp *.* ../backup& (cp is for copy)
How do you execute one program from within another?
The system calls used for low-level process creation are execlp() and execvp(). The execlp call overlays the existing program with the new one , runs that and exits. The original program gets back control only when an error occurs. execlp(path,file_name,arguments..); //last argument must be NULL A variant of execlp called execvp is used when the number of arguments is not known in advance. execvp(path,argument_array); //argument array should be terminated by NULL
The system calls used for low-level process creation are execlp() and execvp(). The execlp call overlays the existing program with the new one , runs that and exits. The original program gets back control only when an error occurs. execlp(path,file_name,arguments..); //last argument must be NULL A variant of execlp called execvp is used when the number of arguments is not known in advance. execvp(path,argument_array); //argument array should be terminated by NULL
What is IPC? What are the various schemes available?
The term IPC (Inter-Process Communication) describes various ways by which different process running on some operating system communicate between each other. Various schemes available are as follows: Pipes:
One-way communication scheme through which different process can communicate. The problem is that the two processes should have a common ancestor (parent-child relationship). However this problem was fixed with the introduction of named-pipes (FIFO).
Message Queues :
Message queues can be used between related and unrelated processes running on a machine.
Shared Memory:
This is the fastest of all IPC schemes. The memory to be shared is mapped into the address space of the processes (that are sharing). The speed achieved is attributed to the fact that there is no kernel involvement. But this scheme needs synchronization.
Various forms of synchronisation are mutexes, condition-variables, read-write locks, record-locks, and semaphores.
The term IPC (Inter-Process Communication) describes various ways by which different process running on some operating system communicate between each other. Various schemes available are as follows: Pipes:
One-way communication scheme through which different process can communicate. The problem is that the two processes should have a common ancestor (parent-child relationship). However this problem was fixed with the introduction of named-pipes (FIFO).
Message Queues :
Message queues can be used between related and unrelated processes running on a machine.
Shared Memory:
This is the fastest of all IPC schemes. The memory to be shared is mapped into the address space of the processes (that are sharing). The speed achieved is attributed to the fact that there is no kernel involvement. But this scheme needs synchronization.
Various forms of synchronisation are mutexes, condition-variables, read-write locks, record-locks, and semaphores.
What is the difference between Swapping and Paging?
Swapping: Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.
Paging:
Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory.
It provides greater flexibility in mapping the virtual address space into the physical memory of the machine. Allows more number of processes to fit in the main memory simultaneously. Allows the greater process size than the available physical memory. Demand paging systems handle the memory more flexibly.
Swapping: Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.
Paging:
Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory.
It provides greater flexibility in mapping the virtual address space into the physical memory of the machine. Allows more number of processes to fit in the main memory simultaneously. Allows the greater process size than the available physical memory. Demand paging systems handle the memory more flexibly.
What is major difference between the Historic Unix and the new BSD release of Unix System V in terms of Memory Management?
Historic Unix uses Swapping – entire process is transferred to the main memory from the swap device, whereas the Unix System V uses Demand Paging – only the part of the process is moved to the main memory. Historic Unix uses one Swap Device and Unix System V allow multiple Swap Devices.
Historic Unix uses Swapping – entire process is transferred to the main memory from the swap device, whereas the Unix System V uses Demand Paging – only the part of the process is moved to the main memory. Historic Unix uses one Swap Device and Unix System V allow multiple Swap Devices.
What is the main goal of the Memory Management?
It decides which process should reside in the main memory, Manages the parts of the virtual address space of a process which is non-core resident, Monitors the available main memory and periodically write the processes into the swap device to provide more processes fit in the main memory simultaneously.
It decides which process should reside in the main memory, Manages the parts of the virtual address space of a process which is non-core resident, Monitors the available main memory and periodically write the processes into the swap device to provide more processes fit in the main memory simultaneously.
What is Fork swap?
fork() is a system call to create a child process. When the parent process calls fork() system call, the child process is created and if there is short of memory then the child process is sent to the read-to-run state in the swap device, and return to the user state without swapping the parent process. When the memory will be available the child process will be swapped into the main memory.
fork() is a system call to create a child process. When the parent process calls fork() system call, the child process is created and if there is short of memory then the child process is sent to the read-to-run state in the swap device, and return to the user state without swapping the parent process. When the memory will be available the child process will be swapped into the main memory.
Which are typical system directories below the root directory?
(1)/bin: contains many programs which will be executed by users (2)/etc : files used by administrator (3)/dev: hardware devices (4)/lib: system libraries (5)/usr: application software (6)/home: home directories for different systems.
(1)/bin: contains many programs which will be executed by users (2)/etc : files used by administrator (3)/dev: hardware devices (4)/lib: system libraries (5)/usr: application software (6)/home: home directories for different systems.
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.
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.
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.
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.
Which of the following commands is not a filter man , (b) cat , (c) pg , (d) head
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.
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.
How is the command “$cat file2 “ different from “$cat >file2 and >> redirection operators ?
is the output redirection operator when used it overwrites while >> operator appends into the file.
is the output redirection operator when used it overwrites while >> operator appends into the file.
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 valuated as variables, unless quoted or escaped.
Command substitution: Any command surrounded by back quotes 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.
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 valuated as variables, unless quoted or escaped.
Command substitution: Any command surrounded by back quotes 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.
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
cmp - Compares two files byte by byte and displays the first mismatch diff - tells the changes to be made to make the files identical
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
‘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
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.
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.
Write a command to kill the last background job?
Kill $!
Kill $!
Which command is used to delete all files in the current directory and all its sub-directories?
rm -r *
rm -r *
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.
$od -cbd file_name
c - character, b - binary (octal), d-decimal, od=Octal Dump.
What will the following command do?
$ echo *
It is similar to 'ls' command and displays all the files in the current directory.
$ echo *
It is similar to 'ls' command and displays all the files in the current directory.
Is it possible to create new a file system in UNIX?
Yes, ‘mkfs’ is used to create a new file system.
Yes, ‘mkfs’ is used to create a new file system.
Is it possible to restrict incoming message?
Yes, using the ‘mesg’ command.
Yes, using the ‘mesg’ command.
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.
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.
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.
Yes, it stands for ‘disk usage’. With the help of this command you can find the disk capacity and free space of the disk.
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.
Yes, wc-stands for word count.
wc -c for counting number of characters in a file.
wc -l for counting lines in a file.
Name the data structure used to maintain file identification?
‘inode’, each file has a separate inode and a unique inode number.
‘inode’, each file has a separate inode and a unique inode number.
How many prompts are available in a UNIX system?
Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).
Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).
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.
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.
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.
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.
No comments:
Post a Comment