Writing to a file without a text editor
by \bin\bash


Inspired from the reference on the 'copy con file' in Alexey Kalmykov's tutorial about programming in extreme conditions, I started thinking about other ways of writing to a file without using a text editor. This is not a tutorial actually, as it is just a text containing some of the methods i thought of, together with their explanations. We shall start with the possible ways in a DOS prompt then in *NIX systems.

- DOS

1st way: copy con filename
Explanation: With this method we write to the 'filename' by telling the DOS command interpreter to copy the contents of the 'con' file to the 'filename'. But then what is 'con'? Well it must be a file right? Right! But it is not just any file. It is a special device file which is named 'con' as an abbreviation of the word 'console' which is the keyboard! So what we actually do is, copying the contents of the keyboard to the 'filename'. It is like saying : Hey mr. DOS, i want you to copy everything I type in the keyboard to the 'filename'. To terminate to process just press CTRL-Z. But then, what is a device file? Well remember those /dev/xxx files in a *NIX machine? Those are special files assinged to physical devices. The same goes for the 'con' file. Well, DOS and Windows machines have not only the con device file. There are others too. Some examples are : 'nul' and 'aux'; 'nul' stands for null ( it's equivalent is /dev/null in a *NIX box ), the famous 'black hole' ;), while 'aux' stands for auxiliary. In case you wonder how did i find out about the 'aux' and 'nul', the answer is by reading an old CP/M manual ;) and since DOS has some relations with CP/M i thought they may just exist in DOS too. If you start playing around with those 3 files you will notice some very interesting attributes. You cannot name a file or a directory as 'aux', 'con' or 'nul'. Also you cannot delete those files or rename them. I don't recommend you to try to copy con to aux or vice versa since you will notice some strange behaviour ;). One negative aspect of this method is that if you want to write to the same file after a while and you want the previous contents to be kept, you cannot the previous contents will be deleted.

2nd way: echo blah blah > filename
Explanation: Well, everybody knows what the 'echo paramaters' command does. It will print to your standard output which by the way is your screen the 'parameters'. OK, but what if we use the redirection symbol '>' ? This way we will redirect the paramaters to be printed not anymore to your screen but to the file after the redirection symbol. Try something like this : 'echo blah blah2 > file.txt'. Then open with edit or with notepad the file.txt. You will notice that it is not empty. It has a line containing 'blah blah2'. There is one problem : you cannot use ENTER ( or any of it's equals : alt-12 or CTRL-M ) to write multiple lines. Only one line at a time using the >> symbol. If you use single redirection the previous contents will be lost. On the other hand, if you use double redirection, the second line will be written after the last line in the file. Example: Suppose that file file1.txt does not exist. Enter the following commands at a DOS prompt :

C:\>echo line 1 > file1.txt
C:\>echo line 2 > file1.txt
C:\>echo line 3 > file1.txt
Open now file1.txt with any text editor or just do 'type file1.txt'. You will see that the only line in the file is :
line 3
Now do this at a DOS prompt:
C:\>echo line 1 >  file1.txt
C:\>echo line 2 >> file1.txt
C:\>echo line 3 >> file1.txt
Do the same as above. The contents of file1.txt are :
line 1
line 2
line 3
Everything should be clear by now!

3d way: type con > filename
Explanation: I am sure you know what the 'type file' command does. It prints ( types ) to the screen the contents of the 'file' given as a parameter. What we do here is obvious. We tell the command interpreter to print the contents of the keyboard to the filename. Wonder what just 'type con' does? Do it! You will notice that every line you type is repeated after you press enter. Why? Because it prints in the screen the everything you type in the keyboard. So we redirect the repetiton to the 'filename'. Press CTRL-C to end the process. Well this 3d way is the best way since we can both write as many lines as we wish and on the other hand we can use the >> to later write at the end of the same file without deleteing the previous contents.

- *NIX

1st way: cp /dev/console filename
Explanation: Same as DOS 1st way.

2nd way: echo blah blah > filename
Explanation: Same as DOS 2nd way. There is something else here which help us a lot. If you use the 'echo' command with the -e switch you can write multiple lines like this : 'echo line 1\\n line 2\\n line 3\\n > filename'.
The -e parameter tells the 'echo' program to interpret escape characters, \\n being the newline character.

3d way: cat /dev/console > filename
Explanation: Same as DOS 3d way. Works only as root due to restricted permissions of the /dev/console special file!

4th way: cat > file
Explanation: If you just 'cat' or 'cat -' you will get the same result as 'type con' which means you will copy the standard input to the standard output. So we just redirect the standard input to a file with 'cat > file' .


Well, these are the ways I have found as of right now. It does not mean that there are not any other ways. If you find any other method just shoot it :).