How to quickly create a text file using the command line in Linux

If you’re a keyboardist, there are many things you can do easily using the Linux command line. To the example, there are some easy to use methods for creating text files in case you need to.

Create a text file with the Cat command

Our first method of creating text files is using the cat Command. This is useful when you want to add text to your new file right away.

Simply enter the following command at the Terminal prompt (replace “sample.txt” with any name for your file), then press Enter:

cat > sample.txt

After pressing Enter, do not return to the terminal prompt. Instead, the cursor is placed on the next line and you can start typing text in your file right away. Enter your lines of text and press Enter after each line. When you’re done, press Ctrl + D to exit the file and return to the command prompt.

To verify that your file was created, you can use the ls Command to display a directory listing for the file:

ls -l sample.txt


You can also use the cat command to view the contents of your file. Just enter the following command at the command prompt and then press Enter:

cat sample.txt

Create a text file with the touch command

You can also create a text file by typing touch Command. A difference between using this command and the cat Command that we covered in the last section is the one during the cat The command allows you to instantly type text into your file touch Command not. Another big difference is that the touch The command allows you to create multiple new files with a single command.

the touch The command is useful for quickly creating files that you want to use later.

To create a new file, type the following command at the Terminal prompt (replace “sample.txt” with the file name you want), then press Enter:

touch sample.txt

Note that you will receive no indication that the file was created; You have just returned to the command prompt. You can use the … ls Command to check the existence of your new file:

ls -l sample.txt

You can also add multiple new files to the. create touch Command. Just add as many additional filenames as you want (separated by spaces) to the end of the command:

touch sample1.txt sample2.txt sample3.txt


Again, there is no indication that the file was created, but a simple one ls Command indicates that the files actually exist:

And if you want to add text to your new files, you can just use a text editor like Vi.

Create a text file with the default redirect symbol (>)

You can also create a text file using the standard redirection symbol typically used to redirect the output of a command to a new file. If you use it without a previous command, the redirect icon will only create a new file. As touch If you create a file this way, you won’t be able to type text into the file right away. not as touch However, when creating a file with the redirect icon, you can only create one file at a time. We’re adding it for completeness and also because it’s the least typing experience when you’re just creating a single file.

To create a new file, type the following command at the Terminal prompt (replace “sample.txt” with the file name you want), then press Enter:

> sample.txt

You will not receive an indication that the file was created, but you can use the ls Command to check the existence of your new file:

ls -l sample.txt

With these three methods, you can quickly create text files on the Linux terminal, whether or not you need to enter text right away.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts