How to Create a File in Linux
There are many reasons you might want to create a file on a quick and easy basis in Linux with as little playing around as possible. You might want to make a placeholder for something you plan to put in later on. Many programs require a single blank file present in some directory to turn on or off an option.
You may also want to create full text files from the command line, which is also possible, but you’ll need to be working from a command line environment for the following examples. You can go search for terminal on the Dash or Whisker Menu. You can also hold down Ctrl, Alt and F1-F6 to get to a virtual terminal or hold down Ctrl, Alt and T in most desktop environments. You might also want to click on the Applications menu, point to the System Tools menu and then select Terminal.
Method 1: With the touch Command
You can type touch empty.txt to create an empty file in whatever directory you’re currently located in. If your command prompt defaults to the home directory, then this would create an empty text file called empty.txt in your home directory as long as there isn’t already an empty.txt file in it. The touch command will update the file modification date for any file that already exists.
Otherwise, if you type touch followed by the name of any file that doesn’t exist this will immediately create an empty zero-byte text file. It’s the standard way to do it, doesn’t require any further input and won’t provide you with any real output when you do it. You can type ls and then push enter to prove that the file is there. You can always use any file name you’d like as long as it’s valid.
Method 2: Using the echo Command
Generally the echo command simply echoes whatever you tell it to straight to the command line. You can redirect it’s output to create a new file. You can use echo -n > test.txt to create an empty file. This works just like touch and features nothing in it. You can also type echo Some text > test.txt and push return to create a file with a single line of text in it. Naturally you can replace Some text and the file name with whatever you’d like, but remember that if that file already exists doing this will overwrite it and get rid of your old file so it’s best to be careful! Alternatively, you might want to use echo ” > testFile.txt, which will create a file with nothing but an empty line in it.
Method 3: With the printf Command
You may want to try printf ” > testFile to create a completely empty file or printf ‘\n’ > testFile to create one that has nothing but a newline character in it. Once again, this will overwrite any file with this name and thus it must be used with caution. While this isn’t significantly different from the other methods, you may see it sometimes in scripts. On some older Linux distributions as well as some other implementations of Unix, the echo -n command won’t strip out newlines, so this is another reason to occasionally resort to this method. Using the touch command by itself, however, is almost always easiest.
Method 4: Redirecting Output from cat
While you’ve probably used cat to read the contents of a short text file or perhaps join two of them together, you can actually create files right from the terminal window with it by using it as a sort of primitive text editor. Let’s say you wanted to create a simple script. You could type cat > hello.sh and push enter. Now start typing #!/bin/bash and push enter followed by echo Hello World and push enter. You can then hold down Ctrl and push the D key to save your file. Type cat hello.sh to see the contents. It would be just like you wrote it in a text editor.
This is an extremely useful trick for any case where you need to create a text file quickly from the command line that actually has some text in it. While you’d need to use chmod +x hello.sh to get your script to execute, this is otherwise a great way to write out scripts in a hurry. You can also use it to write configuration files and anything else that only needs a line or two written very quickly. This isn’t limited to anything at all, and you should feel free to use it so long as you’re not overwriting another file.
Ironically, you can also use redirection to create empty files in the same way you use touch to do the same. Try > bill from the command line to create an empty file. You may have to use :> bill if you’re working with the c shell or some other versions of the tcsh environment.
Again, keep in mind that you could be using any file names that you’d like here. These tricks are in no way limited to writing scripts or anything else of the sort. You can be pretty creative when applying them. You could even do this more exotically if you ever had any need to. For instance, you may want to use cp /dev/null bill instead of the above, so that you could literally copy Linux’s special device file to the new file, which is naturally blank. This once more does the same job that touch would have.