In a graphical operating system like Windows, creating a new file usually involves opening a program (like Microsoft Word), clicking File, clicking New, typing some text, and then saving it. In the Linux command line, you do not have graphical windows or menus. You must command the operating system to spawn a new file on the hard drive using specific commands.
Depending on what you are trying to achieve, there are three primary methods for creating a file in Linux: the touch command (for empty files), the redirection operator (for quick one-liners), and a text editor like nano (for actual writing).
Method 1: The touch Command (Create an Empty File)
The touch command is the most basic file creation tool in Linux. It instantly creates a file with 0 bytes of data inside it. It does not open the file, and it does not prompt you to type anything. It simply places the empty file on the hard drive and returns you to the command prompt.
This is incredibly useful when a piece of software requires a specific configuration file to exist before it can start, but the file doesn’t actually need to contain any text yet.
- Open your terminal.
- Type the
touchcommand followed by the name of the file you want to create (including the extension).
touch server_config.txt
Press Enter. The file is instantly created in your current directory. You can also use touch to create multiple empty files simultaneously by separating the names with a space:
touch index.html style.css script.js
Method 2: The Redirection Operator (Create and Write Quickly)
If you need to create a file and put exactly one line of text into it (like adding a single IP address to a blacklist), opening a full text editor is overkill. You can use the echo command combined with the redirection operator (>) to write the text and create the file in one single stroke.
- Open your terminal.
- Type
echo, followed by the text you want to write wrapped in quotation marks. - Add the
>symbol (which means “redirect output to”). - Type the name of the new file.
echo "192.168.1.100" > blacklist.txt
When you press Enter, Linux will create the file blacklist.txt and instantly inject the IP address into it. (Crucial Note: If blacklist.txt already exists, the > operator will completely overwrite it and destroy the old data. If you want to add the text to the bottom of an existing file without deleting the old data, you must use a double bracket: >>).
Method 3: The nano Text Editor (The Best Method)
If you are writing a python script or a massive server configuration file, you need a traditional text editor where you can use the arrow keys, press enter for new lines, and actually see what you are doing.
The easiest terminal text editor for beginners is nano.
- Open your terminal.
- Type the
nanocommand followed by the name of the file you want to create.
nano script.py
Instead of returning you to the command prompt, the entire terminal window will transform into a blank text editor canvas. You can immediately begin typing your code or text.
When you are finished writing, you must save the file using nano’s specific keyboard shortcuts (which are displayed at the bottom of the screen):
- Press Ctrl + O (the letter O, for Output) to save the file.
- Nano will ask you to confirm the file name at the bottom of the screen. Press Enter.
- Press Ctrl + X to exit the text editor and return to the normal command prompt.