SCP
SCP stands for secure copy is used to copy data (files or directories) from one unix or linux system to another unix or linux server. SCP uses secured shell (ssh) to transfer the data between the remote hosts. The features of SCP are:
- Copies files within in the same machine
- Copies files from local machine to remote machine.
- Copies files from remote machine to local machine.
- Copies files between two different remote servers.
SCP Command Syntax:
The syntax of SCP command is
scp [Options] [[User@]From_Host:]Source_File [[User@]To_Host:][Destination_File]
Each element of the scp command is explained in detail below:
- User is the one who have the permissions to access the files and directories. User should have read permissions if it is a source and write permissions if it is the destination.
- From_Host: hostname or Ip address where the source file or directory resides. This is optional if the from host is the host where you are running the scp command.
- Source_File: Files or directories to be copied to the destination.
- To_Host: Destination host where you want to copy the files. You can omit this when you want to copy the files to the host where you are issuing the scp command.
- Destination_File: Name of the file or directory in the target host.
SCP Command Options:
The important SCP command options are listed below:
- -r : Recursively copies the contents of source files or directories.
- -p : Preserves the access time, modification time, permissions of the source files in the destination.
- -q : Progress bar in not displayed
- -v : verbose mode. Displays debugging messages.
- -P : copy files using the specified port number.
SCP Command Examples:
Let see the examples of scp command in unix or linux system.
. Copying with in the same system
You can use the scp command just like the cp command to copy files from one directory to another directory.
scp Unix-storage.dat /var/tmp/
This command copies the file unix-storage.dat from current directory to the /var/tmp directory.
. Copy file from local host to remote server
This is most frequently used operation to transfer files in unix system.
scp filename user@remotehost:/remote/directory/
This command connects to the remote host and copies the specified file to the /remote/directory/.
. Copy files from remote host to local server.
This operation is used when taking backup of the files in remote server.
scp user@remotehost:/usr/backup/oracle_backup.dat .
This command copies the oracle backup file in the remote host to the current directory.
. Copying files between two remote servers
The scp command can also be used to copy files between two remote hosts.
scp source_user@source_remote_host:/usr/bin/mysql_backup.sh target_user@target_remote_host:/var/tmp/
The above command copies the mysql bakup shell script from the source remote host the /var/tmp directory of target remote host.
. Copying a directory.
To copy all the files in a directory, use the -r option with the scp command. This makes the scp command to copy the directory recursively.
scp -r directory user@remotehost:/var/tmp/
The above command copies the directory from local server to the remote host.
. Improving performance of scp command
By default the scp command uses the Triple-DES cipher/AES-128 to encrypt the data. Using the blowfish or arcfour encryption will improve the performance of the scp command.
scp -c blowfish filename user@remoteserver:/var/ scp -c arcfour localfile user@remoteserver:/var/
. Limit bandwidth
You can limit the bandwidth used by the scp command using the -l option.
scp -l bandwidth_limit filename user@hostname:/usr/backup/ Here bandwidth_limit is numeric to be specified in kilobits per second.
. Specifying the port number
We can make the scp command to copy the files over a specified port number using the -P option.
SSH
SSH client utility in unix or linux server is used to logging into a remote host and execute commands on the remote machine. The rlogin and rsh commands can also be used to login into the remote machine. However these are not secure. The ssh command provides a secure connection between two hosts over a insecure network.
ssh [-l username] hostname | user@remote-hostname [command]
Let see the examples of ssh command.
SSH Command Examples:
. Logging to a remote server
You can login to a remote server from the local host as shown below:
localhost:[~]> ssh -l username remote-server username@remote-server password: remote-server:[~]>
Alternatively you can use the below ssh command for connecting to remote host:
localhost:[~]> ssh username@remote-server username@remote-server password: remote-server:[~]>
Note: If you are logging for the first time, then it will prints a message that host key not found and you can give yes to continue. The host key of the remote server will be cached and added to the .ssh2/hostkeys directory in your home directory. From second time onwards you just need to enter the password.
. Logging out from remote server
Simply enter the exit command on the terminal to close the connection. This is shown below:
remote-server:[~]>exit logout Connection to remote-server closed. localhost:[~]>
. Running remote commands from local host
Sometimes it is necessary to run the unix commands on the remote server from the local host. An example is shown below:
localhost:[~]> ssh user@remote-host "ls test" online-backup.dat oracle-storage.bat unix-dedicated-server.txt
The ssh command connects to the remote host, runs the ls command, prints the output on the local host terminal and exits the connection from remote host.
Let see whether the ls command actually displayed the correct result or not by connecting to the remote host.
localhost:[~]> ssh user@remote-host user@remotehost password: remotehost:[~]> cd test remotehost:[~/test]> ls online-backup.dat oracle-storage.bat unix-dedicated-server.txt
. Version of the SSH command
We can find the version of SSH installed on the unix system using the -V option to the ssh. This is shown below:
> ssh -V OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
. Debugging the SSH Client
When we are not able to connect to the remote host, it is good to debug and find the exact error messages that causing the issue. Use the -v option for debugging the ssh client.
ssh -v user@remote-host OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to remote-host [172.22.200.140] port 22. debug1: Connection established. debug1: identity file /home/user/.ssh/identity type -1 debug1: identity file /home/user/.ssh/id_rsa type -1 debug1: identity file /home/user/.ssh/id_dsa type 2 debug1: loaded 3 keys .......... ..........
. Copying files between remote host and local host.
We can use the scp command to copy the files securely between the local host and remote host using the ssh authentication.
To copy the file from local host to remote hosts /var/tmp/ directory, run the below scp command.
scp filename user@remote-host:/var/tmp/
To copy the file from remote hosts /usr/local/bin/ directory to local hosts current directory, run the below scp command.
scp user@remote-host:/usr/local/bin/add.sh .
CHMOD
Chmod (change mode) is one of the most frequently used commands in unix or linux operating system. The chmod command is used to change the file or directory access permissions. To know about the access permissions of a file or directory, use the ls -l command as shown below:
$ ls -l sample.sh -rwx-rw-r-- 1 matt deploy 94 Oct 4 03:12 sample.sh
Here in the above example: Use matt has the read, write and execute permissions on the file. Group deploy has read and write permissions. Others have only the read permission.
File and Directory permissions:
There are three different permissions. They are:
- Read (4): Permitted to read the contents of the file. In case of directory, you can view all the files and sub-directories in that directory.
- Write (2): Permitted to write to the file. In case of directory, you can create files and sub-directories.
- Execute (1): Execute the file as a program/shell script. In case of directory, You can enter into that directory.
Here in the above, the numbers in the brackets represents the numeric values for the corresponding permissions. If you want to have a combination of permissions add the required numbers. For example, for read and execute, it is 4+1=5.
The syntax of chmod command is
chmod [options] mode filename
THe important options are:
-R : recursively change the permissions of a directory. -v : Verbose
Chmod Examples in Linux / Unix:
. Give read, write and execute permissions to everyone.
Read, write and execute: 4+2+1=7
$ chmod 777 sample.sh
In the above example, you can see that the permissions are specified with a three digit number. The first digit is for user permissions, second is for group and third is for others permission. This type of representation is called octal representation. Alternatively, you can use the symbolic representation to give the permissions.
chmod ugo+rwx sample.sh
We will see more details about the symbolic representation later.
. Give read permission to user, write permission to group and execute permission to others.
$ chmod 421 sample.sh
. Recursive permissions to directory
To give read and write permissions to all the users to a directory (including files and subdirectories) use the recursive option -R.
chmod -R 666 /dir
Symbolic Representation of Permissions:
The following symbols are used to represent the users, groups and others:
- u : User
- g : Group
- o : Others a : All (user, group and others)
The following symbols represent the permissions:
- r : read
- w : write
- x : execute
The following symbols represent the permissions grant or revoke:
- + : Additional permissions. Selected permissions are added.
- – : Revoke the permissions. Selected permissions are revoked.
- = : Specific permissions. Only selected permissions are assigned.
Examples:
. Remove write permission from group
$ chmod g-w sample.sh
This will only removes the write permission for the group.
. Add new permission execute to others
$ chmod o+x sample.sh
In addition to the existing permissions, this will add execute permission to others.
. Give only read permissions to the user
$ chmod u=w sample.sh
This will remove the existing permissions to the user and gives only write permission to the user
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)
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
Use “CRTL+R” for repeating the last matching command
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.
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.
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.
Using pipe instead of firing two commands
Just shown above this nice and little tip I guess everybody knows 🙂
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.
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.
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.other option to get old commands
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.
Write command to list all the links from a directory?
ls -lrt | grep “^l”
Create a read-only file in your home directory?
$ touch file $ chmod 400 file
How will you find which operating system your system is running on in UNIX?
By using command “uname -a” in UNIX
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.
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.
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.
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 sftp would be ok.
How do you find which process is taking how much CPU?
By using “top” command in UNIX,
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.
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.
What is difference between ps -ef and ps -auxwww?
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.
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
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
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.
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. See here for more detailed answer.
In a file word UNIX is appearing many times? How will you count number?
grep -c “Unix” filename
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.
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.
• 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 another hostname – telnet
• outing steps that packets take to get to network host – traceroute
• view user information – finger
• checking status of destination host – telnet
http://javarevisited.blogspot.com/2010/10/basic-networking-commands-in-linuxunix.html
How do you find whether your system is 32 bit or 64 bit ?
Either by using “uname -a” command or by using “arch” command.
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.
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.
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.
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.
If one process is inserting data into your MySQL database? How will you check how many rows inserted into every second?
There is a file Unix_Test.txt which contains words Unix, how will you replace all Unix to UNIX?
SED command in UNIX for example you can execute following command to replace all Unix word 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?
AWK or CUT command here. CUT use tab as default separator so you can use
cut -f3 filename
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.
How do you find for how many days your Server is up?
By using uptime command in UNIX
You have an IP address in your network how will you find hostname and vice versa?
By using nslookup command in UNIX, you can read more about convert IP Address to hostname in Unix here.
1. What command can you use to display the first 3 lines of text from a file and how does it work?
There are two commands that can be used to complete this task:
head -3 test.txt – this uses the “head” command, along with the “-3” parameter that indicates the number of lines to be displayed;
sed ‘4,$ d’ test.txt – this command uses the Sed text editor to perform the task. If the command was simply “sed test.txt” the whole file would have been displayed; however, in our example the delete parameter was used (d) to make Sed delete everything between the 4th and the last line (defined by the $ parameter), leaving only the first 3 lines of the file. It is important to mention that Sed does not actually delete the lines from the file itself, but just from the output result.
2. How can you remove the 7th line from a file?
The easiest way is by using the following command: sed -i ‘7 d’ test.txt
Unlike the previous Sed command, this command also has the “-i” parameter, which tells Sed to make the change in the actual file.
3. What is piping?
Piping is a technique that consists of entering two or more consecutive commands, separated by the pipe symbol “|”. Once the first command is executed, its output will be used as input for the second command, the output of the second command will be used as input for the third and so on, until the whole chain of commands is executed.
4. How do you reverse a string?
You can reverse a string by using a simple piping of two commands: echo “Mary” | rev
The first command will generate the output “Mary”, which will become the input for the rev command, making it return the reverse: “yraM”.
5. How can you find out what a command does?
You use man <command-name> in order to bring up the manual page that describes the actions of the specified command and any other additional options and parameters that command might have.
These are some of the UNIX questions interviewers usually ask during basic interviews. However, if the interview is for a more technical position, you can expect to encounter questions that are more difficult; still, there is no reason to panic, as UNIX itself was created to be quite logical, so a good knowledge of the basic commands and a bit of imagination can help you get the job done.
Check out this online course to learn how to use the UNIX command line to get the most out of OS X – yes, the OS X that runs on your Mac, which is UNIX based by the way. If this doesn’t convince you that UNIX can be simple and intuitive, nothing else will.
1. How are devices represented in UNIX?
All devices are represented by files called special files that are located in /dev directory. Thus, device files and other files are named and accessed in the same way. A ‘regular file’ is just an ordinary data file in the disk. A ‘block special file’ represents a device with characteristics similar to a disk (data transfer in terms of blocks). A ‘character special file’ represents a device with characteristics similar to a keyboard (data transfer is by stream of bits in sequential order).
2. What is ‘inode’?
All UNIX files have its description stored in a structure called ‘inode’. The inode contains info about the file-size, its location, time of last access, time of last modification, permission and so on. Directories are also represented as files and have an associated inode. In addition to descriptions about the file, the inode contains pointers to the data blocks of the file. If the file is large, inode has indirect pointer to a block of pointers to additional data blocks (this further aggregates for larger files). A block is typically 8k.
Inode consists of the following fields:
- File owner identifier
- File type
- File access permissions
- File access times
- Number of links
- File size
- Location of the file data
3. Brief about the directory representation in UNIX.
A Unix directory is a file containing a correspondence between filenames and inodes. A directory is a special file that the kernel maintains. Only kernel modifies directories, but processes can read directories. The contents of a directory are a list of filename and inode number pairs. When new directories are created, kernel makes two entries named ‘.’ (refers to the directory itself) and ‘..’ (refers to parent directory). System call for creating directory is mkdir (pathname, mode).
4. What are the Unix system calls for I/O?
- open(pathname,flag,mode) – open file
- creat(pathname,mode) – create file
- close(filedes) – close an open file
- read(filedes,buffer,bytes) – read data from an open file
- write(filedes,buffer,bytes) – write data to an open file
- lseek(filedes,offset,from) – position an open file
- dup(filedes) – duplicate an existing file descriptor
- dup2(oldfd,newfd) – duplicate to a desired file descriptor
- fcntl(filedes,cmd,arg) – change properties of an open file
- ioctl(filedes,request,arg) – change the behaviour of an open file
- The difference between fcntl anf ioctl is that the former is intended for any open file, while the latter is for device-specific operations.
5. How do you change File Access Permissions?
Every file has following attributes:
- owner’s user ID ( 16 bit integer )
- owner’s group ID ( 16 bit integer )
- File access mode word
(r w x) – (r w x) – (r w x)
(user permission) – (group permission) – (others permission)
To change the access mode, we use chmod(filename,mode).
Example 1:
To change mode of myfile to ‘rw-rw-r–‘ (ie. read, write permission for user – read,write permission for group – only read permission for others) we give the args as:
chmod(myfile,0664) .
Each operation is represented by discrete values
‘r’ is 4
‘w’ is 2
‘x’ is 1
Therefore, for ‘rw’ the value is 6(4+2).
Example 2:
To change mode of myfile to ‘rwxr–r–‘ we give the args as:
chmod(myfile,0744).
6. What are links and symbolic links in UNIX file system?
A link is a second name (not a file) for a file. Links can be used to assign more than one name to a file, but cannot be used to assign a directory more than one name or link filenames on different computers.
Symbolic link ‘is’ a file that only contains the name of another file.Operation on the symbolic link is directed to the file pointed by the it.Both the limitations of links are eliminated in symbolic links.
Commands for linking files are:
Link “ln filename1 filename2”
Symbolic link “ln -s filename1 filename2”
7. What is a FIFO?
FIFO are otherwise called as ‘named pipes’. FIFO (first-in-first-out) is a special file which is said to be data transient. Once data is read from named pipe, it cannot be read again. Also, data can be read only in the order written. It is used in interprocess communication where a process writes to one end of the pipe (producer) and the other reads from the other end (consumer).
8. How do you create special files like named pipes and device files?
The system call mknod creates special files in the following sequence.
- kernel assigns new inode,
- sets the file type to indicate that the file is a pipe, directory or special file,
- If it is a device file, it makes the other entries like major, minor device numbers.
For example:
If the device is a disk, major device number refers to the disk controller and minor device number is the disk.
9. Discuss the mount and unmount system calls.
The privileged mount system call is used to attach a file system to a directory of another file system; the unmount system call detaches a file system. When you mount another file system on to your directory, you are essentially splicing one directory tree onto a branch in another directory tree. The first argument to mount call is the mount point, that is , a directory in the current file naming system. The second argument is the file system to mount to that point. When you insert a cdrom to your unix system’s drive, the file system in the cdrom automatically mounts to “/dev/cdrom” in your system.
10. How does the inode map to data block of a file?
Inode has 13 block addresses. The first 10 are direct block addresses of the first 10 data blocks in the file. The 11th address points to a one-level index block. The 12th address points to a two-level (double in-direction) index block. The 13th address points to a three-level(triple in-direction)index block. This provides a very large maximum file size with efficient access to large files, but also small files are accessed directly in one disk read.
11. What is a shell?
A shell is an interactive user interface to an operating system services that allows an user to enter commands as character strings or through a graphical user interface. The shell converts them to system calls to the OS or forks off a process to execute the command. System call results and other information from the OS are presented to the user through an interactive interface. Commonly used shells are sh,csh,ks etc.
1. There are multiple shell types of unix. What are the features of the Bourne and Korn shell?
The Bourne shell is the standard shell of unix , it provides the following fewatures:
– It gives the ability of input/output redirection.
– Allows the usage of metacharacters for file name abbreviations.
– The environment can be customized using the shell variables.
– It provides the user with a built in command set for the creation of shell programs.
– Allows the user to control a job.
The Korn shell is an extension of the Bourne shell which is backward compatible. The feature unique to the Korn shell are as follows:
– Allows the user to do command line editing.
– It maintains a command history, which enables the user to check the last commands executed.
– There is a provision for integer arithmetic.
– Provides the support for arrays and arithmetic expressions.
– Give the user the option to use aliases which is used to abbreviate the command name.
Linux test part 1 (27 questions)
Linux test part 2 (25 questions)
Linux test part 3 (25 questions)
2. What do you understand by File modes in unix?
Unix enables the user to set the desired level of privacy. It allows the user to set the access permission of the file. This is know as the file mode of a file.
The file permissions of file are displayed in the form :
drwxrwxrw filename date time
The letters r means read , w means write and x means execute. When the permission of a file is set by the user it is necessary to classify the users into three categories:
owner: the user who creates the file.
group: other users having the same permissions as the group owner.
other: all other user.
In the text drwxrwxrw the d letter signifies the directory.Following the d letter the next three letters specify the permissions of the owner. the next three specify the permissions of the group. The last three characters specify the permissions of the other users.
3. What is the alias mechanism in UNIX?
The alias command allows the user to specify or assign another name to a command. It can be also used to club together a group of commands.
For ex.
alias dir ‘ls -sFC’
The above command on being executed would tell the shell that dir is another name for the command ls -sFC. In this way the user can simply assign an alias name to a command that could be difficult to remember. Now the user simply needs to use the specified alias name as given by him in place of the command for the same effect as the code. The user can check the number of aliases active by typing in the command from the shell:
uhunix% alias pwdls ‘pwd; ls -sFC’
4. What are the different ways of redirecting I/O?
Unix allows the user to redirect the output of an operation instead of sending it to your screen. The user has to make used of the redirection pointer (> & >>). The various ways of redirecting the output are as follows:
– To copy the contents of one file to another:
uhunix% cat file1 > file2
– To concatenate multiple files:
uhunix% cat file1 file2 file3 > newfile
– To append a file to another file:
uhunix% cat bottomfile >> appendeefile
– To redirect the input from instead of the keyboard to a file:
uhunix% mail username < letterfile
5. What are the use of pipes in unix?
Piping in unix allows the user to set the output of a specific operation as input to another. The piping of output from one file to another is performed by using the | symbol.
For ex.
uhunix% w | grep username
This command would allow the user to check if the specified user is logged in or not. When pipes are used in combination with redirection it gives the user the powerful ability to manipulate long operations in short steps.
For ex.
uhunix% ls | grep vi > vi.files
This command would result in the creation of a vi.files which would consist of the names of all the files in the current directory with a vi in their names.
6. What steps does the shell take after processing a command.
Once a command line is terminated by the key the shell continues to process the command line in passes.
-Parsing: The for the shell is to separate the commands line into words. For this it uses the references of spaces and delimiters specified by the user. Any consecutive spaces are replaced by a single space unless specified.
– Identifying variables: any word preceded by a $ sign are treated as variables unless specified so.
– Substitution: any command surrounded by back quotes are executed by the shell and replaces the command with the output given.
– Wild card recognition: After all the above tasks are completed the shell looks for wild card symbols in the command line.
7. What are the possible return values of kill()?
The kill() method on execution can yield the the following possible results:
– ‘0’ returned: means that the process exists with the specified PID. It will allow the user to send signals to this process.
– ‘-1’ returned, ‘errno==ESRCH’: This means that the process with the specified PID does not exist or some security enhancements is denying its existence.
– ‘-1’ returned, ‘errno==EPERM’: This means that the system would not permit the process to be killed.
The EPERM process is used to detect if a process exists or not. Any other error would specify that the process does not exist.
8. What is the super block in UNIX?
The file system of a system is described by a system block. Right at the beginning the super block is built when the file system is being created. The purpose of the super block is to contain the basic parameters of a system for ex the number of blocks and a count of the total number of files. There are two superblocks:
– The default super block is always present at a fix offset from the beginning of the system`s disk partition.
– The redundant super block is not usually referenced unless the default superblock is effected by some error or system crash.
The file system consists of files. Some files may be categorized as directories which in itself contains the pointers to files that are themselves directories or files. Every file has a descriptor known as the inode. The inode describes the ownership information of a file.
9. How can non printable characters be shown in unix?
A file sometimes may contain non printable characters such as line breaks for windows. Most of the editors used with unix do not display these non-printable characters. In case the user wants to view these characters he has to use the vim -b command to see and edit these characters. The -b flag used after vim is used to force the vim to use the binary mode.
For ex.
% cat foo.txt
Hello
World
% vim -b foo.txt
Hello^M
World^M
^M
10. What are the types of path names that can be used in unix?
In a file system which contains a hierarchy of directories the user has to specify a path to a file or directory to access it.
For ex.
/home/rohit/my/music/lyrics/text.notes
The above path refers to the file text.notes inside the folder which is inside the lyrics folder. The lyrics folder is contained by the music folder which in turn is contained by the my folder. The root directory is home.
There are tow types of paths that can be used in unix:
– Absolute Path name: This type of file name starts with the / symbol ie the root.
– Relative Path name: This path name begins from the current working directory where the user is. For the above stated path the relative directory would be:
lyrics/text.notes.
1. Write the list of commands that can used in relation to users in unix
The various commands that can be used to know about the user information in unix are as follows:
– id Show the active user id with login and group
– last Shows last logins on the system
– who Shows who is logged on the system
– groupadd admin Used to add group “admin” and user colin (Linux/Solaris)
– usermod -a -G <group> <user> Used to add existing user to group (Debian)
– usermod -A <user> <group> Used to add existing user to group (SuSE)
– adduser raj FreeBSD add user raj (interactive)
Theses commands can be executed at the shell and are distribution dependent.
12. How can file permissions be set in unix?
The command to change file permissions in unix is ‘chmod’ or also known as the change mode. With this command the user can set the permissions for users on how to access a file and what can be done with it. The syntax for changing file permissions is:
chmod [who] [+/-] [new-permissions] [file]
The who can be referred to:
u : This is for the user who owns the file.
g: This is meant to apply to the group which owns this file.
o: This is used to specify the permissions for the rest of the users.
a: This is used to specify everyone and anyone.
The +/- sign refer to if the permissions are to added or removed. The new permissions can be used in form of characters (r for read , x for execute) etc.
13. How is conditional execution be used in unix?
Conditional execution can be accomplished in unix with the use of control operators “and” and “or”. The and operator is represented as && and the syntax of their usage is :
command1 && command2
This implies that the command 2 will only be executed if the command 1 returns an exit status 0.
The or operator is represented by ||. The syntax of using the or operator is:
command1 || command2
This implies that command2 is executed if the command 1 returns a non-zero exit value.
Both the operators can be used together.
For ex.
command1 && command2 if exist status is zero || command3 if exit status is non-zero
This implies that command 2 will be executed if and only if command 1 executes or else command 3 will be executed.
14. Write a script to print numbers 5,4,3,2,1 using the while loops.
The algorithm for writing such a script would be:
– Begin: the value of i is set to 5.
– The while loop is started.
– Check of the value of i is zero. In case it is it will end the program and give the output.
– In case the i is not equal to zero the the value of i will be printed and the i`s value is decremented by 1.
The code:
i=5
while test $i != 0
do
echo “$i”
i=`expr $i – 1`
done
15. Write the algorithm and code to display the sum of all the digits of anumber?
The algorithm for the above requirement would be:
– The user is asked enter the number n.
– The sum and sd is set to zero at the beginning.
– Find the single digit in sd as n%10 which will give the left most digit.
– Construct the sum as sum = sum +sd
– The n is decremented by 1.
– If n is zero print the sum else repeat the loop.
The code for the above algorithm:
if [ $# -ne 1 ]
then
echo “Usage: $0 number”
echo ” I will find sum of all digit for given number”
echo ” For eg. $0 123, I will print 6 as sum of all digit (1+2+3)”
exit 1
fi
n=$1
sum=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo “Sum of digit for number is $sum”
16. Write the script to define a background process that will continuously print time at upper right corner of the screen.
The script to enable the user to work in the shell while defining a background program to display the time at the upper right screen is as follows:
echo
echo “Th clock”
echo “USe kill PID to dicontinue using this block”
echo “Press a key to continue. . .”
while :
do
ti=`date +”%r”`
echo -e -n “33[7s” #save current screen position & attributes
tput cup 0 69 # row 0 and column 69 is used to show clock
echo -n $ti # put clock on screen
echo -e -n “33[8u” #restore current screen position & attributes
#
#Delay fro 1 second
#
sleep 1
done
17. Write the script to display a number in reverse order? Also specify the algorithm.
The algorithm for the above program would be:
– Ask the user to enter the number.
– Set the values of rev and sd as 0.
– Find the single digit that is the left most digit.
– Construct the new number as rev * 10 + sd
– Now the n has to be decremented by 1.
– In case n is greater then zero repeat the above steps from third step or else print the number.
The code for the above algorithm is:
if [ $# -ne 1 ]
then
echo “Usage: $0 number”
echo “I will find reverse of given number”
echo “For eg. $0 123, I will print 321”
exit 1
fi
n=$1
rev=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
rev=`expr $rev \* 10 + $sd`
n=`expr $n / 10`
done
echo “Reverse number is $rev”
18. How can shell scripts be debugged in unix?
A user when programming in shell may face an error. The user can make use of the -v and -x option with sh or bash command to debug the shell script. The syntax format of using the option is :
sh option {shell-script-name} or
bash option {shell-script-name}
The option in the above syntax can be either -v or -x. The -v option prints the shell input lines as they are read. The -x option on the other hand expands a simple command and along with it displays the value of the ps4 variable followed by the command. It also shows the complete list of expanded arguments.
19. How can a users custom environment be made permanent?
Unix provides the user with the option to make their custom environment permanent so that the user does not have to redefine the variables of the environment again in order to define a new process. The user needs to place the set and setnv commands into certain special files to make the environment permanent. The files where the changes need to made are:
– .cshrc
– .login
– .logout
For ex the command to change the settings in the .cshrc file is:
uhunix% cat .cshrc
@(#)cshrc 1.11 89/11/29 SMI
umask 077
set notify
set history = 100
set path = ( /usr/ucb /bin /usr/bin /etc …
20. What are restricted shells ans what commands are banned in such shells?
Whenever the user runs / executes a script or a portion of it in the restricted mode it causes the shell to disable certain commands which would otherwise be available for the users. This can be considered to a security feature of unix which minimises the risk of damage while running a script. The commands or actions that are disabled during a restricted execution are as follows:
– The cd command to change the directory is restricted.
– The changing of the environment variables is not allowed.
– The user is not permitted to perform output redirection.
– To exit the restricted mode within a script is alos not allowed.
– The exec method cannot be invoked to substitute a different process for the shell.
21. What are the Unix system calls for I/O?
The following are the UNIX system calls for I/O: Open: to open a file. Syntax: open (pathname, flag, and mode)……
22. Devices
UNIX represents all devices as files. These files are located in the directory /dev……….
23. Brief about the directory representation in UNIX
A UNIX directory is representing a file that consists of a correspondence between file names and inodes……..
24. Discuss the mount and unmount system calls
The attaching of a file system to another file system is done by using mount system call……..
25. What are the process states in Unix?
UNIX has the following process states:…….
26. What is use of sed command?
Sed command reads from a standard input and places it into the pattern space…….
What is inode
A file in UNIX is given a unique number. This unique number is known as ‘inode’…….
28. What are links and symbolic links in UNIX file system?
Link is a utility program in UNIX which establishes a hard link from one directory to another directory……..
29. Explain fork() system call.
fork() system call is used to create processes. It returns a process id. After calling fork() system call……
30. What is a zombie?
A zombie is a process which is completed the execution and still available in the process table……..
31. How do you create special files like named pipes and device files?
Special files are created by the system call ‘mknod’. Upon using the following sequence of steps, a special file will be created…….
32. How do I use poll()?
poll() allows an event to wait on a file descriptor. A pointer is accepted by poll() to a list of ‘struct pollfd’………..
(1) What is a UNIX shell?
The UNIX shell is a program that serves as the interface between the user and the UNIX operating system. It is not part of the kernel, but communicates directly with the kernel. The shell translates the commands you type in to a format which the computer can understand. It is essentially a command line interpreter.
Details:
Visual representation of the UNIX operating system environment:

List of commonly used UNIX shells:
· The Bourne Shell (sh)
· The C Shell (csh or tsch)
· The Bourne Again Shell (bash)
· The Korn Shell (ksh)
(2) What needs to be done before you can run a shell script from the command line prompt?
You need to make the shell script executable using the UNIX chmod command.
Details:
This chmod command makes the shell script file “example1” executable for the user (owner) only:
$ chmod u+x example1
this syntax makes it executable for all (everyone):
$ chmod a+x example1
You can optionally use octal notation to set UNIX permissions using the chmod command (e.g., $ chmod 755 example1). This topic is beyond the scope of this article, but you can find more information by entering “unix file permissions chmod numeric notation” in your favorite search engine.
(3) How do you terminate a shell script if statement?
With fi, which is “if” spelled backwards.
Details:
The shell script example below uses an if statement to check if a file assigned to the variable myfile exists and is a regular file:
#!/bin/ksh
myfile=$1
if [ -f $myfile ]
then
echo “$myfile exists”
fi
exit 0
(See shell scripting interview question #6 below if you do not know what $1 in this example means.)
(4) What UNIX operating system command would you use to display the shell’s environment variables?
Running the “env” command will display the shell environment variables.
Details:
Sample env command output:
$ env
HISTFILE=/home/lfl/.history
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
SHELL=/bin/ksh
HOSTNAME=livefirelabs.com
USER=lfl
MAIL=/var/spool/mail/lfl
HOME=/home/lfl
HISTSIZE=1000
It would also be good to understand the purpose of the common shell environment variables that are listed in the env command output.
(5) What code would you use in a shell script to determine if a directory exists?
The UNIX test command with the -d option can be used to determine if a directory exists.
Details:
The following test command expression would be used to verify the existence of a specified directory, which is stored in the variable $mydir:
if [ -d $mydir ]
then
command(s)
fi
If the value stored in the variable mydir exists and is a directory file, the command(s) located between then and fi will be executed.
You can consult the test command’s man page (“$ man test”) to see what test command options are available for use.
(6) How do you access command line arguments from within a shell script?
Arguments passed from the command line to a shell script can be accessed within the shell script by using a $ (dollar sign) immediately followed with the argument’s numeric position on the command line.
Details:
For example, $1 would be used within a script to access the first argument passed from the command line, $2 the second, $3 the third and so on. Bonus: $0 contains the name of the script itself.
(7) How would you use AWK to extract the sixth field from a line of text containing colon (:) delimited fields that is stored in a variable called passwd_line?
echo $passwd_line | awk -F: ‘{ print $6 }’
Details:
Consider this line of text stored in the variable $passwd_line –
$ echo $passwd_line
mail:x:8:12:mail:/var/spool/mail:
$
(Background: The lines in the system passwd file are delimited (separated) by a colon (:)…$passwd_line contains a single line from the passwd file.)
The output of the echo command is piped to AWK. The -f option for the awk command informs awk of what the field separator is (colon in this example), and print $6 instructs awk to print the 6th field in the line.
(8) What does 2>&1 mean and when is it typically used?
The 2>&1 is typically used when running a command with its standard output redirected to a file. For example, consider:
command > file 2>&1
Anything that is sent to command’s standard output will be redirected to “file” in this example.
The 2 (from 2>&1) is the UNIX file descriptor used by standard error (stderr). Therefore, 2>&1 causes the shell to send anything headed to standard error to the same place messages to standard output (1) are sent…which is “file” in the above example.
To make this a little clearer, the > in between “command” and “file” in the example is equivalent to 1>.
(9) What are some ways to debug a shell script problem?
Although this is somewhat dependent on what the problem is, there are a few commonly used methods for debugging shell script problems.
One method, which is frequently used across all programming languages, is to insert some debug statements within the shell script to output information to help pinpoint where and why the problem is being introduced.
Another method specific to shell scripting is using “set -x” to enable debugging.
Details:
Consider the following shell script example (notice that “set -x” is commented out at this time):
#!/bin/ksh
#set -x
i=1
while [ $i -lt 6 ]
do
print “in loop iteration: $i”
((i+=1))
done
exit
This script will produce the following output:
$ ./script1
in loop iteration: 1
in loop iteration: 2
in loop iteration: 3
in loop iteration: 4
in loop iteration: 5
$
If we uncomment (remove the #) from the “set -x” line in the script, this is what is displayed when the script runs:
$ ./script1
+ i=1
+ [ 1 -lt 6 ]
+ print in loop iteration: 1
in loop iteration: 1
+ let i+=1
+ [ 2 -lt 6 ]
+ print in loop iteration: 2
in loop iteration: 2
+ let i+=1
+ [ 3 -lt 6 ]
+ print in loop iteration: 3
in loop iteration: 3
+ let i+=1
+ [ 4 -lt 6 ]
+ print in loop iteration: 4
in loop iteration: 4
+ let i+=1
+ [ 5 -lt 6 ]
+ print in loop iteration: 5
in loop iteration: 5
+ let i+=1
+ [ 6 -lt 6 ]
+ exit
$
In addition to displaying the intended output (“in loop iteration” lines), enabling debugging with “set -x” also shows each line of execution preceded by a plus sign (+).
Although this can become unwieldly for larger shell scripts, it should be obvious how useful this can be when debugging a shell script to identify the root cause of a problem.
(10) Within a UNIX shell scripting loop construct, what is the difference between the break and continue?
Using break within a shell scripting loop construct will cause the entire loop to terminate. A continue will cause the current iteration to terminate, but the loop will continue on the next iteration.
5) What is the chief difference between the –v and –x option s to set?
The –v option echoes each command before arguments and variables have been substituted for; the –x option echoes the commands after substitution has taken place.
1) What is UNIX?
It is a portable operating system that is designed for both efficient multi-tasking and mult-user functions. Its portability allows it to run on different hardware platforms. It was written is C and lets user do processing and control under a shell.
2) What are filters?
The term Filter is often used to refer to any program that can take input from standard input, perform some operation on that input, and write the results to standard output. A Filter is also any program that can be used between two other programs in a pipeline.
3) What is a typical syntax being followed when issuing commands in shell?
Typical command syntax under the UNIX shell follows the format:
Command [-argument] [-argument] [–argument] [file]
6) What is Kernel?
Kernel is the UNIX operating system. It is the master program that controls the computer’s resources, allotting them to different users and to different tasks. However, the kernel doesn’t
deal directly with a user. Instead, it starts up a separate, interactive program, called a shell, for each user when he/she logs on.
7) What is Shell?
A shell acts as an interface between the user and the system. As a command interpreter, the shell takes commands and sets them up for execution.
8 ) What are the key features of the Korn Shell?
– history mechanism with built-in editor that simulates emacs or vi
– built-in integer arithmetic
– string manipulation capabilities
– command aliasing
– arrays
– job control
9) What are some common shells and what are their indicators?
sh – Bourne shell
csh – C SHell
bash – Bourne Again Shell
tcsh – enhanced C Shell
zsh – Z SHell
ksh – Korn SHell
10) Differentiate multiuser from multitask.
Multiuser means that more than one person can use the computer at the same time. Multitask means that even a single user can have the computer work on more than one task or program at the same time.
11) What is command substitution?
Command substitution is one of the steps being performed every time commands are processed by the shell. Commands that are enclosed in backquotes are executed by the shell. This will then replace the standard output of the command and displayed on the command line.
12) What is a directory?
Every file is assigned to a directory. A directory is a specialized form of file that maintains a list of all files in it.
13) What is inode?
An inode is an entry created on a section of the disk set aside for a file system. The inode contains nearly all there is to know about a file, which includes the location on the disk where the file starts, the size of the file, when the file was last used, when the file was last changed, what the various read, write and execute permissions are, who owns the file, and other information.
14) You have a file called tonky in the directory honky. Later you add new material to tonky. What changes take place in the directory, inode, and file?
The directory entry is unchanged, since the name and inode number remain unchanged. In the inode file, the file size, time of last access, and time of last modification are updated. In the file itself, the new material is added.
15) Describe file systems in UNIX
Understanding file systems in UNIX has to do with knowing how files and inodes are stored on a system. What happens is that a disk or portion of a disk is set aside to store files and the inode entries. The entire functional unit is referred to as a file system.
16) Differentiate relative path from absolute path.
Relative path refers to the path relative to the current path. Absolute path, on the other hand, refers to the exact path as referenced from the root directory.
17) Explain the importance of directories in a UNIX system
Files in a directory can actually be a directory itself; it would be called a subdirectory of the original. This capability makes it possible to develop a tree-like structure of directories and files, which is crucial in maintaining an organizational scheme.
18) Briefly describe the Shell’s responsibilities
– program execution
– variable and file name substitution
– I/O redirection
– pipeline hookup
– environment control
– interpreted programming language
19) What are shell variables?
Shell variables are a combination of a name ( identifier), and an assigned value, which exist within the shell. These variables may have default values, or whose values can be manually set using the appropriate assignment command. Examples of shell variable are PATH, TERM and HOME.
20) What are the differences among a system call, a library function, and a UNIX command?
A system call is part of the programming for the kernel. A library function is a program that is not part of the kernel but which is available to users of the system. UNIX commands, however, are stand-alone programs; they may incorporate both system calls and library functions in their programming.
21) What is Bash Shell?
It is a free shell designed to work on the UNIX system. Being the default shell for most UNIX-based systems, it combines features that are available both in the C and Korn Shell.
22) Enumerate some of the most commonly used network commands in UNIX
– telnet – used for remote login
– ping – an echo request for testing connectivity
– su – user switching command
– ftp – file transfer protocol used for copying files
– finger – information gathering command
23) Differentiate cmp command from diff command.
The cmp command is used mainly to compare two files byte by byte, after which the first encountered mismatch is shown. On the other hand, the diff command is used to indicate the changes that is to be made in order to make the two files identical to each other.
24) What is the use of -l when listing a directory?
-l, which is normally used in listing command like ls, is used to show files in a long format, one file per line. Long format refers to additional information that is associated with the file, such as ownership, permissions, data and filesize.
25) What is piping?
Piping, represented by the pipe character “|”, is used to combine two or more commands together. The output of the first command serves as input the next command, and so on.
26) What is a superuser?
A superuser is a special type user who has open access to all files and commands on a system. Note that the superuser’s login is usually root, and is protected by a so-called root password.
27) How do you determine and set the path in UNIX?
Each time you enter a command, a variable named PATH or path will define in which directory the shell will search for that command. In cases wherein an error message was returned, the reason maybe that the command was not in your path, or that the command itself does not exist. You can also manually set the path using the “set path = [directory path]” command.
28) Is it possible to see information about a process while it is being executed?
Every process is uniquely identified by a process identifier. It is possible to view details and status regarding a process by using the ps command.
29) What is the standard convention being followed when naming files in UNIX?
One important rule when naming files is that characters that have special meaning are not allowed, such as * / & and %. A directory, being a special type of file, follows the same naming convention as that of files. Letters and numbers are used, along with characters like underscore and dot characters.
30) Why is it that it is not advisable to use root as the default login?
The root account is very important, and with abusive usage, can easily lead to system damage. That’s because safeguards that normally apply to user accounts are not applicable to the root account.
31) What is the use of the tee command?
The tee command does two things: one is to get data from the standard input and send it to standard output; the second is that it redirects a copy of that input data into a file that was specified.
32) Differentiate cat command from more command.
When using the cat command to display file contents, large data that does not fit on the screen would scroll off without pausing, therefore making it difficult to view. On the other hand, using the more command is more appropriate in such cases because it will display file contents one screen page at a time.
33) What is parsing?
Parsing is the process of breaking up of a command line into words. This is made possible by using delimiters and spaces. In the event that tabs or multiple spaces are part of the command, these are eventually replaced by a single space.
34) What is pid?
Pid is short for Process ID. It is used primarily to identify every process that runs on the UNIX system, whether it runs on the foreground or runs at the background. Every pid is considered unique.
35) How does the system know where one command ends and another begins?
Normally, the newline character, which is generated by the ENTER or RETURN key, acts as the signpost. However, the semicolon and the ampersand characters can also serve as command terminators.
37) What is wild-card interpretation?
When a command line contains wild-card characters such as ‘*’ or ‘?’, these are replaced by the shell with a sorted list of files whose pattern matches the input command. Wild-card characters are used to setup a list of files for processing, instead of having it specified one at a time.
39) How do you switch from any user type to a super user type?
In order to switch from any user type to a superuser, you use the su command. However, you will be asked to key in the correct superuser password before full access privileges are granted to you.
40) What would be the effect of changing the value of PATH to:
.:/usr/della/bin: /bin: /usr/bin
This would cause the shell to look in the /usr/della/bin directory after looking in the current directory and before looking in the /bin directory when searching for a command file.
41) Write a command that will display files in the current directory, in a colored, long format.
Answer: ls -l –color
46) What does this command do? cat food 1 > kitty
Answer: it redirects the output of cat food into the file kitty; the command is the same as:
cat food > kitty
47) What is wrong with this interactive shell script?
echo What month is this?
read $month
echo $month is as good a month as any.
Answer: Initially, the question mark should be escaped (\?) so that it is not interpreted as a shell metacharacter. Second, it should be read month, not read $month.
Is there a way to erase all files in the current directory, including all its sub-directories, using only one command?
Yes, that is possible. Use “rm –r *” for this purpose. The rm command is for deleting files. The –r option will erase directories and subdirectories, including files within. The asterisk represents all entries.
What is the output of this command? $who | sort –logfile > newfile
In this command, the output from the command “who” becomes the input to the “sort” command. At the same time, “sort” opens logfile, arranges it together with the output from the command “who”, and places the final sorted output to the file newfile.
Write a command that will find all text files in a directory such that it does not contain the word “amazing” in any form (that is, it must include the words Amazing, AMAZING, or aMAZINg)
grep –vi amazing *.txt
Write a command that will output the sorted contents of a file named IN.TXT and place the output in another file named OUT.TXT, while at the same time excluding duplicate entries.
What command will change your prompt to MYPROMPT: ?
Write a shell script that requests the user’s age and then echoes it, along with some suitable comment.
(sample output: 17:34:51 PDT Sun 12 Feb 2012)
Write a script that will show the following as output:
Give me a U!
U!
Give ma a N!
N!
Give me a I!
I!
Give me a X!
X!
Answer:
for i in U N I X
do
1. What is UNIX?
It may sound as a trivial question, but you’d be amazed just how many people fail to give a clear definition, because simply stating that “Unix is an operating system” is not likely to impress the interviewer. The correct answer should look something like this:
UNIX is a multi-user multitasking-optimized operating system that can run on various hardware platforms.
2. How is UNIX different from Linux?
Linux is basically an open-source clone of UNIX so, while a lot of similarities between the two operating systems do exist, there are also a lot of differences. The main advantage of UNIX is that all the core components of the operating system come from the same vendor, which means greater overall stability and better software support from vendors. UNIX releases are more stable and consistent than Linux releases, making UNIX the better choice for enterprise use.
3. What is a kernel?
The kernel is the program that acts as a middle layer between software and hardware. When a program requires access to certain resources or processing power, the kernel is responsible for sending the correct signals to the CPU and managing all other running programs and services so that the resources are correctly allocated and no conflicts occur.
4. What is the difference between multi-user and multi-tasking?
Multi-tasking means that a user can run multiple tasks simultaneously on a single machine, whereas multi-user means that multiple users can operate simultaneously on a machine.
5. What is a UNIX shell?
A UNIX shell is an interface that acts as a command interpreter, translating user input into machine-understandable language and then passing it to the kernel for execution.
Once you ace the basic stuff, the interviewer is most likely to move on to some questions involving commands. Keep in mind that his role is not to see how good you are at memorizing commands, but rather how well you understand what those commands do and determine your ability to choose the right command in a certain situation. In order to successfully pass this part of the interview, make sure you formulate your answers as clear and as detailed as possible. You should consider taking a course to improve your communication skills prior to the interview.
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:
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!”.
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
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
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.
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.
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.
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.
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”
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.
File permissions in UNIX Linux with Example >> Unix Tutorial
Whenever we execute ls command in UNIX you might have observed that it list file name with lot of details e.g.
stock_options:~/test ls -lrt
total 8.0K
-rw-r–r– 1 stock_options Domain Users 1.1K Jul 15 11:05 sample
If you focus on first column you will see the file permissions as “-rw-r–r–“ this is made of three parts user, group and others. User part is permission relate to user logged in, group is for all the members of group and others is for all others. also each part is made of three permissions read, write and execute so “rw-” means only “read and write” permission and “r–” means read only permission. So if you look permission of example file it has read and writes access for user, read only access for groups and others. Now by using chmod command in UNIX we can change the permissions or any file or directory in UNIX or Linux. Another important point to remember is that we need execute permission in a directory to go inside a directory; you can not go into directory which has just read and write permission.
File permission in Numeric format
File permission can also be expressed in numeric format usually octal number system is used to express file permissions
0 – no permissions
1 – execute only
2 – write only
3 – write and execute
4 – read only
5 – read and execute
6 – read and write
7 – read, write and execute
Symbolic format of file permissions in UNIX
Symbolic format is another format of denoting UNIX file permissions. In symbolic format we have special notations for user, group and others as well as to denote read, write and execute permissions as shown below and by using these symbols you can set any permissions on file in Linux.
Reference Class Description
u user the owner of the file
g group users who are members of the file’s group
o others users who are not the owner of the file or members of the group
a all all three of the above, is the same as ugo
r read read a file or list a directory’s contents
w write write to a file or directory
x execute execute a file or recurse a directory tree
Default permissions on files and directory in UNIX
Whenevera process creates a file it uses default permission 666 for file and 777 for directory. You can use “umask” command to further restrict the permissions of file or directory at creation time. umask value is used to eliminate the permissions specified by umask. for example a common umask values is “022” which makes file read and write permission for owner or group but read only for group members and other and in case of directory it makes directory searchable with execute permissions for all user, group and others because you can not go inside a directory in UNIX or Linux if you don’t have execute permissions on that. Let’s see an example how we arrived to this file permissions:
Default permission of file — 666
usmak — 022
———————————-
Final permissions on file — 644 (which is 110 100 100 i.e. rw- r– r–) read and write for user and read only for group and others
Default permission of directory — 777
umask — 022
—————————————-
Final permission of file — 755 (which is 111 101 101 i.e. rwx r-x r-x) read, write and execute for user (owner) and read+execute for group members and others.
How to change file and directory permission in UNIX
You can use chmod command to change permissions of any file or directory in UNIX or Linux. Chmod command stands for change mode for example from read only mode to writable. Let’s see and example of creating a read only file and then granting it full access in UNIX or Linux.
stock_options:~/test touch stock_trading_systems
stock_options:~/test ls -lrt
total 8.0K
-rw-r–r– 1 stock_options Domain Users 0 Nov 15 11:42 stock_trading_systems
stock_options:~/test chmod 400 stock_trading_systems
stock_options:~/test ls -lrt
total 8.0K
-r——– 1 stock_options Domain Users 0 Nov 15 11:42 stock_trading_systems
stock_options:~/test vim stock_trading_systems
stock_options:~/test chmod 777 stock_trading_systems
stock_options:~/test ls -lrt
total 8.0K
-rwxrwxrwx 1 stock_options Domain Users 0 Nov 15 11:42 stock_trading_systems*
You can see file permission changed to rwxrwxrwx , if you have noticed there is also a * mark at the end of file name “stock_trading_systems*” that shows that this is an executable file. To enable this option you can setup an alias “ls=ls –F” , -F displays that option.
That’s all on File permission on UNIX and Linux OS for now. Please add any important point related to file permissions which are not discussed here. In Summary having good understanding of file and directory permissions in UNIX and how to change file permissions is key for working productively in Linux.
- ls — lists your files
ls -l — lists your files in ‘long format’, which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified.
ls -a — lists all files, including the ones whose filenames begin in a dot, which you do not always want to see.
There are many more options, for example to list files by size, by date, recursively etc. - more filename — shows the first part of a file, just as much as will fit on one screen. Just hit the space bar to see more or q to quit. You can use /pattern to search for a pattern.
- mv filename1 filename2 — moves a file (i.e. gives it a different name, or moves it into a different directory (see below)
- cp filename1 filename2 — copies a file
- rm filename — removes a file. It is wise to use the option rm -i, which will ask you for confirmation before actually deleting anything. You can make this your default by making an alias in your .cshrc file.
- diff filename1 filename2 — compares files, and shows where they differ
- wc filename — tells you how many lines, words, and characters there are in a file
- chmod options filename — lets you change the read, write, and execute permissions on your files. The default is that only you can look at them and change them, but you may sometimes want to change these permissions. For example, chmod o+r filename will make the file readable for everyone, and chmod o-r filename will make it unreadable for others again. Note that for someone to be able to actually look at the file the directories it is in need to be at least executable.
- File Compression
- gzip filename — compresses files, so that they take up much less space. Usually text files compress to about half their original size, but it depends very much on the size of the file and the nature of the contents. There are other tools for this purpose, too (e.g. compress), but gzip usually gives the highest compression rate. Gzip produces files with the ending ‘.gz’ appended to the original filename.
- gunzip filename — uncompresses files compressed by gzip.
- gzcat filename — lets you look at a gzipped file without actually having to gunzip it (same as gunzip -c). You can even print it directly, using gzcat filename | lpr
- printing
- lpr filename — print. Use the -P option to specify the printer name if you want to use a printer other than your default printer. For example, if you want to print double-sided, use ‘lpr -Pvalkyr-d’, or if you’re at CSLI, you may want to use ‘lpr -Pcord115-d’. See ‘help printers’ for more information about printers and their locations.
DIRECTORIES
Directories, like folders on a Macintosh, are used to group files together in a hierarchical structure.
- mkdir dirname — make a new directory
- cd dirname — change directory. You basically ‘go’ to another directory, and you will see the files in that directory when you do ‘ls’. You always start out in your ‘home directory’, and you can get back there by typing ‘cd’ without arguments. ‘cd ..’ will get you one level up from your current position. You don’t have to walk along step by step – you can make big leaps or avoid walking around by specifying pathnames.
- pwd — tells you where you currently are.
links:
——-
http://www.folkstalk.com/p/unix-commands-list.html
Read more: http://javarevisited.blogspot.com/2011/11/file-permissions-in-unix-linux-example.html#ixzz3gkrYzgkC
FTP
The FTP (file transfer program) utility is used to transfer files between a local machine and remote network machine Using the File Transfer protocol. In simple terms it transfers / copies files between two computers. You can transfer files between unix systems and also non-unix systems like windows operating system using FTP.
The FTP command is simple to use and easy to learn. Let see useful examples of FTP command in detail.
FTP Command Examples:
If you are using windows operating system, open the command prompt and practice the below FTP commands. If you are using unix or linux operating systems, just simply type the ftp command on the terminal.
1. Connecting to Remote Host
First you need to connect to a remote host before doing any operations. You can use any one of the following methods to connect to a remote host. First method is
Once the ftp connects to the remote server name, it will prompt you to enter the user name and password. After successful login, your terminal or prompt changes to “ftp>”.
Another method is to use the open option with ftp command. This is shown below:
If the ftp command fails to connect to the remote server, then you will get the below error:
2. Copy file from remote machine to local machine.
The get option is used to download or transfer a file from the remote system to the local system.
This will download the specified file (windows-cleveland.bat) from the remote systems current directory.
3. Copying multiple files from remote machine to local machine.
You can use the mget to transfer multiple files from the remote host to local host.
This will download all the png images to the local machine.
4. Transferring file from local server to remote server
The put option is used to copy the file from the local host to the remote host.
This command puts the rpm file into the remote machine.
5. Transferring multiple files to the remote server.
You can use the mput option to transfer more than one file from local system to the remote system.
6. Executing commands in remote machine.
After connecting to the remote network machine using the ftp, you can run commands like ls to list the files, cd to change directory and many more.
This will list the files and directories in the remote machines current directory.
7. Executing commands in local machine.
Once you have connected to the remote host, to run the commands on local machine you need to exit from the ftp connection. Instead of this, there is a way to run commands on local host without exiting from the ftp connection. Use the ! symbol before the command you want to run.
Now this will list the files in the local machines current directory.
8. Changing the file transferring mode.
You can change the file transfer modes to ascii and binary modes. Use the below commands to change the mode.
9. Deleting files on remote machine
You can use the delete or mdelete to remove a single file or multiple files in the remote machine.
10. Disconnecting from ftp connection.
Use the quit command to close the ftp connection.
11. Using FTP command in batch scripts
The following script reads the instructions from the dat file and executes them on the remote machine.
The contents of the instructions.dat file is
12. Getting the help about ftp command.
To know more about the ftp command, just type the help on the prompt. It will display the options/commands that you can use with ftp command.