Don't Repeat Yourself (in Command Line)
hy you should not repeat yourself
Andrew Hunt?and?David Thomas?in their Book : The Pragmatic Programmer have introduced DRY (Don't Repeat Yourself)?as a?principle?of?software development?aimed at reducing repetition of software patterns,?replacing it with abstractions or using?data normalization?to avoid redundancy. This article only capture the simplest DRY principle as simple as not to re-type the same command more than once.
Command Line Interface (CLI) is the oldest way of human communicate with computer, yet this is the most effective way of doing things as this can be very specific, effective and fast, and this is the preference of many professionals. In summary of comparison between CLI and GUI, the CLI provide speed and control for professionals while GUI is user friendly for most of common user.
CLI require user to type the command and execute them, this require some effort of typing and memorize all of the commands, but don't worry, most of modern CLI have the following features to overcome this :
How to deal with multiple tasks in Command Line
By default CLI require a command to be typed specifically for one task, but there are another way to perform multiple tasks in one command. In this article we use some of the most simple examples for window CMD command, where in Unix(or other Unix like OS) bash have more advance scripting language to perform multiple tasks.
To perform multiple task in windows command line, there are some options available depending on the nature of the tasks, to perform same operation to multiple files we can use pattern recognition (in Windows mostly glob patern) , loop operation, or pipe operation combined with other tools. We may need to extra careful in using glob is not as precise as regex to avoid unwanted, deletion, move or renaming.
Example 1 : Copy one file into multiple copy files
the original command are typed individually to each designated copy file
> copy File CopyFile-1
> copy FIle CopyFile-2
.
.
> copy File CopyFile-10
Using Loop
FOR \L %g IN (1,1,10) DO @copy File FileCopy-%g
Another way of doing this using sequence command (seq) and using pipe ( | ) to be executed using CMD. Pipe is part of redirecting mechanism to transfer an output of a command into a file or into terminal do be executed, "command | cmd" in windows is to send the output of previous command to be executed in CMD.
> seq -f "copy File CopyFile-%g" 10 | cmd
Example 2 : renaming multiple files
The original individual command
领英推荐
> rename File1 NewFile1
> rename File2 NewFile2
.
.
> rename File10 NewFile10
Using Loop
> For /F %g IN ("dir/b File*") DO @rename %g New%g
Using combined awk and pipe. AWK is s a?domain-specific language?designed for text processing, originally part of unix ecosystem that can installed in window by cygwin.
> dir/b File* | awk "{print \"rename \"$0,\"New\"$0}" | cmd
I would not recommend to use globe as the result may not as expected :
> rename File* NewFile* [DON'T USE THIS]
Example 3 : deleting multiple files
In deleting multiple file, instead of doing this :
> del File1
> del File2
.
.
> del File10
We can use glob as long as we can ensure the glob will capture the intended name pattern
> del File* [CAUTION!]
Please note that * can represent anything, therefore not only File1...File10, but also capture FileAnything, FileNothing or File
The safe option would be a combination of awk and pipe operation so we can use regex to only capture File1...File10 without include FileAnything, FileNothing, or File
> dir/b File* | awk "{/[0-9]/ print \"del \"$0}" | cmd
Windows user can also use PowerShell as alternative to CMD, PowerShell is s a task automation and?configuration management?program from?Microsoft, consisting of a?command-line?shell?and the associated?scripting language. As it is created to compete with Linux/Unix bash environment, it is more comprehensive, structured, and powerful, and performing multiple task in PowerShell is not an issue compare to CMD. However, I personally prefer to use CMD combined with Linux tools as I found the command is more compact and simple.
You can find all of command line reference for various systems in SS64
Manager (CMPE Cluster 2) at PETRONAS
3 年Idola, haha