Ok. The if statement can be used to test an expression enclosed in square bracket or to test the result of a command, as in my example. You know about the exit code of a command? Any command in linux has an exit code, which is usually 0 if the command had success, a value different than 0 (in most cases 1) if the command failed.
You can retrieve the exit code explicitly using
$ echo $?
where $? is a special variable storing the exit code of the previous command.
The if ls
statement test the result of the ls command based on its exit code: if it is successful the commands inside the if/then block are executed, otherwise they are skipped. The
just redirect the standard output to /dev/null, which is a sort of black hole in unix systems. This prevent the output to be shown in the terminal, since you're not really intersted to visualize the list of files. The 2>&1 just redirect the standard error to the standard output, so that even the error message (when no file is found) is not displayed. Hope it is clear now.