How to send a file as an input to another file?
You can't use file1 > file2
to copy file1
's contents to file2
because there's no command there. You have to issue some command.
- Redirections apply to (1) a command you run or (2) the shell as a whole (if applied to the
exec
builtin). But they work by changing the source or target of actions that perform input-ouptut operations--that is, that read from or write to a file or device. You have to actually perform some action. -
file1 > file2
is a valid command, under some circumstances, but it doesn't copyfile1
tofile2
. Instead, as l0b0 says, it attempts to runfile1
as a program or script, and--whether or not that succeeds--sends the standard output from runningfile1
intofile2
. (Iffile2
is a regular file, it gets overwritten.) -
It's tempting to think something like
<file1 >file2
would work. But it does not: when you leave off the command, no operation is performed.file1
is used as input andfile2
is used as output... but since nothing is actually done, the output is empty, the only effect is to (a) createfile2
if it didn't already exist, or (b) makefile2
empty if it did:ek@Ilex:~$ echo foobar > file1 ek@Ilex:~$ echo quux > file2 ek@Ilex:~$ cat file1 foobar ek@Ilex:~$ cat file2 quux ek@Ilex:~$ <file1 >file2 ek@Ilex:~$ cat file2 ek@Ilex:~$ file file1 file2 file1: ASCII text file2: empty
You probably just want to run cp file1 file2
.
Assuming, that is, that file2
is a regular file (or doesn't exist but you want it to exist as a regular file after your command runs), you should probably simply use the cp
command.
As Sylvain Pineau says, you can use cat file1 > file2
as well. However, the cp
command is well-behaved when writing to a file that already exists: it overwrites the target with the source, but keeps the target's original permissions mask (and therefore is good even in situations where one might intuitively think cat file1 > file2
were needed):
ek@Ilex:~$ echo glockenspiel > file1
ek@Ilex:~$ echo flugelhorn > file2
ek@Ilex:~$ cat file1
glockenspiel
ek@Ilex:~$ cat file2
flugelhorn
ek@Ilex:~$ chmod +x file2
ek@Ilex:~$ ls -l file1 file2
-rw-rw-r-- 1 ek ek 13 Sep 16 03:28 file1
-rwxrwxr-x 1 ek ek 11 Sep 16 03:28 file2
ek@Ilex:~$ cp file1 file2
ek@Ilex:~$ cat file2
glockenspiel
ek@Ilex:~$ ls -l file1 file2
-rw-rw-r-- 1 ek ek 13 Sep 16 03:28 file1
-rwxrwxr-x 1 ek ek 13 Sep 16 03:28 file2
To append, you probably do want a (different kind of) redirection.
Both the cp
command and redirection with >
will overwrite the contents of a regular file, if it exists. But >>
redirection appends.
So if you want to append the contents of file1
to file2
instead of overwriting file2
with the contents of file1
, a redirection with >>
(not >
) is a good choice:
ek@Ilex:~$ echo 'Violets are blue.' > file1
ek@Ilex:~$ echo 'Roses are red.' > file2
ek@Ilex:~$ cat file1
Violets are blue.
ek@Ilex:~$ cat file2
Roses are red.
ek@Ilex:~$ cat file1 >> file2
ek@Ilex:~$ cat file2
Roses are red.
Violets are blue.
You can redirect the content of text1.txt using the cat
command:
~# cat /root/Documents/text1.txt > /root/Documents/text2.txt
Note: you can use cat
to also create new binary files, e.g:
~# cat mypic.jpg > my_new_pic.jpg
To expand on @SylvainPineau's answer, the reason you can't do /root/Documents/text1.txt > /root/Documents/text2.txt
is that the thing separate from the redirect operator and the file after it has to be a command. When you execute /root/Documents/text1.txt > /root/Documents/text2.txt
you are telling the shell to execute /root/Documents/text1.txt
, which probably won't even run since text files shouldn't have the execute flag set, and save standard output to /root/Documents/text2.txt
. Since standard output from that "command" is empty the target file will be empty.