如何在 bash/zsh/ksh 中复制期间创建目录?
我经常收到以下消息,例如,当将开发文件复制到主分支时,
cp: /Users/Masi/gitHub/shells/zsh/dvorak: No such file or directory
cp: /Users/Masi/gitHub/shells/zsh/dvorak2: No such file or directory
我想询问有关给定文件夹的创建的信息,这样如果我对问题回答“是”,我的初始命令将运行。
当我尝试将文件复制到不存在的目录时,我尝试使用伪代码
if no such a directory exists, then asks users about to create it:
if yes, then mkdir directory AND run the initial command again
else do noting
问题
- 要更改警告消息:文件确实控制“没有这样的文件或目录”命令吗?
- 要抓取初始命令中的路径和没有文件的 mkidr 路径:如何抓取初始命令中的路径?
- 使用您选择的语言(例如 AWK)从末尾开始抓取:当 / 是字段分隔符时,您将如何删除路径中的最后一个匹配项? 我不确定如何使用 AWK 从末尾开始抓取字母。
I get the following messages often, for instance when coping dev files to a master branch
cp: /Users/Masi/gitHub/shells/zsh/dvorak: No such file or directory
cp: /Users/Masi/gitHub/shells/zsh/dvorak2: No such file or directory
I would like to be asked about the creation of the given folders such that my initial command will be run if I answer yes to the question(s).
My attempt in pseudo-code when I am trying to copy a file to a directory which does not exists
if no such a directory exists, then asks users about to create it:
if yes, then mkdir directory AND run the initial command again
else do noting
Problems
- To change the warning message: Which file does control the "No such file or directory" -command?
- To scrape the Path in the initial command AND mkidr Path without the file: How would scrape the Path in the initial command?
- To scrape from the end with your chosen language such as AWK: How would you remove the last match in the Path when / is the field separator? I am not sure how you can scrape letters starting from the end with AWK.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我编写的一个函数,可以在 zsh、bash 或 ksh 中运行。
注意: 它启用了调试(它回显将运行的命令,而不是执行它们)。 如果您注释掉该行,它实际上会运行它们。
警告:它尚未经过彻底测试。
要使用它,请将此脚本放入
/usr/local/bin
(或路径中的其他位置)中名为cpmd
的文件。 要激活它,请在 shell 提示符下键入以下命令(或将其添加到启动脚本中 - 对于 bash,它是~/.bashrc
):然后您可以使用如下命令复制文件:
目录“不存在”或“newdir”都不存在。 创建两个目录,然后将名为“carparts”的文件复制到“newdir”。
如果末尾不包含斜杠(“/”),最后一部分将被视为文件名,并创建之前不存在的目录:
创建目录“anothernew”,然后复制“supplies”新文件名为“consumables”。
如果目标中的所有目录均已存在,
cpmd
的作用类似于常规cp
命令。Here is a function I wrote which will work in zsh, bash or ksh.
Note: It has debugging enabled (it echoes the commands it would run rather than executing them). If you comment out that line, it will actually run them.
Caution: It hasn't been thoroughly tested.
To use it, put this script in a file called
cpmd
in/usr/local/bin
(or elsewhere in your path). To activate it, from the shell prompt type the following command (or add it to your startup script - for bash it would be~/.bashrc
):Then you can copy a file using a command like this:
Neither directory "nonexistent" or "newdir" exist yet. Both directories are created then the file named "carparts" is copied to "newdir".
If you don't include a slash ("/") at the end, the last part is treated as a file name and any non-existent directories before that are created:
The directory "anothernew" is created then "supplies" is copied with the new filename "consumables".
If all the directories in the destination already exist,
cpmd
acts like the regularcp
command.该错误消息来自 cp 命令,而不是 zsh。 如果您想改进输出,则必须编写用于截断和检查路径的逻辑,并检查它是否存在。
有一些命令可以帮助完成此操作,请查看 basename(1) 和 dirname(1)。
That error message is coming from the
cp
command, not zsh. If you want to improve the output, you're going to have to write the logic for truncating and examining the path along with checking to see if it exists or not.There are commands to assist with this, have a look at basename(1) and dirname(1).