Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 从特定目录开始复制文件路径-Bash_Linux_Bash_Directory_Copy_Filesystems - Fatal编程技术网

Linux 从特定目录开始复制文件路径-Bash

Linux 从特定目录开始复制文件路径-Bash,linux,bash,directory,copy,filesystems,Linux,Bash,Directory,Copy,Filesystems,如何使用Bash从特定目录复制文件(包括其文件夹层次结构) 例如,假设我有以下文件: /第一/第二/第三/1.txt /第一/第三/2.txt /第一/第二/3.txt 我想将文件(1.txt,2.txt,3.txt)路径复制到/first/目录之后,结果应该是: /第二/第三/1.txt /第三个/2.txt /秒/3.txt 使用以下命令,但不幸的是,它包含包括“first”目录在内的整个路径,因此需要进行一些调整: find . -type f -name "*.txt&

如何使用Bash从特定目录复制文件(包括其文件夹层次结构)

例如,假设我有以下文件:

  • /第一/第二/第三/1.txt
  • /第一/第三/2.txt
  • /第一/第二/3.txt
我想将文件(1.txt,2.txt,3.txt)路径复制到/first/目录之后,结果应该是:

  • /第二/第三/1.txt
  • /第三个/2.txt
  • /秒/3.txt
使用以下命令,但不幸的是,它包含包括“first”目录在内的整个路径,因此需要进行一些调整:

find . -type f -name "*.txt" -exec cp --parent {} destination_folder/

有什么建议吗?

如果在
查找之前无法先执行
cd
,请尝试在
-exec
中执行:

find . -type f -name "*.txt" -exec bash -c 'cd first; echo cp --parent "${1#./*/}" ../destination_folder/' _ {} \;

如果在
查找之前无法先执行
cd
,请尝试在
-exec
中执行:

find . -type f -name "*.txt" -exec bash -c 'cd first; echo cp --parent "${1#./*/}" ../destination_folder/' _ {} \;

如果您可以执行
find
,您还应该能够执行
cd

也许您需要的是一个子shell,以便
cd
不会在
find
之外继续存在

: commands before, in $someDir
( cd /first/ && # in subshell, only local env logs new dir 
  find . -type f -name '*.txt' -exec cp --parent -t $destination_folder/ {} + 
) # subshell ends, parent never changed logged dir
: commands after - still in $someDir

如果您可以执行
find
,您还应该能够执行
cd

也许您需要的是一个子shell,以便
cd
不会在
find
之外继续存在

: commands before, in $someDir
( cd /first/ && # in subshell, only local env logs new dir 
  find . -type f -name '*.txt' -exec cp --parent -t $destination_folder/ {} + 
) # subshell ends, parent never changed logged dir
: commands after - still in $someDir

将cd放入
/first/
,然后运行以下命令:
find-键入f-name'*.txt'-exec cp--parent-t destination_folder/{}+
@oguzismail这就是问题所在,我无法cd,因为它是更大脚本的一部分。我需要能够将“find”相对路径输出:/cd操作到
/first/
,并运行以下命令:
find-键入f-name'*.txt'-exec cp--parent-t destination_folder/{}+
@oguzismail这就是问题所在,我无法cd,因为它是更大脚本的一部分。我需要能够操作“查找”相对路径输出:/