Don't Repeat Yourself (in Command Line)
Screenshot by Author

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 :

  1. Auto Completion, in most of operating system, Tab key is short shortcut to this feature, so some incomplete command follow by Tab will give the most likely command, by keep pressing tab the computer will give all possible command and match with previous input.
  2. Previous Command, terminal will remember all command that have been executed , and using arrow up (and down) we can recall all previous command that have been executed in that session.

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

Nicholas Lian Mujah

Manager (CMPE Cluster 2) at PETRONAS

3 年

Idola, haha

回复

要查看或添加评论,请登录

Purnomo Setyawendha的更多文章

  • Minkowski distance in R vs Python

    Minkowski distance in R vs Python

    Which one you prefer ? in R or python (credit to ChatGPT for creating code example)

  • Why R is my first language

    Why R is my first language

    Hopefully this is not about Python vs R (again) The motivation for this article as I found most of R vs Python…

    2 条评论
  • Why I love stream editor (sed)

    Why I love stream editor (sed)

    We have situation here ..

  • Managing (Technical) Disagreements

    Managing (Technical) Disagreements

    Human is a very complex creature by design, and diversity is imbedded into our DNA combined with background and…

    1 条评论
  • Introduction to Stream Editor (sed)

    Introduction to Stream Editor (sed)

    Following to my previous sed story : I write this article to help you to start with sed and enjoy the speed…

  • Starting with a Boring Stuff

    Starting with a Boring Stuff

    On my starting point in python three years ago, I was too excited, cannot hardly wait to jump into machine learning and…

  • Geopandas Update for Python 3.8.5

    Geopandas Update for Python 3.8.5

    I just recently update my python to 3.8.

    1 条评论
  • A "sed" Story for Pipeline Integrity Engineer

    A "sed" Story for Pipeline Integrity Engineer

    "sed" story is a story about surviving of an old tools, if we refer to "Jadul" term in Indonesian urban dictionary…

    2 条评论
  • How slow R ....U ?

    How slow R ....U ?

    I have to put disclaimer since R was my first language so I may get biased. It is depend on what you want to do, but as…

    2 条评论
  • Recent changes in R spatial

    Recent changes in R spatial

    I think this is a very important update for all R-Spatial community member : sf, which replaces sp terra, which aims to…

    1 条评论

社区洞察

其他会员也浏览了