如何组合 shell 命令
我正在尝试制作一个脚本,该脚本将从目录复制文件并将复制的文件放入新目录中。
我知道 cp 命令将复制文件,mkdir 命令将创建目录,但有人知道如何将这两个命令合并到一行中吗?
到目前为止,我收到了
mkdir /root/newdir/ cp /root/*.doc /root/newdir
错误消息
mkdir: cannot create directory 'cp': Files exists
mkdir: cannot create directory '/root/files/wp.doc: File exists
mkdir: cannot create directory 'mkdir' : File exists
mkdir: cannot create directory '/root/files/new dir: file exists
,但是它确实创建了目录 newdir
I am trying to make a script that will copy files from a directory and place the copied files into a new directory.
I know that the cp
command will copy the files and the mkdir
command will create the directory but does anyone know how to combines these 2 commands into a single line?
So far I have
mkdir /root/newdir/ cp /root/*.doc /root/newdir
this gives the error message
mkdir: cannot create directory 'cp': Files exists
mkdir: cannot create directory '/root/files/wp.doc: File exists
mkdir: cannot create directory 'mkdir' : File exists
mkdir: cannot create directory '/root/files/new dir: file exists
However it does create the directory newdir
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这将调用 mkdir 创建目录结构,检查命令执行是否成功,如果成功则调用 cp 命令。
This will call
mkdir
to create directory structure, check if command execution was successful and callcp
command if it was.在两个命令之间放置分号
Place semicolon between two commands
发生这种情况是因为您没有告诉 shell 命令的确切结束位置。在这种情况下:
您的命令
cp
将作为mkdir
命令的参数,并且 shell 尝试创建名为cp
的文件。同样的情况也发生在所有其他人身上。通过将
;
放在命令后面。它告诉 shell 命令已经结束,下一个单词是另一个命令。换行符(回车键)也被视为命令分隔符。因此,如果将每个命令放在下一行,它也可以正常工作。
所以你可以尝试以下任一方法:
或者
This happens because you do not tell the shell where exactly the commands end. In this case:
Your command
cp
will go as an argument to themkdir
command and shell tries to make the file namedcp
. Same happens to all other.By putting the
;
after commands. It tells the shell that command has been ended and next word is an another command.newline (Return key) is also treated as the command seprator. So if you put each command in next line, it also works fine.
So you can try either of these:
OR