5 Simple Ways to Create a File in a Directory in Linux - wikiHow (2024)

  • Categories
  • Computers and Electronics
  • Operating Systems
  • Linux

Download Article

Explore this Article

methods

1Using the "Touch" Command

2Using the "Cat" Command

3Using the Nano Text Editor

4Using the "Echo" Command

5Using the VI and VIM Text Editor

+Show 2 more...

-Show less...

Other Sections

Related Articles

References

Written byBlain Gunter|Edited byTravis Boylls

Last Updated: February 21, 2024Fact Checked

Download Article

This wikiHow article teaches you how to create a file in a directory in Linux. There are many ways you can create files using the Terminal in Linux. You can create simple text files using short Terminal commands, or you can use one of Linux's built-in text editors to create complex documents.

Method 1

Method 1 of 5:

Using the "Touch" Command

Download Article

  1. 1

    Press Ctrl+Alt+T to open the Terminal. The Terminal is a command-line interface that is available on most Linux distributions. It typically has an icon that resembles a black screen with a white text cursor. You can either click the Terminal icon in the Apps menu, or press Ctrl + Alt + T at the same time to open the Terminal.[1]

  2. 2

    Navigate to the directory you want to create a file in. To do so, type cd followed by the path to the directory you want to create a file in and press Enter.. For example, you could type cd /home/username/Documents to navigate to your Documents folder.

    • Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory.[2]
    • You can also type cd ~ followed by the path relative to your Home/User folder to navigate to a directory. For example, type cd ~/Documents and press enter to navigate to "/home/user/Documents" directory.
    • Type pwd and press Enter to see what directory you are currently in.
    • Type mkdir followed by the directory name to create a new directory in the directory you are currently in.

    Advertisem*nt

  3. 3

    Type touch. The "touch" command is used to create an empty file in Linux. This is the easiest way to create a file in Linux.[3]

  4. 4

    Type the name of the file and press Enter. You can type the filename by itself (i.e. "touch blank_file"}} or add a file extension to the end of it (i.e. "touch blank_file.txt"). This creates a blank file.

    • If you need to create large amounts of files, use the touch command as follows: touch filename{01..20}.txt. This will create 20 new files using that file name.
    • Add multiple filenames after the "touch" command to create multiple files (i.e. "touch file_1 file_2 file_3").
    • Alternatively, you can create a file in any directory you want by adding the directory path after the "touch" command (i.e "touch /home/username/Documents/blank_file.txt")
    • You can create a file in multiple directories by adding the directory names after the touch command. For example touch category/file1.txt subcategory/blank_file.txt or touch {category,subcategory}/blank_file.txt.
    • If the file you are trying to create already exists in the directory, the time stamp for that file will be updated instead.
  5. 5

    Type ls and press Enter. This displays a list of all files in your current directory and allows you to verify that the file you just created is present.[4]

  6. Advertisem*nt

Method 2

Method 2 of 5:

Using the "Cat" Command

Download Article

  1. 1

    Press Ctrl+Alt+T to open the Terminal. The Terminal is a command-line interface that is available on most Linux distributions. It typically has an icon that resembles a black screen with a white text cursor. You can either click the Terminal icon in the Apps menu, or press Ctrl + Alt + T at the same time to open the Terminal.[5]

  2. 2

    Navigate to the directory you want to create a file in. To do so, type cd followed by the path to the directory you want to create a file in and press Enter.. For example, you could type cd /home/username/Documents to navigate to your Documents folder.

    • Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory.[6]
    • You can also type cd ~ followed by the path relative to your Home/User folder to navigate to a directory. For example, type cd ~/Documents and press enter to navigate to "/home/user/Documents" directory.
    • Type pwd and press Enter to see what directory you are currently in.
    • Type mkdir followed by the directory name to create a new directory in the directory you are currently in.
  3. 3

    Type cat > followed by the file name and press + Enter. For example, you can type cat >README.txt to create a new text file. You can type the filename by itself (i.e. "README") or add a file extension to the end of it (i.e. "cat >hello.txt"). Use the "cat" command to create files that contain text or code.[7]

    • Alternatively, you can use the command cat >> to append an existing file. If no file exists with the specified file name, a new file will be created. If a file by that name already exists, then the text you add will be appended to the end of the file instead of replacing the existing text.[8]
  4. 4

    Type the text you want to go in the file. After creating a file using the "cat" command, the Terminal enters write mode allowing you to add text to the file. Type the text you want the file to contain on the next line.

  5. 5

    Press Ctrl+d. This saves the file with the text and exits write mode in the Terminal.

    • To view the text in the file, simply type "cat" (without the ">" symbol) followed by the file name and press Enter (i.e. cat README.txt). This displays the text in the Terminal.
  6. 6

    Type ls and press Enter. This displays a list of all files in your current directory and allows you to verify that the file you just created is present.[9]

  7. Advertisem*nt

Method 3

Method 3 of 5:

Using the Nano Text Editor

Download Article

  1. 1

    Press Ctrl+Alt+T to open the Terminal. The Terminal is a command-line interface that is available on most Linux distributions. It typically has an icon that resembles a black screen with a white text cursor. You can either click the Terminal icon in the Apps menu, or press Ctrl + Alt + T at the same time to open the Terminal.[10]

  2. 2

    Navigate to the directory you want to create a file in. To do so, type cd followed by the path to the directory you want to create a file in and press Enter.. For example, you could type cd /home/username/Documents to navigate to your Documents folder.

    • Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory.[11]
    • You can also type cd ~ followed by the path relative to your Home/User folder to navigate to a directory. For example, type cd ~/Documents and press enter to navigate to "/home/user/Documents" directory.
    • Type pwd and press Enter to see what directory you are currently in.
    • Type mkdir followed by the directory name to create a new directory in the directory you are currently in.
  3. 3

    Type nano followed by a file name and press Enter. You can type the filename by itself (i.e. "nano text_file"}} or add a file extension to the end of it (i.e. "nano python_program.py"). This creates a new file and opens it in Nano.

    • Nano may not be available on all Linux distributions. To install Nano, type sudo apt install nano and press Enter to install Nano on Ubuntu/Debian. Type sudo yum install nano and press Enter to install Nano on CentOS/Fedora.
    • You can also specify which directory you want to create the file in by entering the path of the directory after the "vi" command (i.e nano /home/username/Documents/website/main.php).
  4. 4

    Type the text you want your file to contain. As soon as Nano opens, you can begin entering your text.

  5. 5

    Press Ctrl+X. Use this keyboard shortcut to save and exit Nano when you are done editing your text.

  6. 6

    Press y followed by Enter. This confirms that you want to save your file and exits Nano. You will be returned to the standard Terminal command-line.

  7. Advertisem*nt

Method 4 of 5:

Using the "Echo" Command

Download Article

  1. 1

    Press Ctrl+Alt+T to open the Terminal. The Terminal is a command-line interface that is available on most Linux distributions. It typically has an icon that resembles a black screen with a white text cursor. You can either click the Terminal icon in the Apps menu, or press Ctrl + Alt + T at the same time to open the Terminal.[12]

  2. 2

    Navigate to the directory you want to create a file in. To do so, type cd followed by the path to the directory you want to create a file in and press Enter.. For example, you could type cd /home/username/Documents to navigate to your Documents folder.

    • Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory.[13]
    • You can also type cd ~ followed by the path relative to your Home/User folder to navigate to a directory. For example, type cd ~/Documents and press enter to navigate to "/home/user/Documents" directory.
    • Type pwd and press Enter to see what directory you are currently in.
    • Type mkdir followed by the directory name to create a new directory in the directory you are currently in.
  3. 3

    Type echo followed by some text in quotations. The text you enter in quotations will be saved to the file. Use the "echo" command to create files with short lines of text.[14]

  4. 4

    Type > followed by the file name and press Enter. The entire command should look something like this echo "Hello World" >hello_world.txt. This saves the file with the text in quotations.[15]

    • Alternatively, you can add >> at the end of the text. This will create a new file if no file by the file name specified exists. If a file by that name already exists, the text will be appended to the end of the existing file text.
  5. 5

    Type ls and press Enter. This displays a list of all files in your current directory and allows you to verify that the file you just created is present.

  6. Advertisem*nt

Method 5

Method 5 of 5:

Using the VI and VIM Text Editor

