I am trying to figure out what this command is doing:
rm OUTPUT/* >> log_${ENV} 2>&1
I am trying to figure out what this command is doing:
rm OUTPUT/* >> log_${ENV} 2>&1
the rm OUTPUT/*
deletes all files in the OUTPUT
Folder. >>
appends the output of the command to a file called log_${ENV}
. This does normally only redirect messages from stdout (rm without the -v
parameter shouldn't write anything to stdout).
The 2>&1
redirects the stream 2 (this is stderr where rm writes its error messages) to stream 1 (stdout) which is redirected to the file.
So this command does remove the files and writes the errors into the logfile if there are any.
The command is identical to
rm OUTPUT/* 2>>log_${ENV}
As in your other question, the curly braces won't do anything special here. See also my answer there about ENV
.
The command removes everything that OUTPUT/*
matches. If there are any errors, these are appended to the given output file.
Depending on the shell and what shell options are set, the OUTPUT/*
pattern may in addition to all the names in the OUTPUT
directory, also match the names of hidden files and directories, or it may not.
The rm
utility will not produce any output on its standard output (which is why the command may be rewritten as above), but may give errors when deleting files that don't exist or that the current user does not have permission to delete, or if it tries to delete directories (without -r
). These error messages would be appended to the given file. It's the use of >>
(rather than >
) that makes the shell open the output file for appending rather than for rewriting.
In the original command, >>log_${ENV} 2>&1
would instruct the shell to open the file for appending and send the rm
utility's standard output stream to it. The shell would then also, with the 2>&1
bit, send the standard error stream to the same place. The numbers 1
and 2
indicate the standard output stream and the standard error stream respectively, and when no number is used, the standard output stream is implied.
rm OUTPUT/*
removes all files under the OUTPUT folder
>> log_${ENV}
appends the output to the file log_${ENV}
2>&1
redirects STDERR to STDOUT
References: [1]
-v
parameter to show what was removed.
– mrc02_kr
Oct 10 '18 at 11:04
rm
has a -v
option that does that.
– Kusalananda
Oct 10 '18 at 12:47
${ENV}
, it means an environment variable namedENV
! – peterh Oct 10 '18 at 23:19OUTPUT
(ifOUTPUT
has some sub-directories, therm
will fail with an error message) 2) logging all output, including the errors, into the fileLOG_
suffixed by the value of the environment variableENV
. Note also, the 2 reasons I can imagine to use an environment variableENV
: 1) some very beginner programmer seriously misunderstood something and wrote a VLQ script 2) it is a malicous code and its author tried to make it looking harmless for uncareful supervision. – peterh Oct 10 '18 at 23:23rm
doesn't give any output for a successful deletion! There won't be anything in the logfiles! He should have written at least some likerm -rvf OUTPUT/* >>log_${batchId} 2>&1
, and its author will probably write it on this way - after some years of unix experience. – peterh Oct 10 '18 at 23:25rm
was aliased torm -vr
or something, the code might be more useful. Perhaps prior code might provide better context. – agc Oct 11 '18 at 00:55rm
is aliased tocp
which will result they copying of all files into the alphabetically last directory entry, if it is a directory (else error message). It is possible, but it is not in the question :-) I think the questions should be interpreted always as default setting. Scripts typically ignore the aliases (maybe bash have same flag against it). – peterh Oct 11 '18 at 01:25