在 Linux 中循环文件是一项方便的技能。它可以帮助您更轻松地管理和组织文件。在本教程中,我们将学习如何使用 Bash 循环文件。
我们将看看如何使用for loop迭代常规文件、名称中带有空格的文件、目录、忽略大小写等等。
在本教程结束时,您将学习如何循环不同类型的文件和模式。
for file in /path/to/files/* 模式
Command:
for file in /path/to/files/*; do
echo "$file"
done
Output:
/path/to/files/file1.txt
/path/to/files/file2.jpg
/path/to/files/file3.doc
...
该脚本使用 shell 通配机制。
The /path/to/files/*
模式匹配目录中的每个文件/path/to/files/
.
对于目录中的每个文件,回显命令打印控制台的完整路径。
循环继续,直到处理完所有文件。结果是指定目录中每个文件的列表。
循环常规文件
Bash 中有各种通配符可以帮助您定位特定的文件类型或模式。让我们探讨一些最常用的。
使用 (*)
Command:
for file in /path/to/files/*.txt; do
echo "$file"
done
Output:
/path/to/files/file1.txt
/path/to/files/file2.txt
...
The *
字符匹配任何字符串,允许您根据特定扩展名或命名约定过滤文件。
使用 (?)
Command:
for file in /path/to/files/file?.txt; do
echo "$file"
done
Output:
/path/to/files/file1.txt
/path/to/files/file2.txt
...
The ?
字符匹配任何单个字符,提供了一种定位具有特定命名结构的文件的方法。
使用 ([])
Command:
for file in /path/to/files/file[1-3].txt; do
echo "$file"
done
Output:
/path/to/files/file1.txt
/path/to/files/file2.txt
/path/to/files/file3.txt
通过使用[]
,您可以匹配特定字符或一系列字符,从而使文件定位更加精确。
带 for 的 find 命令(递归循环)
Linux查找命令允许您根据各种条件搜索文件或目录。
通过结合find
与for
循环,您可以有效地迭代嵌套目录中的文件。
命令:
for file in $(find /path/to/files/ -type f); do
echo "$file"
done
Output:
/path/to/files/subdir/file4.txt
/path/to/files/file1.txt
/path/to/files/file3.doc
...
The find
命令用于检索所有常规文件(-type f
) 之内/path/to/files/
.
结果来自find
然后由for
环形。这echo
命令将每个文件的完整路径打印到控制台。
循环继续,直到找到所有文件find
被处理。
处理带有特殊字符的文件名
在 Bash 中循环文件时,特殊字符(尤其是空格)可能会造成问题。让我们来应对这一挑战。
命令:
IFS=$'\n'
for file in $(find /path/to/files/ -type f); do
echo "$file"
done
Output:
/path/to/files/My Document.txt
/path/to/files/Program #123.log
...
通过将内部字段分隔符 (IFS) 设置为换行符。
我们指示 shell 仅将换行符视为分隔符,忽略空格。
这可确保带空格的文件名不会拆分为多个条目。
结果,for
循环可以迭代带有空格或其他特殊字符的文件名,没有任何问题。
循环目录
有时,您需要循环目录而不是文件。具体做法如下:
命令:
for dir in /path/to/files/*/; do
echo "$dir"
done
Output:
/path/to/files/directory1/
/path/to/files/directory2/
...
尾随的/
之后*
通配符确保您只匹配目录。
每个匹配目录的路径都会打印到控制台。循环继续,直到处理完指定位置的所有目录。
循环符号链接
符号链接(或符号链接)本质上是对其他文件或目录的引用。
命令:
for link in $(find /path/to/files/ -type l); do
echo "$link"
done
Output:
/path/to/files/link_to_file1.txt
/path/to/files/link_to_directory2/
...
我们使用find
命令与-type l
仅针对符号链接的选项。
结果来自find
均由for
环形。每个符号链接的路径都会打印到控制台。
混合文件类型
您可以循环遍历常规文件、目录和符号链接的混合。让我们看看如何做到这一点。
命令:
for item in /path/to/files/*; do
if [[ -f "$item" ]]; then
echo "File: $item"
elif [[ -d "$item" ]]; then
echo "Directory: $item"
elif [[ -L "$item" ]]; then
echo "Symlink: $item"
fi
done
Output:
File: /path/to/files/file1.txt
Directory: /path/to/files/directory1/
Symlink: /path/to/files/link_to_file1.txt
...
该循环迭代目录中的所有项目。条件句 (if
, elif
) 用于确定每个项目的类型。
根据项目的分类打印项目的类型和路径。
循环特定文件扩展名
有时,您需要循环遍历具有特定扩展名的文件。处理某些类型的数据或文件格式时通常会出现这种情况。
命令:
for file in /path/to/files/*.{txt,jpg,png}; do
echo "$file"
done
Output:
/path/to/files/file1.txt
/path/to/files/image1.jpg
/path/to/files/picture1.png
...
大括号{}
允许您一次匹配多个文件扩展名。
每个文件都匹配扩展名.txt
, .jpg
, and .png
打印到控制台。
循环继续进行,直到处理完所有匹配的文件。
区分大小写和匹配
默认情况下,Bash 区分大小写。但是,在某些情况下,您希望循环遍历文件,无论其大小写如何。
命令:
shopt -s nocaseglob
for file in /path/to/files/*.[Tt][Xx][Tt]; do
echo "$file"
done
shopt -u nocaseglob
Output:
/path/to/files/file1.TXT
/path/to/files/file2.txt
...
The shopt -s nocaseglob
命令用于关闭通配符的区分大小写。
图案*.[Tt][Xx][Tt]
匹配小写和大写变体.txt
files.
每个匹配文件的路径都会打印到控制台。最后,使用返回默认的区分大小写shopt -u nocaseglob
.
忽略隐藏文件
如果您想处理文件同时排除隐藏文件。以下是如何实现这一目标:
命令:
for file in /path/to/files/*; do
if [[ ! $file =~ ^..* ]]; then
echo "$file"
fi
done
Output:
/path/to/files/file1.txt
/path/to/files/image1.jpg
...
该循环迭代目录中的所有项目。这if
语句检查文件名是否不以点开头(.
).
如果文件未隐藏,其路径将打印到控制台。
使用正则表达式
Bash 支持正则表达式,为高级模式匹配提供了强大的手段。
命令:
for file in /path/to/files/*; do
if [[ $file =~ ^/path/to/files/file[0-9]+.txt$ ]]; then
echo "$file"
fi
done
Output:
/path/to/files/file1.txt
/path/to/files/file23.txt
...
循环遍历中的每个项目/path/to/files/
.
The if
语句检查文件是否与正则表达式模式匹配^/path/to/files/file[0-9]+\.txt$
.
文件名为file
后跟一位或多位数字并以.txt
匹配这个模式。