Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

Friday, July 30, 2010

Unix File Redirection: How to Overwrite an Existing File

I have set my tc shell settings so that when ever I try to redirect command output to an existing file, it gives me an warning. The reason for why I am using this option is that while redirecting to a file, you loose all the contents of the file. If you make a mistake while typing the file name, this can lead to very dangerous outcome. Because that file may contain data whose backup you don't have.

But, this option has some drawbacks also. That is when you want to rewrite a file by redirect to it. The problem is every time you try to do this, you are given an warning. What you have to do is to remove the file first, then redirect to that file. Two steps become involved which eats many time. But, today I have found that there is a way to perform these steps by just one. It is adding the "!" sign at the end of ">" sign. Here is the example.

% echo hello world! > test.txt
% cat test.txt
hello world!
% echo again hello world > test.txt
test.txt: File exists.
% echo again hello world >! test.txt
% cat test.txt
again hello world!


In this example, I first redirect the output of the command "echo" to file "test.txt". As "test.txt" file does not exist, shell creates a new file and writes the output to this file. Next, I redirect the output of another "echo" command to this same file and this time, shell gives me an warning. Now, if I do this by adding the "!" sign after ">", shell overwrites the existing file and writes the new output result to that file. This trick will surely save a lot of my time.

Tuesday, June 15, 2010

GNU Screen

It has been two years since I switched to linux from windows. And ever since the first day, I liked linux and liking linux more day by day. I use mainly vi to edit files (especially program files) on the terminal. Sometimes, I feel the need of several terminals. Present linux desktops provide tab based terminal interface which served my need for a while. But switching between the tabs takes times and not that convenient. I wasn't satisfied with it. I was hoping for some tools to give me an interface like vi where I can split to multiple windows, hide windows, switch between windows easily. And it would be very good to be able to make sessions and use them as I use buffers in vi. Guess what, I just found that tool of my dream. Its the "GNU screen". Its really amazing. It gave me everything that I wanted for. Here are some of the features of screen.
  1. Create multiple sessions, switch between the session
  2. Split the window into several windows, switch between the windows
  3. Copy/paste region of text on the screen without using mouse. I couldn't do it on conventional terminal.
  4. Detach session and attach the session again. In this way you can save your session and use it in later time. Its like the vncserver.
  5. And many more.....
One problem I had with the screen is its default escape keystroke which is Ctrl-A. I use Ctrl-A a lot to move to the beginning of a line in command line and emacs. I need this key stroke very much. But, screen is customizable and you can remap the keystrokes. So, I mapped the escape command to Ctrl-U. I did this by adding the following line in the .screenrc file.
escape ^Uu
Next, I wanted to use the j,k keys to move between the windows and did this by adding the following lines.
bind j focus down
bind k focus up
bind t focus top
bind b focus bottom
I wish to write a lot about the screen but because of my shortage of time, I would like to refer to the following site.
http://www.gnu.org/software/screen/