How to Copy a File in the Terminal in One Line
You have just learned how to save the output of a command into a file, and even how to add more text to the bottom of it. But here’s a new puzzle: what if you want to copy a whole file? On your phone or laptop, you would right-click and pick “copy.” In the terminal, there is no right-click and no menu. Just one clean line of typing.
To copy a file in the terminal in one line, run cat a.txt > b.txt. The cat command reads everything inside a.txt, and the > sign pours that text into a brand-new file called b.txt. Whatever was in a.txt now also lives in b.txt — a copy made with one short command.
What does this command actually do?
You have already met two small tools in earlier lessons. The first is cat, which reads a file out loud, showing you what is inside. The second is the greater-than sign, >, which sends output into a file instead of printing it on screen.
Put them together and you get a copy. Think of it as copy-paste, but terminal style.
Let’s break the line apart, piece by piece:
cat a.txtreads everything insidea.txt. That file is the source.>takes that text and pours it somewhere new.b.txtis the brand-new copy that receives the text.
So whatever was sitting inside a.txt now also sits inside b.txt. Same words, two files.
Why does copying a file this way matter?
This is how power users move data around — fast. No mouse, no menus, no clicking through three windows. Just one short line and the job is done.
When you start building real projects later in the free Zero to AI Hero course, you will copy files all the time: backing up a config before you edit it, duplicating a template, or making a safe copy before you try something risky. Learning to copy a file in the terminal now means these everyday tasks feel natural later.
How do I try it right now?
Open your terminal. If you do not have a file named a.txt yet, that is fine — the steps before this lesson showed you how to make one. Then type this:
cat a.txt > b.txt
Press Enter. Nothing seems to happen, and that is normal — the text went quietly into b.txt instead of onto your screen. To check that the copy worked, read the new file:
cat b.txt
You will see the contents of a.txt, now copied into b.txt. That is your proof. The same words live in both files.
A quick word of caution
The > sign always creates a fresh file or completely overwrites an existing one. So if b.txt already had something important inside it, this command would replace it without asking. When you only want to add to the bottom of a file instead of replacing it, you use the double sign >> — which you saw in the previous lesson on adding to a file. Pick the right one for the job.
Key takeaways
- To copy a file in the terminal, run
cat a.txt > b.txt— one clean line, no menus. catreads the source file; the>sign sends that text into a new file.- The new file (
b.txt) becomes an exact copy of the source (a.txt). - Check your work with
cat b.txtto see the copied contents. - Be careful:
>overwrites the target file, while>>adds to the bottom.
That is copying a file, terminal style. Next up, we move from copying text to finding text — searching inside files with a tool called grep, like a Ctrl-F for any file on your computer.
🚀 Take the full free course: Zero to AI Hero — learn to build with AI from scratch. New lessons daily, in Hindi & English.
Want to actually learn this?
151 free 2-minute lessons — from "what's a file?" to building with AI.
▶ Start the free course