Download Article

  1. 1

    Press Ctrl+Alt+T to open the Terminal. The Terminal is a command-line interface that is available on most Linux distributions. It typically has an icon that resembles a black screen with a white text cursor. You can either click the Terminal icon in the Apps menu, or press Ctrl + Alt + T at the same time to open the Terminal.[16]

  2. 2

    Navigate to the directory you want to create a file in. To do so, type cd followed by the path to the directory you want to create a file in and press Enter.. For example, you could type cd /home/username/Documents to navigate to your Documents folder.

    • Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory.[17]
    • You can also type cd ~ followed by the path relative to your Home/User folder to navigate to a directory. For example, type cd ~/Documents and press enter to navigate to "/home/user/Documents" directory.
    • Type pwd and press Enter to see what directory you are currently in.
    • Type mkdir followed by the directory name to create a new directory in the directory you are currently in.
  3. 3

    Type vi followed by the a file name and press Enter. You can type the filename by itself (i.e. "vi file1"}} or add a file extension to the end of it (i.e. "vi file1.txt"). This creates a new file by the name specified and opens the file in the VI text editor. VI is a Terminal-based text editor. It is the oldest text editor for Linux and works in all versions of Linux.

    • Alternatively, you can use the vim in place of the {[kbd|vi}} command. This opens the VIM text editor, which is a newer and more user-friendly version of VI. VIM may not be available on all Linux distributions. If VIM is not installed on your Linux distribution, type sudo apt install vim and press Enter to install VIM on Ubuntu/Debian. Type sudo yum install vim and press Enter to install VIM on CentOS/Fedora.[18]
    • You can also specify which directory you want to create the file in by entering the path of the directory after the "vi" command (i.e., "vi /home/username/Documents/Website/index.html").
  4. 4

    Press i to switch to Insert mode. VI has two modes, Command mode and Insert mode. Command mode allows you to perform various tasks using the keyboard keys. Insert mode allows you to enter text. Press I to switch to Insert mode.

  5. 5

    Type the text you want to add. Now that you are in "Insert" mode, you can type the text the way you would using any other text editor. Enter the text you want the file to contain.

  6. 6

    Press Esc. This switches from Insert mode back to Command mode..[19]

  7. 7

    Type the command to save and exit VI. To do so, type :wq and press Enter. This saves the file and returns to the standard Terminal command-line.

  8. Advertisem*nt

Expert Q&A

Ask a Question

200 characters left

Include your email address to get a message when this question is answered.

Submit

      Advertisem*nt

      Tips

      Submit a Tip

      All tip submissions are carefully reviewed before being published

      Submit

      Thanks for submitting a tip for review!

      You Might Also Like

      How toInstall Google Chrome Using Terminal on LinuxCan Linux Run .exe Files? How to Run Windows Software on Linux
      How to Tar a Directory and Subdirectories in LinuxFinding Linux Files: Complete Guide to Using Find CommandsHow toTake a Screenshot in LinuxHow toBecome Root in LinuxHow toAdd or Change the Default Gateway in LinuxHow to Install and Use Wine on LinuxHow toRun Files in LinuxHow to Run an INSTALL.sh Script on Linux in 4 Easy Steps

      Advertisem*nt

      More References (10)

      About This Article

      5 Simple Ways to Create a File in a Directory in Linux - wikiHow (50)

      Written by:

      Blain Gunter

      Computer Repair Specialist

      This article was written by Blain Gunter and by wikiHow staff writer, Travis Boylls. Blain Gunter is a Computer Repair Specialist and small business owner based in Bakersfield, California. He was first introduced to computers at the age of five and has over twenty years of experience in his field. He is both an IT consultant and computer repair technician and takes pride in his ability to troubleshoot anything. He works with hardware, software, Windows, macOS, GNU/Linux, and even vintage electronics. This article has been viewed 97,729 times.

      How helpful is this?

      Co-authors: 4

      Updated: February 21, 2024

      Views:97,729

      Categories: Linux

      • Print
      • Send fan mail to authors

      Thanks to all authors for creating a page that has been read 97,729 times.

      Is this article up to date?

      Advertisem*nt

      5 Simple Ways to Create a File in a Directory in Linux - wikiHow (2024)
      Top Articles
      Latest Posts
      Article information

      Author: Eusebia Nader

      Last Updated:

      Views: 5615

      Rating: 5 / 5 (80 voted)

      Reviews: 87% of readers found this page helpful

      Author information

      Name: Eusebia Nader

      Birthday: 1994-11-11

      Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

      Phone: +2316203969400

      Job: International Farming Consultant

      Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

      Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.