2011-04-20 10:24:10 +0000 2011-04-20 10:24:10 +0000
79
79
Advertisement

在Linux上递归移动文件和文件夹

Advertisement

思考:

ls -al ../public-back
drwxrwxr-x 4 apache apache 4096 Apr 19 03:32 templates

ls -al ../public-back/templates

drwxrwxr-x 2 apache apache 4096 Apr 19 03:33 content
drwxrwxr-x 2 apache apache 20480 Apr 20 06:14 images
drwxrwxr-x 2 apache apache 4096 Apr 19 03:35 video

ls -al /public

drwxrwxr-x 4 apache apache 4096 Apr 20 09:49 templates

ls -al /public/templates

drwxrwxr-x 2 apache apache 4096 Apr 20 09:50 content
drwxrwxr-x 2 apache apache 4096 Apr 20 09:50 images
drwxrwxr-x 2 apache apache 4096 Apr 20 09:50 video

&001

如何将/public-back/templates中的内容递归移动到/public/templates中,并有权限的文件和文件夹?

Advertisement
Advertisement

答案 (5)

97
97
97
2011-04-20 14:10:36 +0000

除非我误解了问题,否则这样做可以:

mv /public-back/templates/* /public/templates

另外,除非你有一个巨大的文件列表,否则添加-i会在覆盖任何东西之前询问,这在使用像*这样的通配符时增加了一些安全性。

10
10
10
2011-04-20 14:24:07 +0000

cp的页面上写着:

-p same as --preserve=mode,ownership,timestamps
-r same as --recursive=copy directories recursively

尝试;

cp -rp /public-back/templates/* /public/templates/
5
Advertisement
5
5
2016-02-17 07:43:21 +0000
Advertisement

当我把项目从优盘移动到我的OSMC系统时,我发现以下方法非常有用:

find /media/Pi\ Hard\ 16GB/ -name '*' -exec mv -v {} /media/External\ HDD/Videos/ \;

下面解释一下它的工作原理。

find finds all files and folders in the destination path.

/media/Pi Hard 16GB/ is the path searched. Escape special char such as spaces.

-name '*' filters on names. If you do not escape or quote this then 
          the shell will expand it before find sees it.

-exec Executes a command, in our case mv

-v Verbose, so you can see what's happening (optional)

{} is replaced by the name of the found object.

实际上,你是在查找所有的文件和所有的文件夹,然后逐一移动它们(或者说,如果一个目录先被找到,你就是在移动这个目录和其中的内容)。这样每次移动都要开始一个新的过程,效率很低。只有在常规命令失败时才使用。

2
2
2
2017-09-06 07:05:14 +0000
cp -a --link ../public-back/* /public/. && rm -rf ../public-back

所以在目标目录中创建硬链接,然后删除源目录。"mv “在你的情况下根本不起作用,一般来说,只有当源目录和目的地没有共同的子目录时才会起作用。

2
Advertisement
2
2
2015-10-20 15:24:37 +0000
Advertisement

mv似乎没有这样做。但你可以用这个小技巧,效果很好:

tar cf - . |(cd /targetdir; tar xvf -)

&001

并保留权限。

Advertisement

相关问题

6
10
7
5
2
Advertisement
Advertisement