How to Create a New File in Ubuntu Terminal
Ubuntu's Cat and Touch terminal commands will create new files that contain no data, but are visible to the file manager or the Ls directory-listing command. Although the Touch command is normally used to change the time-stamp of existing files, it will also generate one or more new files with any extension you choose. The Cat, or concatenation, terminal command can copy the characters you type at the keyboard to an new file. If you press "Ctrl-C" and no other keys, however, Cat creates an empty file. The Touch and Cat commands work with Ubuntu derivatives like Kubuntu, Xubuntu and Lubuntu as well.
Use the Touch command to create the file you need. For example, type the following command at the terminal prompt to create a text file named "sample.txt."touch sample.txt
Press "Enter" to execute the Touch command and generate the empty file.
Type "ls -t -r" at the command prompt and then press "Enter" to confirm that your new file exists. This command will list all files in the current directory in the order in which they were created. Your new file will be the last item in the last line of the listing.
Type the Cat command that will create a new file and, if you wish, its contents. For example, to create a text file named "sample.txt," type the following command at the terminal prompt:cat >sample.txtThe ">" or redirection symbol tells Cat to copy any characters you type at the keyboard to the designated file.
Press the "Enter" key to execute the Cat command and then "Ctrl-C" to stop it. After Cat stops running, the new, empty file is automatically saved, closed and made accessible to other Ubuntu utilities. If you typed anything before pressing "Ctrl-C," that input will be written as the file's contents.
Type "ls -t -r" at the command prompt and then press "Enter" to confirm that your new file exists.
Tips & Warnings
- Although Touch and Cat can create file types like PDF, ODT or JPEG, you may not be able to open them with the associated application. For example, the GIMP won't open empty JPEGs or PNG images. On the other hand, when you open a zero-length, ODT word processor file with LibreOffice Writer, the program will display a document containing nonsensical characters.