..MindWrite..

Posts Tagged ‘unix’

Remove first word using sed

Posted by guptaradhesh on April 2, 2014

cat filename | sed -e 's/^ *[^ ]* //'

Rating: 4.5

Posted in uncategorized | Tagged: , | Leave a Comment »

Output redirection: > /dev/null 2>&1

Posted by guptaradhesh on July 27, 2012

One would frequently notice this training redirect construct in many Unix commands – all it means is, ALL output from this command should be pushed into /dev/null.

> /dev/null 2>&1

/dev/null is a place where we can dump anything we don’t want to use. Since, 1 is stdout and 2 is stderr: 2>&1 can be interpreted as redirect stderr to stdout.  Thus, Error messages which go to stderr are redirected to stdout which is being redirected to dev/null. Therefore, no output at all.

 

Posted in tech, unix | Tagged: , , , , , | Leave a Comment »

Number of occurrences of a string in a file using vi editor

Posted by guptaradhesh on June 20, 2012

:%s/string/&/gn

It counts the number of substitutions without actually performing the substitutions.

Posted in tech, unix | Tagged: , , , | Leave a Comment »

remove front spaces from a string

Posted by guptaradhesh on November 8, 2010

a simple sed command to do the same is as:

sed -e 's/^ *//' data

Posted in tech, unix | Tagged: , , | 1 Comment »

Unix mtime, ctime, atime

Posted by guptaradhesh on November 1, 2010

ctime: is the inode change time. When does the inode change, when you of course update a file, but also when you do things like changing the permissions of the file but not necessarily its contents. We can view the ctime with the ls -lc command.

atime: stands for access time which is when the file was last read. Displaying the contents of a file or executing a shell script will update a file’s atime. We can view the atime with the ls -lu command.

mtime: is the modification time, so if you change the contents of the file, this time is updated. Keep in mind when you change the contents of a file and and save it, the inode gets updated as well for that file, so ctime always changes too. We can view this with ls -l.

Therefore, when mtime changes, ctime will always change.

Posted in tech, unix | Tagged: , , , , | Leave a Comment »

some arbit workaround with Unix Shell

Posted by guptaradhesh on October 25, 2010

>> finding if a directory exists or not:

to check if a directory ‘tempDir’ exists in your home directory, the shell command would be,

$ [ ! -d ~/tempDir ] && echo 'Directory tempDir not found'
OR
$ [ -d ~/tempDir ] && echo 'Directory found' || echo 'Directory tempDir not found'

Similarly, to check if a file exists, we use -f parameter in the above command.

Parameters like -r, -w, -x are also available to check if the file exists and is readable, writable or executable resp.

>> Adding a directory to the path of user or all users:

PATH=$PATH:/path/of/my/scripts

"export PATH" must be added to ensure that any programs called 
by bash have the altered $PATH variable.

- To add a directory to the path of a single user, 
place the lines in that user's .bash_profile file.

- For all users globally, set a path in /etc/profile.

Posted in tech, unix | Tagged: | 1 Comment »

Special parameters in UNIX Shell

Posted by guptaradhesh on October 24, 2010

$# Number of positional parameters

$? Exit value of last executed command

$$ Process number of current process

$! Process number of background process

$* All arguments on command line

“$@” All arguments on command line ,  individually quoted “$1” “$2” …

$-    Options currently in effect

Posted in tech | Tagged: , , , | Leave a Comment »

What exactly does this command does: echo $?

Posted by guptaradhesh on October 24, 2010

It displays the exit status of previous unix command.

Every Linux command executed by the shell script or user, has an exit status. The exit status is an integer number. 0 exit status  means the command was successful without any errors. A non-zero (1-255 values) exit status means command was failure.

 

Posted in tech | Tagged: , , | Leave a Comment »