How to Create a File in Unix
This wikiHow 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...
Method 1 of 4:
Creating a Blank File with Touch
-
Picture 1 of How to Create a File in Unix 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. -
Picture 2 of How to Create a File in Unix 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. -
Picture 3 of How to Create a File in Unix 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
-
Picture 4 of How to Create a File in Unix 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. -
Picture 5 of How to Create a File in Unix Type cat > newfilename and press ↵ Enter. Replace newfilename with whatever you'd like to call your new file. This opens a new blank line. -
Picture 6 of How to Create a File in Unix Type some text. Anything you type here will be added to the file. -
Picture 7 of How to Create a File in Unix Press ↵ Enter to move to a blank line. You'll need to be on a blank line to finish the cat command.[2] -
Picture 8 of How to Create a File in Unix 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.
Method 3 of 4:
Creating a Text File with Vi
-
Picture 9 of How to Create a File in Unix 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. -
Picture 10 of How to Create a File in Unix 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. -
Picture 11 of How to Create a File in Unix Type vi and press ↵ Enter. This opens the Vi (or Vim, depending on the version of Unix you're using) text editor.- To edit a specific text file with Vi, type vi filename' instead.
-
Picture 12 of How to Create a File in Unix 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. -
Picture 13 of How to Create a File in Unix 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.
- 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:
-
Picture 14 of How to Create a File in Unix Press Esc when you're ready to save the file. This places you into command mode. -
Picture 15 of How to Create a File in Unix 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).
-
Picture 16 of How to Create a File in Unix 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
-
Picture 17 of How to Create a File in Unix 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. -
Picture 18 of How to Create a File in Unix 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. -
Picture 19 of How to Create a File in Unix 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.- 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).
Update 04 March 2020
You should read it
- How to Run a Batch File from the Command Line on Windows
- Press type in C
- How to Find a File's Path on Windows
- What is a .DAT file and how to open a .DAT file?
- How to Make a New File in Windows
- Data type in C #
- 106 tips with Microsoft Office - Part 3
- How to Open an Img File on PC or Mac
- How to Install JDK in Linux
- How to Use Plain Text
- Set the default file type when saving Word documents
- What is USB Type C? Type C cable models are widely used
Maybe you are interested
Apple locks iOS 17.1 signing, users need to consider when upgrading to iOS 17.1.1 How to create a password protection for Excel sheets How to manage News Feed on Facebook application How to change the font when creating Zalo logs 27 images show that work risks are inevitable The secret of making lipstick is both beautiful and easy for stylish girls