Linux: Create Text File on Linux Shell / Command Line

terminal-logo-smallThis is a very typical case, the need to create a temp file on the command line quickly. Opening editor, writing content, save file and quit editor is not the fastest possible way. A faster way is to use the cat command with the name of the file and write contents of the file and give the end-of-file character (Ctrl-D).

This is guide, howto create (or append to) text file without text editor on Linux shell / command line.

Create Text File Without Text Editor on Command Line

# Create file
$ cat > /tmp/temp_file_name.txt
some content
more content
and even more content
<Ctrl-D>
 
# View file content
$ cat /tmp/temp_file_name.txt
some content
more content
and even more content

Append to Temporary File Without Editor on Command Line

# Append to file
$ cat >> /tmp/temp_file_name.txt
append content
append more content
append even more content
<Ctrl-D>
 
# View file content
$ cat /tmp/temp_file_name.txt
some content
more content
and even more content
append content
append more content
append even more content
Follow If Not True Then False Updates!
  1. Linux: Display / Show File Contents (tabs, line breaks, non-printing characters)
  2. Linux: Get IP Address on Command Line/Shell (internal/external)
  3. Linux Tip: How to handle a files with a dash as first character from command line
  4. Google search from Linux and UNIX command line
  5. Linux: Enable Laptop Touchpad Vertical Scrolling and Tapping from Command Line

2 Comments

  1. How could you copy the text in the shell to a file. Say I run a series of commands. Such as wget, tar. ./configure, make, make install. I want to save all the messages in a file.

    • Hi john,

      You can do following to get your command output to file:

      ## Create new file ##
      command > /path/to/file.txt
       
      ## Append to file ##
      command >> /path/to/file.txt

      Real example:

      ## Create new file ##
      ls -la /var/log > /tmp/var-log.txt
       
      ## Append to file ##
      ls -la /var/log >> /tmp/var-log.txt

Leave a Comment

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

terms
Bear