7

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

5 Answers 5

17

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.
8

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
2
  • 1
    The other answer seems to try its best to make the whole thing seem way too complicated. Yes, in this simplistic example you can just use cp. However, I interpret the OP's question as being about how to pipe from a file in general. And in that sense, this is the best answer. In short, cat is an essential, common command for lots of command line tasks. Sep 17, 2014 at 5:58
  • 1
    @neon_overload The question here is about redirection, not piping. (I'm not disagreeing with the part of your comment that says this answer is valuable--this answer does not confuse those two concepts.) A question about how to pipe from a file in general would need answers that talk about pipes (with |). A question about how to redirect from a file in general would need answers explaining how to redirect from files, with <, as nux's and Stephen Quan's answers do but which neither this answer nor mine does. Sep 17, 2014 at 9:13
1

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.

1
  • 1
    To be pedantic, the redirection can appear anywhere. For example > out date successfully writes the current time to the file "out". Sep 16, 2014 at 12:57
0

Command output Redirection :

ls > file.txt

ls gives a directory listing and the output of ls command is redirected to file.txt .

Error Redirection :

command 2>~/Desktop/errorsfile.txt

This will redirect the error result of command to a txt file.

For file content redirection :

  1. cat file1 > file2 (will copy the content of file1 to a new file file2)
  2. cat file2 >> file1 (here will copy content of file2 to an existing file file1 without deleting its content )
  3. cat file1 file2 > file3 (This would add the contents of ' file1 ' and ' file2 ' and then write these contents into a new file named ' file3 ' )

I would also like to add The Input Redirection :

$ mail -s "Hello World" [email protected] < /home/user/mailcontent.txt

Here if the mail body is in a file then we can use it directly to send the mail.

0

As others have already stated, you're missing a command. Here are some interesting combinations:

cat <file1.txt >file2.txt
tail <file1.txt >file2.txt
head <file1.txt >file2.txt
wc -l <file1.txt >file2.txt
base64 <file1.txt >file2.txt
base64 -D <file1.txt >file2.txt
gzip -c <file1.txt >file2.txt
gunzip -c <file1.txt >file2.txt

The spirit of piping/redirecting indicates that a command (such as copying, extracting, counting, encoding/decoding, compressing/decompressing) is needed to specify the action to transform the input file1.txt into output file2.txt.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .