-4

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

rm OUTPUT/* >> log_${ENV} 2>&1

closed as unclear what you're asking by user88036, JdeBP, RalfFriedl, Rui F Ribeiro, Toby Speight Oct 10 '18 at 13:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

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

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.

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]

  • By default rm doesn't write anything to ouput. You have to use -v parameter to show what was removed. – mrc02_kr Oct 10 '18 at 11:04
  • 1
    @mrc02_kr If the particular implementation of rm has a -v option that does that. – Kusalananda Oct 10 '18 at 12:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.