Question
The example given for the use of output redirection here:
cat oceans.txt > continents.txt
focuses on overwriting the contents of an existing file. What is another common use for this operation?
Answer
First, let’s note that >
can also be used to create files. In the example above for instance, if we replace continents.txt
by newFile.txt
, where newFile.txt
doesn’t already exist in the directory,
cat oceans.txt > newFile.txt
will create the newFile.txt
, store it in the current directory, and make its contents the exact same as oceans.txt
. With that in mind, another common use for >
is to store the output of a program in a new file. It is likely that you have run a program at some time or other and watched as a lot of text speeds by on your screen. This is fine sometimes but in other cases you may want to store that text for further analysis. One way to do this on the command line is to run the program on the left side of >
and add the name that you would like to call the new file (or an old file to be overwritten) on the right side. For instance, if the program executable is called a.out
and I want the output to be stored in a file called out.txt
, I write
./a.out > out.txt
18 Likes
cp a.txt b.txt
seems to work the same as cat a.txt > b.txt
, because both commands overwrite the content of b.txt
with that of a.txt
.
Is there any difference in usage/behavior between the two?
17 Likes
cp is kind of a COPY PASTE think about it that way - it copies file/directories. But it’s not transferring files (it’s not a cut paste) just copy paste. CAT is kind of a CUT PASTE. You are transferring file/directories from one file to the other. It also allows you create a new file in the process.
12 Likes
You are saying so but in the exercise we did cat glaciers.txt >> rivers.txt
and it just copied and added some stuff from glaciers.txt
to rivers.txt
, it didn’t CUT anything as you are implying…
Yes, but the difference is not that one works like a “cut and paste” whilst the other works like “copy and paste” as has previously been suggested…
Let’s look at what actually happens in each case.
-
cat a.txt > b.txt
There are several things happening in this short command:
1.1 cat a.txt
is telling cat
to read the contents of the file a.txt
. Once it’s done so, cat
's default behaviour is to output the contents to the standard output (stdout) which is the terminal usually.
1.2 >
is the redirection operator, which tells cat
that rather than sending it’s output to the terminal we want it go to somewhere else.
1.3 b.txt
is where we are redirecting the output from cat
to.
The end result here is we overwrite the content of b.txt
with the content of a.txt
.
-
cp a.txt b.txt
Here we are not doing as much. All we are doing is copying the entire a.txt
file to b.txt
.
The difference is that our cat
command only changes the content of the file. If b.txt
already exists, we change only it’s contents; if b.txt
is a new file then we create a new file with the default permissions as defined by the system.
If we copy using the cp a.txt b.txt
variation, we don’t just copy the contents of the file - we copy the entire file, including its permissions so if the original file was read-only then the copy will be also.
This may seem a subtle difference to you, but it’s actually quite significant. There’s more explanation available here.
10 Likes