here's a php script that writes a file named using the unix timestamp:
#!/usr/bin/env php
<?php
file_put_contents("file" . ((int) microtime(true)) . ".txt", file_get_contents("/proc/cpuinfo"));
?>
if you want fill the file from i.e. stdin, change /proc/cpuinfo to php://stdin
here's a shell script that does the same as the original php script
#!/bin/sh
cat /proc/cpuinfo | tee "file`date +%s`.txt" > /dev/null
you can chmod +x both of these and execute them
Pipe is used to pass output to another program or utility.
Redirect is used to pass output to either a file or stream.
Example: thing1 > thing2
vs thing1 | thing2
thing1 > thing2
- Your shell will run the program named
thing1
- Everything that
thing1
outputs will be placed in a file called thing2
. (Note - if thing2
exists, it will be overwritten)
If you want to pass the output from program thing1
to a program called thing2
, you could do the following:
thing1 > temp_file && thing2 < temp_file
which would
- run program named
thing1
- save the output into a file named
temp_file
- run program named
thing2
, pretending that the person at the keyboard typed the contents of temp_file
as the input.
However, that's clunky, so they made pipes as a simpler way to do that. thing1 | thing2
does the same thing as thing1 > temp_file && thing2 < temp_file
EDIT to provide more details to question in comment:
If >
tried to be both "pass to program" and "write to file", it could cause problems in both directions.
First example: You are trying to write to a file. There already exists a file with that name that you wish to overwrite. However, the file is executable. Presumably, it would try to execute this file, passing the input. You'd have to do something like write the output to a new filename, then rename the file.
Second example: As Florian Diesch pointed out, what if there's another command elsewhere in the system with the same name (that is in the execute path). If you intended to make a file with that name in your current folder, you'd be stuck.
Thirdly: if you mis-type a command, it wouldn't warn you that the command doesn't exist. Right now, if you type ls | gerp log.txt
it will tell you bash: gerp: command not found
. If >
meant both, it would simply create a new file for you (then warn it doesn't know what to do with log.txt
).
Best Answer
You can't use
file1 > file2
to copyfile1
's contents tofile2
because there's no command there. You have to issue some command.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: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 thecp
command.As Sylvain Pineau says, you can use
cat file1 > file2
as well. However, thecp
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 thinkcat file1 > file2
were needed):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
tofile2
instead of overwritingfile2
with the contents offile1
, a redirection with>>
(not>
) is a good choice: