bat命令行遍历文件_命令行基础知识:如何遍历目录中的文件

news/2024/7/5 8:00:10

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命令行遍历文件


http://www.niftyadmin.cn/n/3649361.html

相关文章

ndk的入门

第一个ndk 第一步:MainActivity中定义一个方法: public static native String getStringFromC(); 第二步生成头文件: ①我的项目位置:E:\android\HelloNdk ②我的sdk中android.jar包的位置:G:\android\AndroidX86\AndroidX86\sdk\platfor…

CentOS 7安装并配置Nginx

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使…

spring react_如何使用React Spring创建动画的React应用

spring react介绍 (Introduction) Animations add life to your applications and improve the overall user experience. React Spring is an animation package that uses spring-like physics in its core animations to make it easily configurable. Springs are cumulati…

[GOLF]过了磨合期了

今日到了4S店,换了三滤,检查了胎压和空调。换了三滤果然很见效,回去的路上,就觉得爱车凶猛了许多,很给劲儿。第一次开着车去了王府井,并且第一次停到了收费的地下停车场,蜿蜒着下到地下&#xf…

CentOS 7搭建PHP环境

PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。利于学习,使用广泛,主要适用于Web开发领…

Android Context 详解

Android中context可以作很多操作,但是最主要的功能是加载和访问资源。 在android中有两种context,一种是application context,一种是activity context,通常我们在各种类和方法间传递的是activity context。 继承关系:…

如何在JavaScript中替换字符串的所有实例

介绍 (Introduction) Replacing text in strings is a common task in JavaScript. In this article you’ll look at using replace and regular expressions to replace text. 替换字符串中的文本是JavaScript中的常见任务。 在本文中,您将研究如何使用replace和正…

[C#]I/O完成端口的实现

在VC中我几乎每一个Windows Service都是采用I/O完成端口。至于在C#中如何使用I/O完成端口,一直很少见人提及。William Kennedy的三篇文章《IOCP Thread Pooling in C#》,对实现这种机制很有帮助,唯一美中不足的是,它只能把int数值…