bat命令行遍历文件
Looping is one of the most powerful things you can do in programming. It allows you to apply the same logic over and over to a group of items with minimal code. If done properly, infinite loops can be a blessing instead of a curse. Unfortunately though, not all command-line utilities allow you to run a command against multiple files, but with the power of shell scripting and the for
loop, we can super charge any command we choose!
循环是您可以在编程中执行的最强大的功能之一。 它允许您以最少的代码一遍又一遍地将相同的逻辑应用于一组项目。 如果处理得当,无限循环可能是一种福气,而不是诅咒。 不幸的是,虽然并非所有命令行实用程序都允许您针对多个文件运行命令,但是借助Shell脚本和for
循环的强大功能,我们可以for
我们选择的任何命令提供超强的功能!
入门 (Getting Started)
We are going to be using the for
shell scripting loop. This particular control flow method is baked into your command-line shell, but it’s recommended that you use either Bash or Z shell for the greatest compatibility with the syntax below.
我们将使用for
shell脚本循环。 这种特殊的控制流方法已包含在命令行外壳程序中,但是建议您使用Bash或Z Shell程序,以最大程度地与以下语法兼容。
The following commands can be applied to any directory of your choosing, but for the sake of example, and in case you want to get crazy and try to manipulate your files without a backup, let’s go ahead and create a directory and some files to play around with:
以下命令可以应用于您选择的任何目录,但出于示例的目的,如果您想发疯并尝试在没有备份的情况下操作文件,让我们继续创建目录并播放一些文件周围有:
$ mkdir /tmp/alliloop
$ cd /tmp/alliloop
$ touch file-{1..5}.txt
遍历文件 (Looping Through Files)
Okay, so this example will basically be the same as using ls
in a directory, but trust me, we’re going to build upon this. To loop through a directory, and then echo
the name of the file you can run:
好的,因此该示例基本上与在目录中使用ls
相同,但是请相信我,我们将在此基础上进行构建。 要遍历目录,然后echo
显可以运行的文件名,请执行以下操作:
$ for FILE in *; do echo $FILE; done
file-1.txt
file-2.txt
file-3.txt
file-4.txt
file-5.txt
Not much to it. You probably noticed we’re using the wild card character, *
, in there. That tells the for
loop to grab every single file in the directory. The wild card could just as easily be changed to file-*
to target all of the files that started with file-
or *.txt
to grab just the text files.
没什么。 您可能已经注意到我们在其中使用通配符*
。 这告诉for
循环抓取目录中的每个文件。 通配符可以很容易地更改为file-*
以将所有以file-
开头的file-
或*.txt
为仅捕获文本文件。
应用命令 (Applying a Command)
Now that we know how to loop through the files in a directory and spit out the names, let’s try something a bit more advanced. The files we created in the getting started section were all created with the touch
command and are empty, so showing out how to cat
each file wouldn’t be very eventful.
现在,我们知道了如何遍历目录中的文件并吐出名称,让我们尝试一些更高级的方法。 我们在入门部分中创建的文件都与创建的touch
命令是空的,所以呈现出怎样cat
每个文件不会很多事。
Fortunately, using a similar for
loop, we can insert text into each of our files with ease:
幸运的是,使用类似的for
循环,我们可以轻松地在每个文件中插入文本:
$ for FILE in *; do echo "$FILE\nAlligator's Rule\!" > $FILE; done
$ for FILE in *; do cat $FILE; done
file-1.txt
Alligator's Rule!
file-2.txt
Alligator's Rule!
file-3.txt
Alligator's Rule!
file-4.txt
Alligator's Rule!
file-5.txt
Alligator's Rule!
Now each of our files contains the name of the file on the first line, and a universal truth about alligator’s on the second line.
现在,我们的每个文件在第一行中都包含文件名,在第二行中包含有关鳄鱼的通用真理。
To take it a step further, we could also combine these examples to first write to the file, then cat
it’s contents in a single loop:
要采取这一步,我们也可以这些例子结合起来,首次写入该文件,然后cat
它在一个循环的内容:
$ for FILE in *; do echo "$FILE\nAlligator's Rule\!" > $FILE; cat $FILE; done
file-1.txt
Alligator's Rule!
file-2.txt
Alligator's Rule!
file-3.txt
Alligator's Rule!
file-4.txt
Alligator's Rule!
file-5.txt
Alligator's Rule!
By separating our commands with a semi-colon, ;
, we’re able to string together whichever commands we’d like to.
通过用分号,分隔命令;
,我们可以将我们想要的任何命令串在一起。
进行备份 (Making backups)
Now that we know the ropes of how the for
loop works, let’s try something a bit more real world by taking a directory of files, and making backups that are suffixed with the .bak
extension:
现在,我们了解了for
循环的工作原理,让我们通过获取文件目录并进行带有.bak
扩展名后缀的备份来尝试一些更实际的事情:
$ for FILE in *; do cp $FILE "$FILE.bak"; done;
$ ls -l
total 40K
-rw-r--r-- 1 josh josh 29 Nov 7 18:34 file-1.txt
-rw-r--r-- 1 josh josh 29 Nov 7 18:41 file-1.txt.bak
-rw-r--r-- 1 josh josh 29 Nov 7 18:34 file-2.txt
-rw-r--r-- 1 josh josh 29 Nov 7 18:41 file-2.txt.bak
-rw-r--r-- 1 josh josh 29 Nov 7 18:34 file-3.txt
-rw-r--r-- 1 josh josh 29 Nov 7 18:41 file-3.txt.bak
-rw-r--r-- 1 josh josh 29 Nov 7 18:34 file-4.txt
-rw-r--r-- 1 josh josh 29 Nov 7 18:41 file-4.txt.bak
-rw-r--r-- 1 josh josh 29 Nov 7 18:34 file-5.txt
-rw-r--r-- 1 josh josh 29 Nov 7 18:41 file-5.txt.bak
We now have exact copies of each of our files, with an extension that indicates that they are backup files!
现在,我们有了每个文件的精确副本,并带有扩展名指示它们是备份文件!
You’re not limited to making copies in the same directory either. You could just as easily specify a new path for your backup files:
您也不仅限于在同一目录中进行复制。 您可以轻松地为备份文件指定新路径:
$ for FILE in *; do cp $FILE "/tmp/my-backups/$FILE.bak"; done;
翻译自: https://www.digitalocean.com/community/tutorials/workflow-loop-through-files-in-a-directory
bat命令行遍历文件