In Linux, you can create a file using redirection by redirecting output from a command to a file. This is a quick and efficient way to create files and add content to them. Here are some common methods to create a file using redirection:
1. Create an Empty File
To create an empty file, you can use the touch
command or simply use the redirection operator >
.
Using touch
:
touch newfile.txt
Using >
:
> newfile1.txt
2. Create a File with Content
You can redirect the output of commands like echo
or cat
to create a file with specific content.
Using echo
:
echo "This is a sample text" > newfile.txt
Using cat
:
You can use cat
with a heredoc to create a file with multiple lines of text.
cat >> newfile.txt << EOL This is an appended line. EOL
Examples
Example 1: Creating an Empty File
> newfile1.txt
This command creates an empty file named newfile1.txt
.
Example 2: Creating a File with a Single Line of Text
echo I am a goodman > nexfile.txt
This command creates a file named nexfile.txt
and writes “I am a goodman” into it.
Example 3: Appending Multiple Lines of Text into a exiting file
cat >> nexfile.txt << EOL I am a super goodman Another line Next line EOL
This command append multiple lines into file nexfile.txt
.
Using redirection in Linux is a powerful way to create and manage files. The >
operator is used to create new files and overwrite existing ones, while the >>
operator is used to append content to existing files.
About cat
command
please check here for more about cat command.
cat
is more than just a file viewer – its name comes from ‘concatenate’, meaning “to link together”.
let us create three text file textfile1.txt textfile2.txt textfile3.txt, and use echo
command to give them content text file 1, text file 2, text file 3 separately.
echo text file 1 > textfile1.txt echo text file 2 > textfile2.txt echo text file 3 > textfile3.txt
If you pass more than one filename to cat
it will output each of them, one after the other, as a single block of text:
cat testfile1.txt testfile2.txt testfile3.txt
results will be:
text file 1 text file 2 text file 3
Where you want to pass multiple file names to a single command, there are some useful shortcuts that can save you a lot of typing if the files have similar names. A question mark (“?”) can be used to indicate “any single character” within the file name. An asterisk (“*”) can be used to indicate “zero or more characters”. These are sometimes referred to as “wildcard” characters. A couple of examples might help, the following commands all do the same thing:
cat textfile?.txt
results will be :
text file 1 text file 2 text file 3
An asterisk (“*”) can be used to indicate “zero or more characters”
cat tex*
results will be:
text file 1 text file 2 text file 3
If you look at the output of ls
you’ll notice that the only files or folders that start with “t” are the three test files we’ve just created, so you could even simplify that last command even further to cat t*
, meaning “concatenate all the files whose names start with a t and are followed by zero or more other characters”.
We can use cat
command join all our files together into a single new file, then view it:
cat t* > combinedfile.txt
cat com*
results will be
text file 1 text file 2 text file 3
avoid typing the commands again you can use the Up Arrow and Down Arrow keys to move back and forth through the history of commands you’ve used. Press the Up Arrow a couple of times to get to the first cat
and press Enter to run it, then do the same again to get to the second.
As you can see, the file looks the same. That’s not because it’s been left untouched, but because the shell clears out all the content of the file before it writes the output of your cat
command into it. Because of this, you should be extra careful when using redirection to make sure that you don’t accidentally overwrite a file you need.
You can use >>
to append the content to the file.
cat t* >> combinedfile.txt
The content in combinedfile.txt will be following:
text file 1 text file 2 text file 3 text file 1 text file 2 text file 3