【问题标题】:Copying no of files from one folder to another in Mac using terminal使用终端在Mac中将文件从一个文件夹复制到另一个文件夹
【发布时间】:2020-09-26 01:29:06
【问题描述】:

我正在尝试关注有关机器学习的博客。在博客中,作者使用 linux 命令将一定数量的文件从一个文件夹传输/复制到另一个文件夹。我在 Mac 上工作,我试图通过根据 mac 环境(我的知识有限)修改命令来运行命令,但无法将它们复制到其他文件夹。这是从博客复制的Linux命令

#Transfer data into the specific directory
echo "---Parasitized---"
cd Parasitized_all
cp `ls Parasitized_all | head -5000` ../Parasitized_train
cp `ls Parasitized | tail -n+5001 | head -5000 | wc -l` ..Parasitized_validation
cp `ls Parasitized | tail -n+5001 | tail -3779 | wc -l` ..Parasitized_test

例如,我尝试将上述语句的第一个复制命令复制到 mac 中,如下所示:

cp `Documents/folder_directory/Parasitized_all | head -5000` Documents/folder_directory/Parasitized_train

但是,它不起作用。有没有人可以指导我找出我所犯的错误?

【问题讨论】:

  • 根据您给出的示例,您应该谨慎对待此博客。遵循它需要您自担风险。上面展示了许多不良做法。
  • 请注意:Why not parse ls?

标签: linux bash macos terminal


【解决方案1】:

当你运行这个时,故障点就来了:

cp `Documents/folder_directory/Parasitized_all | head -5000`

因为你正在尝试做一件事,而 shell 正在理解另一件事。通过运行head 5000,您想读取该文件Documents/folder_directory/Parasitized_all并将该列表作为源,然后复制与它的前5.000行一致的那些文件,但cp不能那样工作,它只能理解@ 987654325@。而且,它甚至不是一个文件,而是一个目录。

该命令与ls Parasitized_all | head -5000 之间的区别在于,最后一个命令的输出将是一个文件列表,可用作将它们复制到目标路径的源。

也就是说,请注意,在您运行的命令中,您缺少开头的 ls,因此文件列表不会出现。所以,尝试运行它(我从 ls 更改为 find 只是因为我更喜欢使用它):

cp $(find Documents/folder_directory/Parasitized_all -type f | head -5000) Documents/folder_directory/Parasitized_train/

或者如果你遇到像argument list too long这样的错误:

for FILE in $(find Documents/folder_directory/Parasitized_all -type f | head -5000); do
    cp $FILE Documents/folder_directory/Parasitized_train/
done 

【讨论】:

  • 感谢您的回答,当我运行命令时,它会向我抛出以下消息:-bash: /bin/cp: Argument list too long
  • 在这种情况下,您可以尝试运行一个循环:for FILE in $(find Documents/folder_directory/Parasitized_all -type f | head -5000); do cp $FILE Documents/folder_directory/Parasitized_train/; done
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 2011-08-22
  • 2021-09-06
  • 2014-02-10
  • 2020-07-30
  • 2017-02-11
相关资源
最近更新 更多