Ubuntu – How to send a file as an input to another file

command lineredirectscripts

Through the command shell we can redirect the three standard filestreams so that we can get input from either a file or another command instead of from our keyboard, and we can write output and errors to files or send them as input for subsequent commands.

So why can't we give input to an other file as

~# /root/Documents/text1.txt > /root/Documents/text2.txt

Best Answer

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 copy file1 to file2. Instead, as l0b0 says, it attempts to run file1 as a program or script, and--whether or not that succeeds--sends the standard output from running file1 into file2. (If file2 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 and file2 is used as output... but since nothing is actually done, the output is empty, the only effect is to (a) create file2 if it didn't already exist, or (b) make file2 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.