xargs 用于创建目录时导致错误?

xargs 用于创建目录时导致错误?

我想创建大约 100 个具有唯一名称的目录,这些目录都包含一个 README.md 文件。我尝试使用包含以下内容的文本文件“PICOCTF.txt”执行此操作:

vault-door-1
whats a net cat
strings it
Easy1
logon
13
caesar
dont-use-client-side
Bases
First Grep
So Meta
shark on wire 1
extensions
What Lies Within
la cifra de
picobrowser
asm1
Tapping
Flags
Based
Mr-Worldwide
plumbing
vault-door-3
rsa-pop-quiz
Client-side-again
Pitter, Patter, Platters
Web Gauntlet
asm2
m00nwalk
WhitePages
c0rrupt
vault-door-4
like1000
Guessing Game 1
droids0
mus1c
m00nwalk2
waves over lambda
messy-malloc
flag_shop
Investigative Reversing 0
asm3
miniRSA
vault-door-5
reverse_cipher
Irish-Name-Repo 1
shark on wire 2
Guessing Game 2
OTP Implementation
Investigative Reversing 2
droids1
Investigative Reversing 1
WebNet0
vault-door-6
seed-sPRiNG
Irish-Name-Repo 2
1_wanna_b3_a_r0ck5tar
b00tl3gRSA2
asm4
Irish-Name-Repo 3
JaWT Scratchpad
Java Script Kiddie
Need For Speed
Investigative Reversing 4
B1ll_Gat35
droids2
vault-door-7
AES-ABC
Investigative Reversing 3
vault-door-8
droids3
Java Script Kiddie 2
WebNet1
investigation_encoded_1
b00tl3gRSA3
john_pollard
droids4
Forky
investigation_encoded_2
sice_cream
B1g_Mac
zero_to_hero

在命令行和文本文件的目录中,我运行命令来生成目录:

xargs -a PICOLIST.txt -d '\n' mkdir

或(结果相同)

while read -r line; do mkdir -p "$line" ; done < PICOLIST.txt

它确实可以创建它们,只有一个小问题。我知道它只为当前目录创建目录,但是当我运行它时,我得到的文件末尾有一个(空?)特殊字符。我不知道这是否是 Windows 文件系统的问题,因为我已经格式化了我的文本文件以删除任何可能导致该问题的内容(至少我认为如此)。例如:

这是在命令运行之前:

在此处输入图片描述

这是运行命令之后的情况:

在此处输入图片描述

如您所见,每个文件夹名称后都有一个特殊字符(星号?空字节?在 xargs 中解读新行时出现问题?)...

我的计划是在终端中运行以下代码,但使用此文本文件:

xargs -a PICOLISTFILL.txt -d '\n' touch
vault-door-1/README.md
whats a net cat/README.md
strings it/README.md
Easy1/README.md
logon/README.md
13/README.md
caesar/README.md
dont-use-client-side/README.md
Bases/README.md
First Grep/README.md
So Meta/README.md
shark on wire 1/README.md
extensions/README.md
What Lies Within/README.md
la cifra de/README.md
picobrowser/README.md
asm1/README.md
Tapping/README.md
Flags/README.md
Based/README.md
Mr-Worldwide/README.md
plumbing/README.md
vault-door-3/README.md
rsa-pop-quiz/README.md
Client-side-again/README.md
Pitter, Patter, Platters/README.md
Web Gauntlet/README.md
asm2/README.md
m00nwalk/README.md
WhitePages/README.md
c0rrupt/README.md
vault-door-4/README.md
like1000/README.md
Guessing Game 1/README.md
droids0/README.md
mus1c/README.md
m00nwalk2/README.md
waves over lambda/README.md
messy-malloc/README.md
flag_shop/README.md
Investigative Reversing 0/README.md
asm3/README.md
miniRSA/README.md
vault-door-5/README.md
reverse_cipher/README.md
Irish-Name-Repo 1/README.md
shark on wire 2/README.md
Guessing Game 2/README.md
OTP Implementation/README.md
Investigative Reversing 2/README.md
droids1/README.md
Investigative Reversing 1/README.md
WebNet0/README.md
vault-door-6/README.md
seed-sPRiNG/README.md
Irish-Name-Repo 2/README.md
1_wanna_b3_a_r0ck5tar/README.md
b00tl3gRSA2/README.md
asm4/README.md
Irish-Name-Repo 3/README.md
JaWT Scratchpad/README.md
Java Script Kiddie/README.md
Need For Speed/README.md
Investigative Reversing 4/README.md
B1ll_Gat35/README.md
droids2/README.md
vault-door-7/README.md
AES-ABC/README.md
Investigative Reversing 3/README.md
vault-door-8/README.md
droids3/README.md
Java Script Kiddie 2/README.md
WebNet1/README.md
investigation_encoded_1/README.md
b00tl3gRSA3/README.md
john_pollard/README.md
droids4/README.md
Forky/README.md
investigation_encoded_2/README.md
sice_cream/README.md
B1g_Mac/README.md
zero_to_hero/README.md

我使用的两个文本文件都是用 notepad++ 制作的,设置为“Unix (LF)”
在此处输入图片描述

任何帮助将不胜感激!

答案1

假设:PICOLIST.txt使用 DOS/Windows 行尾 (CRLF,\r\n)。

您尝试的两个命令(read, xargs)都要求 Unix 行尾(LF, \n)。它们被\r视为常规字符。在您的情况下,每次都\r包含在传递给的字符串中。mkdir

用于dos2unix转换文件。

如果您必须在 Windows 中编辑此类文件,请使用允许您选择 Unix 行尾的编辑器。具有此功能的编辑器可用于dos2unix修复当前文件。

或许PICOLISTFILL.txt同样存在“缺陷”。

答案2

如果您希望保留 txt 文件的原始格式,您可以在 xargs 命令前添加前缀:

cat PICOLISTFILL.txt | dos2unix | xargs ... 

如果这样做的话,xargs 部分将不需要“-a”或“-d”标志。

相关内容