Showing posts with label Shell. Show all posts
Showing posts with label Shell. Show all posts

Tuesday 7 January 2014

Count lines, words & characters of a file in Unix

On Unix and Linux machines, if you want to find out the word count, number of lines and the total number of characters used in a certain file, use the wc command in the following way.

Eg:

cat build.xml | wc

The output will be like 

9      26     297

Here, 9 is the number of lines, 26 is the number of words and 297 is the number of characters.

Monday 30 December 2013

Kill a Process in Unix

In UNIX and OSX, you can kill a process from the command line using the "kill" command.

kill -9 PID

The PID (Process ID) can be found by using ps -ef command.

Eg:

ps -ef | grep jrockit

The output will show the Process ID. Let's say , for instance it's 4519.

Then you can kill the process by using :

kill -9 4519

-9 is basically a signal (SIGKILL) to be used with the kill command. The default signal is -15 i.e SIGTERM which is milder signal to terminate a process.

More detailed information about kill signals can be found here.

Technorati cliam token USM62NJ7A438

Saturday 28 December 2013

Check if a directory exists in Unix

In Unix, you can check if a certain directory exists or not using -d

Eg:

if [-d $directory_name]
then
       execute_some_commands
fi

In the variable $directory_name you specify the name of the directory. If the directory exists, then the if loop executes and the commands would be executed. 

Monday 23 December 2013

Pass arguments to build file - Apache Ant

In Apache Ant, we can pass arguments to the build file using the switch -D. We can define a value for a property from the command line using -Dname=value

Eg:

ant build.xml -Dx=5

Sunday 22 December 2013

Bourne shell (bash) vs Korn shell (ksh)

There are a few notable differences between Bash shell and Korn shell as follows:





DifferenceBashKorn
Redo previous commandArrow Up the letter 'r'
Source fileWe can use the keyword 'source' as well as dot to source a file Eg: source file or .fileWe can just use the dot to source it Eg: .file

Friday 20 December 2013

Create file without editing in Unix

The best way to create a new file without editing or opening it is using touch command.

Eg: 

touch test1.txt  => This will create a new file test1.txt

Touch can also be used to update an existing file's timestamp to current time. The Date modified timestamp will indicate current time

Note that if you want to touch a file only and only if it exists, use the -c switch

Eg:

touch -c log.txt

(If the log.txt exists previously, only then the access and modification timestamps would update. If log.txt does not exists, the above command will do nothing i.e a new file will not be created)

Remove directories - Unix

The command to delete or remove directories is rmdir

Note that rmdir can delete only empty directories. You CANNOT delete non-empty directories using rmdir.

In order to remove non-empty directories use the rm -rf command instead. Use the -r switch with caution since it deletes the inner contents of a directory recursively.

Disk Space - Unix

In Unix, in order to find the disk space statistics of mounted volumes, use the following command:

df -h

The output will show the size of volume, the used space and the available free space in the Mb and Gb format.

Eg:

Filesystem        Size      Used    Avail   Capacity  Mounted on
/dev/disk0s2     465Gi   275Gi  190Gi     60%            /

Thursday 19 December 2013

Process Status - Unix

The ps command is quite a useful command in Unix to find out information related to processes.

There are a lot of switches for the ps command for doing a bunch of things.

Most frequently used switch is -ef  This can be used to to list all the running processes alongwith their PID's (process ID)

  • If you want to find out all processes of a specific user then use the foll.


ps -u <username>

Eg: ps -u root


  • If you want to find the PID of a certain process, searching by its process name then use the following command: 
ps -ef | grep <process name string>

Eg: ps -ef | grep jrockit  (Narrow down the output of ps command by piping it to a grep statement)

System Uptime - Unix

In Unix, you can find how long your system was up by using the uptime command

You could get the output like:

15:34  up 22 days, 29 mins, 4 users, load averages: 0.78 0.82 0.77

Hard Link vs Soft Link - Unix

As you may be aware, in Unix we can create Links to point to a certain program or a file.

We create a link using ln command. We can create a symbolic link (also known as symlink) using the -s switch.

Eg:

ln -s /path to file1/file1.rtf /path to file2/file2.rtf

Now there are 2 types of links : a Hard link and a Soft link

Basically both types point to a certain file or a program. However one key difference is when the original file name is changed or modified.

If the name of original file is modified or renamed or if the original program is deleted from its location then:

a soft link is broken BUT a hard link does NOT break

Secondly, a Hard link cannot point to directories while a Soft link can link directories.


Wednesday 18 December 2013

Exit status of previous command - Unix

In Unix, one is often asked to find out the exit status of previous command i.e whether it was executed successfully or not.

In order to find out, type the foll. in terminal :

echo $?

If the output is 0 then it means the previous command got executed successfully.

If the previous command failed then the output of echo $? would be 127.

Tail the Logs - Unix

Tail is just another basic command in Unix where you can 'tail' a certain file to view its contents Bottoms-up.

Usage:

tail -lines <file-name>

Eg:

To view last ten lines of a file log.txt. Note the -n switch

tail -n 10 log.txt

To view the live results of a logfile use the -f switch

tail -f logfile.txt

Tuesday 17 December 2013

Unix - Remove range of lines from a file

In Unix, we have the ability to remove certain lines from a file by using sed. Sed as you may be aware, is a short for stream editor. Using Sed, we can specify the range of lines which we want to delete.

Usage :

sed -i <beginning line no> <ending line no> <file-name>

Eg:

For deleting lines from 3 to 9 of a file Test.txt, use the following command

sed -i '3,9 d' Test.txt

Monday 16 December 2013

Rename file in Unix

People new to Unix or linux environment often wonder how to rename a file from the command line.

Just remember, the command to rename is mv

Usage:

mv old_filename new_filename

Eg:

mv file1.txt file1_new.txt


Wednesday 11 December 2013

Shell Scripting - Read User Input

Inorder to accept user defined input in a shell script, make use of the read command. Try a simple example in Terminal.

Eg:

#!/usr/bin/bash
echo "Please enter your name"
read name
echo "Welcome to shell scripting $name"

Make sure the bash interpreter location is correctly defined in the shebang statement. Else you would see the error like - bad interpreter: No such file or directory

Tuesday 10 December 2013

Shell Scripting : The Shebang

People who are new to shell scripting or are new to it often hear the term "shebang". If you're one of them, then you might have seen the shebang symbol in the topmost line in any shell script but maybe you're not sure what it means or what's its significance.

The symbol #!  i.e shebang is part of the topmost statement of a shell script which is used to tell which interpreter you'll be using.

Eg:

#!/usr/bin/bash  (Bash interpreter)

#!/usr/local/perl   (invokes the Perl interpreter)

#!//usr/bin/ksh (Korn shell interpreter)

#!/usr/bin/awk   (use the AWK interpreter)
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker