https://www.cyberciti.biz/faq/how-to-find-my-public-ip-address-from-command-line-on-a-linux/
How to grep a text file which contains some binary data?
Reference: http://javarevisited.blogspot.com/2011/05/unix-command-interview-questions.html
What is difference between ps -ef and ps -auxwww?
ps -ef will omit process with very long command line while ps -auxwww will list those process as well.
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.
In a file word UNIX is appearing many times? How will you count number?
grep -c "Unix" filename
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.
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.
http://linuxcommando.blogspot.com/2007/10/grep-with-color-output.html
There are 3 color options available to you:
With color=always, it colors the matched string.
Quite often, you want to page through the output:
The problem is that less does not understand those control characters, by default. You need to use the -R parameter.
Alternatively, use more.
Another problematic scenario is when you want to save the grep output to a file. The output file will contain those control characters.
With color=auto, it displays color in the output unless the output is piped to a command, or redirected to a file.
http://unix.stackexchange.com/questions/9252/determining-what-process-is-bound-to-a-port
sed s/Unix/UNIX/g fileName
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.
IP address using nslookup command
nslookup servername
IP address using ping command
ping servername
hostname -i
How to find all the links in a folder in UNIX or Linux ?
- dig +short myip.opendns.com @resolver1.opendns.com
- Or dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
fgrep
fgrep is an acronym that stands for "Fixed-string Global Regular Expressions Print".
fgrep (which is the same as grep -F) is fixed or fast grep and behaves as grep but does NOT recognize any regular expression meta-characters as being special. The search will complete faster because it only processes a simple string rather than a complex pattern.
For example, if I wanted to search my .bash_profile for a literal dot (.) then using grep would be difficult because I would have to escape the dot because dot is a meta character that means 'wild-card, any single character':
grep "." myfile.txt
The above command returns every line of myfile.txt. Do this instead:
fgrep "." myfile.txt
Then only the lines that have a literal '.' in them are returned. fgrep helps us not bother escaping our meta characters.
pgrep
pgrep is an acronym that stands for "Process-ID Global Regular Expressions Print".
pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. pgrep is handy when all you want to know is the process id integer of a process. For example, if I wanted to know only the process ID of my mysql process I would use the command
pgrep mysql
which would return a process ID like 7312.How to grep a text file which contains some binary data?
You could run the data file through
cat -v
, e.g$ cat -v tmp/test.log | grep re
http://tech-wonderland.net/blog/shell-pattern-explained-with-shell-script-examples.htmlReference: http://javarevisited.blogspot.com/2011/05/unix-command-interview-questions.html
What is difference between ps -ef and ps -auxwww?
ps -ef will omit process with very long command line while ps -auxwww will list those process as well.
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.
In a file word UNIX is appearing many times? How will you count number?
grep -c "Unix" filename
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.
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.
http://linuxcommando.blogspot.com/2007/10/grep-with-color-output.html
There are 3 color options available to you:
- --color=auto
- --color=always
- --color=never
With color=always, it colors the matched string.
$ grep --color=always abc a_file.txt
abcdef
Quite often, you want to page through the output:
$ grep --color=always abc a_file.txt |less ESC[01;31mabcESC[00mdef (END)
The problem is that less does not understand those control characters, by default. You need to use the -R parameter.
$ grep --color=always abc a_file.txt |less -R
abcdef
Alternatively, use more.
$ grep --color=always abc a_file.txt | more
abcdef
Another problematic scenario is when you want to save the grep output to a file. The output file will contain those control characters.
$ grep --color=always abc a_file.txt > myoutput.txt $ less myoutput.txt ESC[01;31mabcESC[00mdef myoutput.txt (END)
With color=auto, it displays color in the output unless the output is piped to a command, or redirected to a file.
http://unix.stackexchange.com/questions/9252/determining-what-process-is-bound-to-a-port
netstat -lntp
netstat -lnp
will list the pid and process name next to each listening port. This will work under Linux, but not all others (like AIX.) Add -t
if you want TCP only.
There is a file Unix_Test.txt which contains words Unix, how will you replace all Unix to UNIX? sed s/Unix/UNIX/g fileName
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.
IP address using nslookup command
nslookup servername
IP address using ping command
ping servername
hostname -i
How to find all the links in a folder in UNIX or Linux ?
You need to use ls command which list everything in directory and then you need to list all the links, as they starts with "l" as first characters.
ls -lrt | grep '^l'
List the files in current directory sorted by size ? -
ls -l | grep ^- | sort -nr
List the hidden files in current directory ?
ls -a1 | grep "^\."
Delete blank lines in a file ?
cat sample.txt | grep -v ‘^$’ > new_sample.txt
How to find a process and kill that ?
ps -ef| grep process_identifier // will give you PID
kill -9 PID
How to run a program in background in UNIX or Linux ?
You can use & to run any process in background and than you can use jobs to find the job id for that process and can use fg and bg command to bring that process into foreground and background.
How do you find all files which are modified 10 minutes before ?
This is another the Linux interview questions from frequently used command e.g. find and grep. you can use -mtime option of find command to list all the files which are modified 10 or m minutes before.
find . -mtime 1 (find all the files modified exact 1 day)
find . -mtime -1 (find all the files modified less than 1 day)
find . -mtime +1 (find all the files modified more than 1 day)
Display disk usage in Kilobytes ? - du -k
References: http://opensourcewin.wordpress.com/2012/05/11/linux-system-administrator-interview-questions-and-answer-with-pdf-part-01/
What command can be used to findout server architechure (x86 or x64) apart from uname?
arch
How do u findout the users who are NOT logged in for more than 30 days? which file u will check?
last , lastlog, /var/log/wtmp
What command can be used to findout server architechure (x86 or x64) apart from uname?
arch
How do u findout the users who are NOT logged in for more than 30 days? which file u will check?
last , lastlog, /var/log/wtmp
References