Friday, 8 August 2014

Unix Interview Prep Tutorial- Part 1: Commonly used Unix Commands, usage and purpose:

Unix Interview Prep Tutorial- Part 1:Commonly used Unix Commands, usage and purpose:


Below are list of unix commands that you might need for daily use. This is more focused for beginners and intermediate unix users.The purpose of the command is mentioned in comments between /* and */.

PWD: command is used to show the present working directory.
Usage: pwd

ls: command is used to get list of all files in the directory
Usage: ls -l
       ls -ltr

wc: is used to get word count from a file.
Usage: wc inputfile.txt /* word count from a file */
       wc -l inputfile.txt /*line counts from a file */
       ls -l| wc -l /* to get number of files in a directory */

cd: commands is used to change the working directory.
Usage: cd pathtodirectory
       cd /usr/bin
       cd .. /* go one directory up */

mkdir: is used to create a directory:
Usage: mkdir <dirname>

chmod: is used to change the permissions on a directory. After you create a directory or file you might have to change the permission on the directory.
Usage: chmod 777 filename /* provide read/write/execute permission to all the users */
       chmod 555 filename /Previde read/execute permission to all the users */

More: command is used to shown the contents of a file.
Usage:
more filename.txt

Head/ Tail: Head command is used to show the first few lines of a file and tail command is used to show the last few lines. You can also specify how many lines to be displayed;
Usage: head inputfilename.txt
       head -100 inputfilename.txt /* show first 100 lines of inputfilename.xt */
       tail inputfilename.txt
       tail -100 inputfilename.txt /* show last 100 lines of inputfilename.xt */

Grep: command is used search for a pattern string in a file:
Usage:
grep "*2014" inputfile.txt  /*search for 2014 in your input file*/
grep "searchstring" inputfile.txt
grep 'scp' *  /*search scp in all the files in the current directory*/

        
gzip command is used to zip a file to .gz format or unzip a file.
Usage:
gzip <filename> /* To create a .gz file*/
gzip -d <filename> /* to uncompress a .gz file

unzip command is used to create .zip files in unix.
Usage: unzip -l <file.zip> /* list all the files without uncompressing it */
       unzip <file.zip>   /* to unzip a file i.e to decompress a file to .zip format */
       unzip -c  <file.txt> /* to zip a file i.e to compress a file to .zip format */

touch command is used to create a file.
usage: touch filename.txt

rm command is used to remove a file. rmdir command is used to remove a directory.
usage: rm filename
       rmdir directoryname
       rm -f filename  /*Does not display error message. Good to use when removing files that might not exist through an automated script */.

sftp command is used to sftp to a sftp server
usage: sftp -o Port=22 username@serveraddress

date command is used to display the sytem date
usage: date
       date +'%Y-%m-%d %H:%M:%S'

echo command is used to print a text on the terminal
usage: echo "print something"
       echo "write to a file" > out.txt /* write the string to a file */

export command is used to export environment variables
Usage:
export SYSNAME=UNIX

sed command is used to replace a content of a file or extract contents of a file
Usage:
sed "s/2014/2015/g" inputfile.txt > ./outputfile.txt /* replaces 2014 with 2015 in the inputfile.txt and writes it to outputfile.txt */
sed -n '3p' ./inputfile.txt /* read third line of the inputfile.txt file */

Cut command is used to extract sub string
Usage: cut -c 3-7 filename /* to output 3rd to 7th character of each line */
cut -d ":" -f 3- filename /* to extract 3rd fiedd field of each line using the : as the field delimiter */

diff command is used to compare two files:
Usage: diff file1 file2

du command is used to find about the disk space usage
Usag: du
du -sk

df command is used to find out the available disk space.
Usage: df
df -k


Also check:
http://dwbitechguru.blogspot.ca/2014/09/unix-tutorial-part2-simple-unix-scripts.html