- How to create a file in Linux using the bash shell terminal
- How to create a file in Linux from terminal window?
- How to create a text file using the cat command
- How to create an empty text file using the touch command
- Creating a file in Linux using the echo or printf
- Appending data
- How to create a file in Linux using joe text editor
- How to create a text file in Linux using vi / vim text editor
- Conclusion
- 4 Ways to Create a Text File in Linux Terminal
- Create file in Linux command line
- 1. Create an empty file using touch command
- 2. Create files using cat command
- 3. Create new file using echo command
- 4. Create a new file using a text editor like Nano or Vim
- How To Create Files in Linux From a Bash Shell Prompt
- Create a Text File using cat command
- Create a text file using echo or printf command
- Create a Text File using nano text editor
- Create a Text File using joe text editor
- Create a Text File using vi / vim text editor
How to create a file in Linux using the bash shell terminal
I am a new Linux system user. How do I create a file in Linux using the bash shell terminal? What is the fastest and easiest way to create a file in a Linux terminal?
Introduction – A file is nothing but a container in a Linux based system for storing information. For example, music stored in a file named foo.mp4. Similarly, my cat’s picture stored in kitten.jpg and so on. This page shows various methods to create a file in Linux using the terminal window.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Bash shell/terminal on Linux |
Est. reading time | 3 minutes |
How to create a file in Linux from terminal window?
- Create an empty text file named foo.txt:
touch foo.bar
OR
> foo.bar - Make a text file on Linux:
cat > filename.txt - Add data and press CTRL + D to save the filename.txt when using cat on Linux
- Run shell command:
echo ‘This is a test’ > data.txt - Append text to existing file in Linux:
echo ‘yet another line’ >> data.txt
Let us see some examples for creating a text files on Linux operating systems.
How to create a text file using the cat command
To create a text file named sales.txt, type the following command and then press [Enter] key:
cat > sales.txt
Now type your lines of text. For example:
When done and you need to save and exit, press Ctrl + D to return to the bash shell prompt. To view file use cat or more command/less command:
cat sales.txt
more sales.txt
How to create a file in Linux from terminal window
How to create an empty text file using the touch command
Simply type any one of the following command:
> data.txt
OR
touch test.txt
Verify that empty files are created with the help of ls command:
ls -l data.txt test.txt
Creating a file in Linux using the echo or printf
Let us create a file called quote1.txt using echo command, enter:
echo «While I thought that I was learning how to live, I have been learning how to die.» > quote1.txt
OR use the printf command printf ‘Study nature, love nature, stay close to nature. It will never fail you.\n’ > quote2.txt
Appending data
Use the the >> instead of > to append data to existing file and to avoid overwriting files. The syntax is:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How to create a file in Linux using joe text editor
JOE is text editor. To create a file called delta.txt, type:
joe -help delta.txt
You will see help menu on screen. Next type something. To save the file and leave joe, by typing ^KX (press CTRL+K+X).
How to create a text file in Linux using vi / vim text editor
The vi / vim is another text editor. To create a file called purchase.txt, type:
vi purchase.txt
OR
vim purchase.txt
Press i to insert new text. To save the file and leave vi, type ESC + : + x (press ESC key, type : followed by x and [enter] key).
Conclusion
You learned various methods that allow you to create text files in a Linux or Unix/macOS terminal window quickly.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
4 Ways to Create a Text File in Linux Terminal
In this Linux beginner series, you’ll learn various methods to create a text file in Linux terminal.
If you have used the desktop oriented operating system such as Windows, creating file is a piece of cake. You right click in the file explorer and you would find the option of creating new file.
Things won’t look the same when you are in a command line environment. There is no right click option here. So how do you create a file in Linux then? Let me show you that.
Create file in Linux command line
There are various ways of creating a new file in Linux terminal. I’ll show you the commands one by one. I am using Ubuntu here but creating files in Ubuntu terminal is the same as any other Linux distribution.
1. Create an empty file using touch command
One of the biggest usages of the touch command in Linux is to create a new empty file. The syntax is super simple.
If the file doesn’t exist already, it will create a new empty file. If a file with the same name exists already, it will update the timestamps of the file.
2. Create files using cat command
Another popular way of creating new file is by using the cat command in Linux. The cat command is mostly used for viewing the content of a file but you can use it to create new file as well.
You can write some new text at this time if you want but that’s not necessary. To save and exit, use Ctrl+D terminal shortcut.
If the file with that name already exists and you write new text in it using the cat command, the new lines will be appended at the end of the file.
3. Create new file using echo command
The main use of the echo command is to simply repeat (echo) what you type on the screen. But if you use the redirection with echo, you can create a new file.
To create a new empty file using echo you can use something like this:
The newly created filename.txt file will have the following text: This is a sample text. You can view the file in Linux using cat or other viewing commands.
You are not obliged to put a sample text with echo. You can create an (almost) empty file using the echo command like this:
This will create a new file with just one empty line. You can check the number of lines with wc command.
4. Create a new file using a text editor like Nano or Vim
The last method in this series is the use of a text editor. A terminal-based text editor such as Emacs, Vim or Nano can surely be used for creating a new file in Linux.
Before you use these text editors, you should make sure that you know the basics such as saving an existing from the editor. Unlike the GUI tools, using Ctrl+S in the terminal won’t save the file. It could, in fact, send your terminal into a seemingly frozen state from which you recover using Ctrl+Q.
Let’s say you are going to use Vim editor. Make sure that you are aware of the basic vim commands, and then open a new file with it like this:
What’s your favorite command?
So, I just shared 4 different ways of creating a file in Linux. Personally, I prefer using touch for creating empty file and Vim if I have to edit the file. On a related note, you may want to learn about the file command in Linux that is helpful in determining the actual type of the file.
Which command do you prefer here? Please share your views in the comment section below.
Источник
How To Create Files in Linux From a Bash Shell Prompt
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | 5m |
- cat command
- echo or printf command
- Nano text editor
- vi text editor
- joe text editor
- Any other console based text editor
Create a Text File using cat command
To create a text file called foo.txt, enter:
$ cat > foo.txt
Output:
To display file contents, type:
$ cat foo.txt
Create a text file using echo or printf command
To create a file called foo.txt, enter:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
To display file contents, type:
$ cat foo.txt
Create a Text File using nano text editor
Nano is a text editor from GNU project for Linux or Unix-like system. To create a file named foo.txt, run:
$ nano foo.txt
Sample outputs:
GNU nano is a simple terminal-based text editor for Linux/Unix
Create a Text File using joe text editor
JOE is text editor. To create a file called foo.txt, type:
$ joe -help foo.txt
You will see help menu on screen. Next type something. To save the file and leave joe, by typing ^KX (press CTRL+K+X).
Create a Text File using vi / vim text editor
vi / vim is another text editor. To create a file called bar.txt, type:
$ vi bar.txt
Press ‘i’ to insert new text. To save the file and leave vi, type ESC+:+x (press ESC key, type : followed by x and [enter] key).
Further readings
- Learn more about joe here : Introduction to the Joe Text Editor
- Learn more about vi here : Download mastering the VI editor pdf version.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
You can also use touch command to create empty files:
touch foobar.txt
basically i m beginner could please give me any links or book to understand linux architecture, well i m also confused in using linux commands since there r so many option is there any web which explains how to use linux commands
thanks in advance.
thanks for pointing out touch command!
echo ‘ This is a multiline
text file created
from the bash shell
using echo command’ > multiline.txt
i want create a text file. pls could you help me…
Thanks a lot! It helps me.
Thank you very much! all of you
Excellent, cause I just used it at work, and it worked and helped me.
Detailed information but would appreciate if someone can upload the solution for cat command as in executing or running the vi program. I am writing a finction in the vi and want to execute it outside the vi on the bash shell.
Thx- Nitish Anand
thanks guys. i do try cat, it works perfectly.
but in vi mode i’m not able to save & exit the file. i had to exit using ctrl+z & the file was not saved.
please help me. mail on nehra13@gmail.com
Have u tried using:-
!wq for save and exit VI
!w to just exit.
have two files, ‘file1’ and ‘file2’.
file1 has:
1234
4567
6789
file2 has:
abcd
efgh
ijkl
would like to combine into one like:
1234 | abcd
4567 | efgh
6789 | ijkl
Please give direction.
here is an solution. Using arrays:
—
#!/bin/bash
declare -a ARRAY1
declare -a ARRAY2
let count=0
let count1=0
while read LINE
do
ARRAY1[$count]=$LINE
((count++))
done Nitish Anand Mar 27, 2008 @ 12:07
Did u manage to resolve your problem? Try the folling link if thats what you meant:-
cd /var/log && ls -al
this switches to the /var/log directory if the directory change was sucessfuly it will then list the contents of that directory.
You can also list the contents of a directory without leaving your current location..
ls -al /var/log
Would list the contents of /var/log no matter your current location in the directory tree.
You can also use | to send the output of one command to the input of another..
mount | column -t
Show mount points in a column format
ls -al | grep *.jpg
List the contents of a directory buit only show files ending in .jpg
ps aux | grep X11
List processes but only show processes containing X11 > and list.txt
Will list the contents of the /var/log directory and save it in the file list.txt
hope this helps
You can chain copmmands together using &&
cd /var/log && ls -al
this swithces to the /var/log directory if the directory change was sucessfuly it will then list the contents o fhte directory.
just to add to that, you can likewise chain commands together with the || operator (OR operator, not to be confused with a pipe | )
Code:
$ cd /mnt || cd /varthis would attempt to change to the /mnt directory. If it could not it would then switch to the /var directory. If the cd /mnt command was successfull then it would stop. Similarly,
Code:
$ cd /blah || cd /homeit’s probably safe to say there is no /blah directory on your computer so typing this command would attempt to change to a non-existant directory, fail, and consequently change to /home
have fun!
There is so much to learn when you get inside of linux, for example, VI is not the easier text editor but it rocks xD
That’s good I am new Linuxer , now I am studying it .
Thanks.
Thx a lot guys.
I am also a newbie to Linux and it helps a lot!
how to chkdsk in linux cmd mode
how to check in all disk drive in linux command mode
how to install other software in linux command mode
please i am not able to understand to do program with echo command. please tell mr steps from starting….
how to save lines in a file? how to execute ?
how to save lines in a file at unix? how to execute?
Just $ > filename will do the same, manily creating a empty file named “filename”
Go ahead type “> test” and see for your self
What is the main difference between touch & vi commnd?
Could someone help me understand what this means?
Save the information that you have read from the keyboard into a file
how to search the empty file in linux …
For that which command use .
please tell me …. help me …..
Perhaps a subliminal mistake, looks like you used ‘cat foot.txt’ instead of ‘cat foo.txt’ in your example.
mu /dev/hdc had 2.7 G and its now 100%. Need to clear it.
I am trying to maintain a list of “ROMS” be it NDS, or MAME, no matter.
Instead of accessing each page on a website, is it possible to use cat to look for say; title” … ” within .aspx on the website and copy it to local txt file ? I have been using ..
cat wordlist/* | tr -cs A-Za-z ’12’ | tr A-Z a-z | sort | uniq > wordlist/masterlist.txt to merge numerous txt files into one, have them sort, and no repeats. something like this to be able to input the website start & end page and where to save the roms list.. there doesn’t really seem to be any 1 site that has a complete & accurate list. I an not trying to circumvent anything to get to the roms, I merely wish to be able to goto a webpage, enter in the variables, run a script to gather the list from the site, then after I have done this to a few sites run the above quoted, to merge the different sites into 1 ( hopefully ) accurate list so I know the names & numbers of the roms for each list.
Nice job.. thanxxx… 😀
hey guys, how can I concatenate an echo command & DATE to only one line ..
like:
echo “The date is: ” && date >> file.txt
but this command will display text “The date is: ” and insert the date into the file.txt
the output I want is
“The date is 07-12-12.”
echo `echo The date is: && date` >> file.txt
Very helpful steps for a fresher in C/C++
pls send me how to create log file(tape wirrten is normal file, normal file format only not tar file format)
i want create “example.com” file in this dir:
etc/apache2/
plz help me…
tnx
How do you edit the file you created ?
Use a text editor such as vi/emacs/joe and more.
Great, but I found a typo:
To display file contents, type
$ cat foot.txt
It should be foo.txt not foot.txt.
Thanks for the heads up!
I am doing with my BS Computer Science, I am provided with a project to make a
A_primitive linux shell using terminal of Ubuntu or GNU (GCC) compiler and in it i have to set up a shell environment to use the following commands
1 cd
2 ls
3 pwd
4 pop
5 push
B_ using any technique within this file system to make a single file password protected.
Noted: Can’t use ‘system’ or ‘exe’ command but can use libC and its assorted libraries also can use dirent.h,ctype.h.
please please please help me with this having said that i have to submit it on monday 9th March,2015
how we can create html file automatically in linux
Thank you for the post. It keeps me coming back because it is easy and very direct.
Источник