Download ArticleDownload Article

This How.com.vn teaches you different ways to create a new file at the Unix command prompt. To quickly create a blank file, use the touch command. To create a new text file from scratch, try the Vi text editor or the cat command. If you want to duplicate an existing file, use the cp (copy) command.

Method 1
Method 1 of 4:

Creating a Blank File with Touch

Download Article
  1. How.com.vn English: Step 1 Open a terminal window.
    If you're using a window manager, you can usually press Ctrl+Alt+T to open a new terminal window. If not, log into the system on which you want to create a file through the console.
  2. How.com.vn English: Step 2 Use cd to change to the desired directory.
    If you're already in the directory where you'd like to create the file you can skip this step.
    Advertisement
  3. How.com.vn English: Step 3 Type touch newfilename and press ↵ Enter.
    Replace newfilename with the desired file name. This creates a new blank file with that name in the current directory.[1]
  4. Advertisement
Method 2
Method 2 of 4:

Creating a Text File with Cat

Download Article
  1. How.com.vn English: Step 1 Open a terminal window.
    If you're using a window manager, you can usually press Ctrl+Alt+T to open a new terminal window. If not, log into the system on which you want to create a file through the console.
  2. How.com.vn English: Step 2 Type cat > newfilename and press ↵ Enter.
    Replace newfilename with whatever you'd like to call your new file. This opens a new blank line.[2]
  3. How.com.vn English: Step 3 Type some text.
    Anything you type here will be added to the file.
  4. How.com.vn English: Step 4 Press ↵ Enter to move to a blank line.
    You'll need to be on a blank line to finish the cat command.[3]
  5. How.com.vn English: Step 5 Press Ctrl+D.
    This saves the file to the current directory with the name you entered.
    • To view the vile, type cat filename and press Enter.
  6. Advertisement
Method 3
Method 3 of 4:

Creating a Text File with Vi

Download Article
  1. How.com.vn English: Step 1 Open a terminal window.
    If you're using a window manager, you can usually press Ctrl+Alt+T to open a new terminal window. If not, log into the system on which you want to create a file through the console.
  2. How.com.vn English: Step 2 Use cd to change to the desired directory.
    You'll want to be in the directory where you'd like to store your new file before opening the text editor.
  3. How.com.vn English: Step 3 Type vi and press ↵ Enter.
    This opens the Vi (or Vim, depending on the version of Unix you're using) text editor.[4]
    • To edit a specific text file with Vi, type vi filename' instead.
  4. How.com.vn English: Step 4 Press i to enter input mode.
    Vi has two modes—insert mode and command mode. You must be in input mode to type text into the new file.[5]
  5. How.com.vn English: Step 5 Type some text (optional).
    If you want to create a blank file you can just skip this step. Otherwise, type any text you wish to add now.
    • You can't use your mouse or the arrow keys in Vi. If you make a mistake when typing, you'll need to run a command in command mode. Press Esc to make the arrow keys available, use them to move the cursor to the location of the mistake, and then use any of these commands:
      • x deletes the character under the cursor.
      • dw deletes the current word.
      • dd deletes the entire line.
      • r replaces the letter under the cursor with the next one you type. This will automatically put you back into input mode after use.
      • See How to Learn Vi to learn about more Vi commands.
  6. How.com.vn English: Step 6 Press Esc when you're ready to save the file.
    This places you into command mode.
  7. How.com.vn English: Step 7 Type :w newfilename and press ↵ Enter.
    Replace newfilename with the name of the file. This saves the file to the current directory.
    • If you want to keep editing the file, press i to go back into input mode.
    • The next time you want to save the file, you can just type :w in command mode (no file name necessary).
  8. How.com.vn English: Step 8 Press q and press ↵ Enter to quit Vi.
    This returns you to the command line.
  9. Advertisement
Method 4
Method 4 of 4:

Copying a File to a New File

Download Article
  1. How.com.vn English: Step 1 Open a terminal window.
    If you're using a window manager, you can usually press Ctrl+Alt+T to open a new terminal window. If not, log into the system on which you want to create a file through the console.
  2. How.com.vn English: Step 2 Use cd to change to the desired directory (optional).
    You'll be using the cp (copy) command to copy an existing file to another new file. You'll either need to move to the directory that contains the original file or know its full path.[6]
  3. How.com.vn English: Step 3 Type cp originalfile newfile and press ↵ Enter.
    Replace originalfile with the name of the file you want to copy, and newfile with the name of the desired new file. This creates a new file that contains the contents of the old file.[7]
    • Use the full path to the file if you're working with files outside of the current working directory (e.g., cp /home/maria/textfile1 /home/maria/contracts/textfile2).
  4. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Video

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      About This Article

      How.com.vn English: Nicole Levine, MFA
      Written by:
      How.com.vn Technology Writer
      This article was co-authored by How.com.vn staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for How.com.vn. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 112,798 times.
      How helpful is this?
      Co-authors: 14
      Updated: February 10, 2024
      Views: 112,798
      Categories: Operating Systems
      Article SummaryX

      1. Open a terminal window.
      2. Type "touch newfilename".
      3. Press Enter.

      Did this summary help you?

      Thanks to all authors for creating a page that has been read 112,798 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement