List Command
The ls command is used to list files. ``ls'' on its own lists all files in the current directory except
for hidden files. ``ls *.tex'' lists only those files ending in ``.tex''. There are a large number of
options; here are some of the most useful. Options can be combined (this is a general principle of
Unix commands) - for example ``ls -la'' gives a long listing of all files.
ls -a will list all files including hidden files (files with names beginning with a dot).
ls -F gives a full listing, indicating what type files are by putting a slash after directories
and a star after executable files (programs you can run).
ls -l gives a long listing of all files. Here is an example section of the output of ls -l :
drwxr-xr-x 6 eva users 1024 Jun 8 16:46 sabon
-rw------- 1 eva users 1564 Apr 28 14:35 splus
-rw------- 1 eva users 1119 Apr 28 16:00 splus2
-rw-r--r-- 1 eva users 9753 Sep 27 11:14 ssh_known_hosts
-rw-r--r-- 1 eva users 4131 Sep 21 15:23 [Link]
-rw-r--r-- 1 eva users 94031 Sep 1 16:07 [Link]
What does it all mean?
o The first column gives the type of the file (e.g., directory or ordinary file) and the
file permissions.
o The second column is the number of links to the file i.e., (more or less) the
number of names there are for the file. Generally an ordinary file will only have
one link, but a directory will have more, because you can refer to it as ``dirname'',
``dirname/.'' where the dot means ``current directory'', and if it has a subdirectory
named ``subdir'', ``dirname/subdir/..'' (the ``..'' means ``parent directory'').
o The third and fourth columns are the user who owns the file and the Unix group
of users to which the file belongs. You almost certainly don't need to worry about
Unix groups, as you probably only belong to the default group ``users''.
o The fifth column is the size of the file in bytes.
o The next three columns are the time at which the file was last changed (for a
directory, ths is the time at which a file in that directory was last created or
deleted).
o The last column is the name of the file.
ls -R gives a recursive listing, including the contents of all subdirectories and their
subdirectories and so on.
ls -t lists the files in order of the time when they were last modified (newest first) rather
than in alphabetical order.
ls -r lists the files in the reverse of the order that they would otherwise have been listed
in.
ls -lrt will give a long listing, oldest first, which is handy for seeing which files in a large
directory have recently been changed.
ls -lrt | grep “^l”
How to list all shortcuts links from a unix directory?
Note:
Grep - print lines matching a pattern
^ - matches start of the line
^1 - this will search “1” from the line.
In our case we are searching for “l” from each standard output of List
Command ( ls –lrt )
$ ls / -lrt | grep “^l”
UNIX Interview Questions and Answers
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 ch-mod
command you can also change your u-mask to create read only file.
$ touch file
$ chmod 400 file
Read more about file and directory permission in unix and linux here.
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 "&"
For bringing it back in foreground use command "fg jobid"
For getting job id you use command jobs
For finding the process id use PID
For killing that process use kill -9 PID command.
6. How do you see command line history in UNIX?
Very useful indeed, use history command along with grep command in
UNIX 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.
$ history
Output:
911 man 7 signal
912 man ps
913 man 7 signal
914 man killall
915 killall -l
916 man killall
917 su -
918 su - lighttpd
919 su - lighttpd
920 cd /tmp/
921 ncftpget -u 'vivek@[Link]' [Link]
7. How do you copy file from one host to other?
Many options but you can say by using "scp"/ rsync/ sftp command.
scp [Link] /var/tmp/
This command copies the file [Link] from current directory to the /var/tmp directory.
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?
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?
chmod command is used to change permission of a file or directory in UNIX. The line you see
shows the permission for three different set of people : user, group and others. User is the
currently logged in user, while group is for all other member which are part of certain group and
others means anyone other than user and group member. Each group has three permissions rwx
stands for read, write and execute and they are written as user_group_others. So in above line,
user has only read permission, group members has write permissions and other people has only
execute permission. If it is a directory then you need execute permission to go inside that
directory.
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
find -name "[Link]"
./bkp/[Link]
./[Link]
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 command, for example export count=1 will be available on
all sub shell.
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.
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.
10. You have an IP address in your network how will you find
hostname and vice versa?
nslookup command in UNIX, you can read more about convert IP Address
to hostname in Unix here.
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.
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.
3. What is nohup in UNIX?
nohup is a special command which is used to run process in background, but
it is slightly different than & which is normally used for putting a process in
background. An UNIX process started with nohup will not stop even if the
user who has stared log off from system. While background process started
with & will stop as soon as user logoff.
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 following command to
replace all Unix word to UNIX
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
Background Processes in Unix/Linux
The & is an important little character in UNIX; it means "run the command in the background";
i.e., detach it from the window it was started from, so it does not block the command line.
Should the program ever try to read from the terminal window, it will be suspended, until the
user "brings it to the foreground"; i.e., brings it to the state it would have had without the & to
begin with.
To bring a program to the foreground, use "fg" or "%". If you have more than one background
job to choose from ("jobs" will show you), then use for example "%2" to choose the second
one.
Important:
If you forget to give the & at the end of line, and the process blocks the command input to the
terminal window, you can put the process in the background "after the fact", by using Ctrl-Z.
The process is suspended, and you get the command prompt back. The first thing you should
do then is probably to give the command "bg", that resumes the process, but now in the
background.
$Id: [Link],v 1.4 2007/02/07 16:18:31 aake Exp $
To run the count program, which will display the process identification number of the
job, enter:
count &
To check the status of your job, enter:
ps
To bring a background process to the foreground, enter:
fg
If you have more than one job suspended in the background, enter:
fg %#
Replace # with the job number, as shown in the first column of the output of the jobs
command.
You can kill a background process by entering:
kill PID
Replace PID with the process ID of the job. If that fails, enter the following:
kill -KILL PID
To determine a job's PID, enter:
jobs -l
If you are using sh, ksh, bash, or zsh, you may prevent background processes from
sending error messages to the terminal. Redirect the output to /dev/null using the
following syntax:
count 2> /dev/null &