ele*_*n81 533 unix cp recursive
如何绝对cp -r复制目录中的所有文件和目录
要求:
我丑陋但有效的黑客是:
cp -r /etc/skel/* /home/user
cp -r /etc/skel/.[^.]* /home/user
Run Code Online (Sandbox Code Playgroud)
如何在没有模式匹配的情况下在一个命令中完成这一切?我需要使用什么标志?
Bru*_*ira 613
假设您创建了新文件夹(或将要创建一个)并希望在创建文件夹后将文件复制到其中
mkdir /home/<new_user>
cp -r /etc/skel/. /home/<new_user>
Run Code Online (Sandbox Code Playgroud)
这将递归地将所有文件/文件夹从/etc/skelin复制到在第一行创建的现有文件夹中。
Ran*_*son 336
不要指定文件:
cp -r /etc/skel /home/user
(注意/home/user不能已经存在,否则会创建/home/user/skel。)
小智 177
这样做的正确方法是使用该-T (--no-target-directory)选项,并递归复制文件夹(没有尾部斜杠、星号等),即:
cp -rT /etc/skel /home/user
Run Code Online (Sandbox Code Playgroud)
这将复制/etc/skelto的内容/home/user(包括隐藏文件),/home/user如果文件夹不存在则创建该文件夹;但是,如果该文件夹存在,该-T选项会阻止 的内容/etc/skel被复制到新文件/home/user/skel夹中/home/user。
Per*_*ulf 75
bash本身有一个很好的解决方案,它有一个shell option,你可以cp,mv等等:
shopt -s dotglob # for considering dot files (turn on dot files)
Run Code Online (Sandbox Code Playgroud)
和
shopt -u dotglob # for don't considering dot files (turn off dot files)
Run Code Online (Sandbox Code Playgroud)
以上解决方案是标准的 bash
笔记:
shopt # without argument show status of all shell options
-u # abbrivation of unset
-s # abbrivation of set
Run Code Online (Sandbox Code Playgroud)
小智 7
rsync 很好,但另一种选择:
cp -a src/ dst/
Run Code Online (Sandbox Code Playgroud)
从主要帮助:
-a, --archive
same as -dR --preserve=all
-d same as --no-dereference --preserve=links
-R, -r, --recursive
copy directories recursively
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您的源目录和目标目录具有相同的名称,即使目标目录存在,您也可以简单地键入:
cp -R /etc/skel /home/
Run Code Online (Sandbox Code Playgroud)
这会将 /etc/skel 目录复制到 /home/ 中,包括隐藏文件和目录。
最后,您可以复制该目录并在一行中重命名它:
cp -R /etc/skel /home/ && mv /home/skel /home/user
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用 rsync。
rsync -aP ./from/dir/ /some/other/directory/
Run Code Online (Sandbox Code Playgroud)
你甚至可以通过 ssh 复制
rsync -aP ./from/dir/ username@remotehost:/some/other/directory/
Run Code Online (Sandbox Code Playgroud)
您可以使用多种标志:-a, --archive # archive (-rlptgoD)
-r, --recursive
-l, --links # copy symlinks as links
-p, --perms # preserve permissions
-t, --times # preserve times
-g, --group # preserve group
-o, --owner # preserve owner
-D # --devices --specials
--delete # Delete extra files
You may want to add the -P option to your command.
--partial # By default, rsync will delete any partially transferred file if the transfer is interrupted. In some circumstances it is more desirable to keep partially transferred files. Using the --partial option tells rsync to keep the partial file which should make a subsequent transfer of the rest of the file much faster.
-P # The -P option is equivalent to --partial --progress. Its purpose is to make it much easier to specify these two options for a long transfer that may be interrupted.
Run Code Online (Sandbox Code Playgroud)
cp -r /etc/skel/{.,}* /home/user
Run Code Online (Sandbox Code Playgroud)
该表达式{.,}*包括所有文件和目录(也以点开头)。
如果您不想使用上述表达式,则可以使用该cp属性,即为一个目标文件夹指定多个源的能力:
cp -r /etc/skel/* /home/user
Run Code Online (Sandbox Code Playgroud)