How to Create a File in Unix

Method 1 of 4:

Creating a Blank File with Touch

  1. How to Create a File in Unix Picture 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 to Create a File in Unix Picture 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.
  3. How to Create a File in Unix Picture 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]
Method 2 of 4:

Creating a Text File with Cat

  1. How to Create a File in Unix Picture 4
    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 to Create a File in Unix Picture 5
    Type cat > newfilename and press Enter. Replace newfilename with whatever you'd like to call your new file. This opens a new blank line.
  3. How to Create a File in Unix Picture 6
    Type some text. Anything you type here will be added to the file.
  4. How to Create a File in Unix Picture 7
    Press Enter to move to a blank line. You'll need to be on a blank line to finish the cat command.[2]
  5. How to Create a File in Unix Picture 8
    Press Ctrl+D. This saves the file to the current directory with the name you entered.
    1. To view the vile, type cat filename and press Enter.
Method 3 of 4:

Creating a Text File with Vi

  1. How to Create a File in Unix Picture 9
    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 to Create a File in Unix Picture 10
    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 to Create a File in Unix Picture 11
    Type vi and press Enter. This opens the Vi (or Vim, depending on the version of Unix you're using) text editor.
    1. To edit a specific text file with Vi, type vi filename' instead.
  4. How to Create a File in Unix Picture 12
    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. How to Create a File in Unix Picture 13
    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.
    1. 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:
      1. x deletes the character under the cursor.
      2. dw deletes the current word.
      3. dd deletes the entire line.
      4. r replaces the letter under the cursor with the next one you type. This will automatically put you back into input mode after use.
      5. See How to Learn Vi to learn about more Vi commands.
  6. How to Create a File in Unix Picture 14
    Press Esc when you're ready to save the file. This places you into command mode.
  7. How to Create a File in Unix Picture 15
    Type :w newfilename and press Enter. Replace newfilename with the name of the file. This saves the file to the current directory.
    1. If you want to keep editing the file, press i to go back into input mode.
    2. The next time you want to save the file, you can just type :w in command mode (no file name necessary).
  8. How to Create a File in Unix Picture 16
    Press q and press Enter to quit Vi. This returns you to the command line.
Method 4 of 4:

Copying a File to a New File

  1. How to Create a File in Unix Picture 17
    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 to Create a File in Unix Picture 18
    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.
  3. How to Create a File in Unix Picture 19
    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.
    1. 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 ★ | 1 Vote

May be interested

  • Korn Shell OperatorKorn Shell Operator
    this tutorial lists all the operators available in the korn shell. here most of these operators are similar to what we have in the c program language.
  • Network communication utilities in Unix / LinuxNetwork communication utilities in Unix / Linux
    when you work in a distribution environment then you need to communicate with remote users and you also need access to remote unix devices.
  • Manage the default UNIX license with adduser and umaskManage the default UNIX license with adduser and umask
    the most important point of the file-level security process on unix systems is to limit the file access license as much as possible.
  • Control loop in Unix / LinuxControl loop in Unix / Linux
    the previous chapter has been shown how to create loops and work with them to perform various tasks. sometimes you need to stop a loop or continue their repetitive process.
  • System log in Unix / LinuxSystem log in Unix / Linux
    unix systems have a very powerful and flexible logging system, which gives you the ability to record almost anything you can imagine and then manipulate this log to track the information you require.
  • What is Unix / Linux?What is Unix / Linux?
    unix operating system is a collection of programs that act as a link between a computer and a user.
  • Basic Shell operatorsBasic Shell operators
    there are many operators supported by each shell. our tutorial is based on the default shell (bourne) so we are discussing bourne shell operators in this chapter.
  • How to create an EXE installation fileHow to create an EXE installation file
    sometimes to install certain software on your computer, you must use the .exe file, but have you ever wondered what the purpose of this file is and how it was created? in this article, tipsmake.com will answer that question and guide you to create an exe installation file.
  • Basic utilities: print, email in UnixBasic utilities: print, email in Unix
    by this chapter, you have a few basic insights about unix systems and some of its basic commands. this chapter will briefly discuss some of the basic but important utilities of unix utilities that you will use in your daily activities.
  • Instructions for installing Unix / LinuxInstructions for installing Unix / Linux
    an important unix concept is the environment, which is defined by environment variables. some are set up by the system, others are set up by you, by the shell or any program you download.