Tuesday, September 11, 2012

Fifth session of Some of basic UNIX commands that are useful very frequently

Today we will discuss about some more useful commands that are used frequently when you use UNIX/LINUX based system.

Whenever you are running your application in UNIX/LINUX based server, then you may get to view log files for debugging purpose. In that case below command will be useful.

more filename

Above command will display content in the file named 'filename' that fits according to screen. If you want next content in that file to display, then you can simply press 'space' to scroll file content. If you want to quit to view the file then you can simply press 'q' to quit that view. In the displayed content of file if you want to search for some string, then you can use '/pattern' for searching that string.

For example, above file name displayed below content:

Hi My Name is John.

And in that if you want to search for 'John' then you can simply press '/John' and enter will highlight the word 'John'. If there are more 'John's in the displayed content then all will be highlighted. You can traverse one by one.

There is one more way to view file content but not whole content instead just last few lines in that file, then you can use below command.

tail -f filename

Above command will display last few lines of file according to screen fit. If that file is getting modified then it will display accordingly.

For example you are viewing 'catalina.log' file in the tomcat server, then it will be modified frequently based up on the hits to your application. In that case this command will display last few lines accordingly. 

If you want to rename one file to another, then you can use below command.

mv filename1 filename2

Above command renames 'filename1' to 'filename2' . I.e. moves content from 'filename1' to 'filename2'. If you want to move 'filename1' to different directory then also you can use same command but in different manner.

mv filename1 dir2

Above command will move 'filename1'from current directory to 'dir2'. If your file is not present in current directory, then you need to give 'filename1'with full path.

As specified in my previous post, you can get more uses of this command by hitting 'man mv' in your terminal.

Like 'mv' command that we use for renaming or moving files, there is one more command that you may used very frequently in windows if you are get familiar with command prompt. i.e. copy command. Below is its usage:

cp filename1 filename2

Above command will copy 'filename1'into 'filename2'. As specified above with 'mv' command, you can use all similar patterns with this command.

To delete a file you can use below command.

rm filename

Above command will removes file named 'filename' from the system. You can use option ' -i  ' for asking confirmation to delete. 

For all these three commands, you can use patterns for handling set of files. Below is one small example.

mv /dir1/* /dir2

Above command will moves all files present in '/dir1' to '/dir2'. Here '*' is one patten that we discussed in our previous post.

Will discuss some more useful commands in next post...

No comments: