-4

I am trying to figure out what this command is doing:

rm OUTPUT/* >> log_${ENV} 2>&1
Kusalananda
  • 333,661
juju
  • 75
  • Is the full stop part of the command? Please put it in the question body and format it with markup so that it is clear what is and is not the command. – JdeBP Oct 10 '18 at 11:18
  • It looks very unnice, even in a build script (it seems to be some build script, or some data processing script). Honestly, this command looks so ugly that I am not sure if it is not part of some malicious code. What is that ${ENV}, it means an environment variable named ENV! – peterh Oct 10 '18 at 23:19
  • Furthermore, this command is also false - what it tries to do: 1) removing all non-subdirectories in the directory OUTPUT (if OUTPUT has some sub-directories, the rm will fail with an error message) 2) logging all output, including the errors, into the file LOG_ suffixed by the value of the environment variable ENV. Note also, the 2 reasons I can imagine to use an environment variable ENV: 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:23
  • The problem is that rm doesn't give any output for a successful deletion! There won't be anything in the logfiles! He should have written at least some like rm -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:25
  • Btw, I think your question is a good one, and the answer is enough objective to be not closed as "unclear" (probably all the unix pros will say roughly the same for that), but it seems I represent here a minority opinion. Remain on the site, your other questions might have a better luck in the future! – peterh Oct 10 '18 at 23:27
  • @peterh, If rm was aliased to rm -vr or something, the code might be more useful. Perhaps prior code might provide better context. – agc Oct 11 '18 at 00:55
  • @agc Yes, and it is also possible that rm is aliased to cp 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

3 Answers3

2

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.

bsod
  • 21
2

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.

Kusalananda
  • 333,661
0

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]

agc
  • 7,223
xergon
  • 1