Monday, February 22, 2021

Creating new text file from terminal on Windows and Ubuntu



Creating new text file on Ubuntu from terminal

The Linux tee command is a way to write the standard output to a file. Or, to quote from the man page documentation, tee - read from standard input and write to standard output and files This is a little different from redirecting output to a file. In this case, the output is still send to standard out, but an additional copy is sent to create your text file. There are some good examples of how this can be useful, shown in the info documentation for tee. To view the info page, open a terminal window and enter info coreutils 'tee invocation' To understand the basic usage of the tee command, go to a terminal window and navigate to a directory that contains a small number of files. You can then use the ls and tee commands to create a text file that contains a listing of the files in that directory. ls | tee listing.txt You can also append a file using the -a switch. This will insert the output lines at the bottom of a pre-existing file. ls | tee -a listing.txt An example of when tee comes in handy is when you want to create a simple text file as root from your user account. I used it to create the fully qualified domain name file for my Apache configuration. You would think you could use: sudo echo "ServerName localhost" > /etc/apache2/conf.d/fqdn But that doesn't work because the sudo only affects the echo command and not the output redirection. Instead, you can use tee like this. echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn That command succeeds in creating the fqdn file with the desired content. Some other commands to create text files on Ubuntu: Following line creates an "HTML" page. $ echo " " >> mypage.html Following command creates a 0 size text file: $ touch mypage.html

Creating new text file on Windows from CMD

1. type NUL > EmptyFile.txt 2. echo. 2>EmptyFile.txt This command creates an empty file as there is no error produced here that would go into the file. The command "echo." outputs an empty line in the CMD. 3. copy nul file.txt > nul 4. REM. > empty.file 5. fsutil file createnew file.cmd 0 # to create a file on a mapped drive 6. aaaa > empty_file This will output something like: 'aaaa' is not recognized as an internal or external command, operable program or batch file. But it does create an empty file. In the same spirit and the shortest one is: 7. .>out.txt It does give an error: '.' is not recognized as an internal or external command But this error is on stderr. And > only redirects stdout, where nothing have been produced. Hence the creation of an empty file. The error message can be disregarded here. Or redirected to NUL: 8. .>out.txt 2>NUL 9. echo.>filename (echo "" would actually put "" in the file! And echo without the '.' would put "Command ECHO activated" in the file...) Note: the resulting file is not empty but includes a return line sequence: 2 bytes. 10. Batch solution for a real empty file: <nul (set/p z=) >filename dir filename 11/09/2009 19:45 0 filename 1 file(s) 0 bytes The "<nul" pipes a nul response to the set/p command, which will cause the variable used to remain unchanged. As usual with set/p, the string to the right of the equal sign is displayed as a prompt with no CRLF. Since here the "string to the right of the equal sign" is empty... the result is an empty file. The difference with cd. > filename (does produce a 0-byte-length file) is that this "bit of redirection" (the <nul... trick) can be used to echo lines without any CR: <nul (set/p z=hello) >out.txt <nul (set/p z= world!) >>out.txt dir out.txt The dir command should indicate the file size as 11 bytes: "helloworld!".
% http://tuxtweaks.com/2010/06/command-line-basics-create-text-files-with-tee/ % https://stackoverflow.com/questions/1702762/how-to-create-an-empty-file-at-the-command-line-in-windows Tags: Linux, Windows CMD, Technology

No comments:

Post a Comment