Industry News by Thomas Garcia Jul 5, 2018 Learning Linux: Running More Than One Command When administering a system through Linux, many times you’ll enter commands one at a time into terminal to review the output or to complete an action. However, there are times when you’ll want to run more than one command in one go. With Linux, you can string together as many commands as you’d like using ;. Let’s say you want a record of file information in a specific directory. You want to include the date, the full directory path and you want the listing in long format (which includes information like file ownership, permissions, size, symbolic links and so forth). You can do this in one entry like this: (date; pwd; ls -al) > listing.txt. This will run the commands in the parentheses (technically, in a subshell), and redirect the contents into the listing.txt file, which will be created automatically. It’s important to note if that file exists already, it will be overwritten by default. The commands in the parentheses will run in order, outputting the date, the full directory path and then list the contents in long format. An important thing to note about using ; is that the commands will run in order regardless if any of them fail. In the above commands, if you accidentally enter (dat3; pwd; ls -al) > listing.txt, you’ll get an error output to terminal telling you that dat3 isn’t a command. However, the rest will complete successfully and redirect to the file, and you’ll get a file just listing the current working directory and long format listing. If you don’t want the rest of the commands to complete once a failed command is reached, you would use && instead. To simplify the example, if you run ls && pwf && date, only the first command will complete; since pwf isn’t a command, it will output an error, and subsequent commands won’t run. This can be helpful if you need to run multiple commands in a row, where one is dependent on another. Alternatively, if you need only the first successful command to run, you could use ||. For example, date || ls will only run the the first command (as it will successfully complete), whereas with dat3 || ls, the second command will run since the first will fail (although it will still output an error to the console). It’s important to note that if you daisy chain a string of commands this way, it will stop at the first command that completes successfully. This can be helpful if you have a series of scripts, where one should run if another fails, and so on. These simple tips may not be used frequently, but can be really convenient if you need to string together a series of commands, or need a series of commands to complete in a certain order. That way, you won’t have to run them one by one, saving time and streamlining the process. Stay tuned for my next post for more tips!