我要做的操作是取目录1:
Dir1
Dir A
File A
Dir B
File B然后使用find命令检查Dir 1中的每个文件是否有现有的硬链接,如下所示:
find . -type f -links 1 -exec cp -al {} /path/to/Dir2/{} \;然后我想要结束:
Dir2
Dir A
File A (hardlink)
Dir B
File B (hardlink)现在,我知道如何找到目录中的每个非硬链接文件,并将这些文件的硬链接放置在不同的目录中,但在创建新的硬链接时,我希望保持相同的目录结构。我现在的命令会产生这样的结果:
Dir2
File A (hardlink)
File B (hardlink)假设我正在查看文件B,而B文件只有一个链接(还没有硬链接),我如何引用"Dir B“才能将该目录复制到新目录?我不想要"/Path/To/Dir B“只是"Dir B”。
有没有办法在bash中完成这一任务?
发布于 2023-04-03 12:39:06
您可以使用像find和mkdir这样的工具来实现这一点。
在bash文件中,.sh不要忘记将/ path / to /Dir1替换为源目录路径,将/ path /to/Dir2替换为目标目录路径。
#!/bin/bash
src_dir="/path/to/Dir1"
dest_dir="/path/to/Dir2"
find "$src_dir" -type d -print0 | while IFS= read -r -d '' dir; do
dest_subdir="${dir/$src_dir/$dest_dir}"
mkdir -p "$dest_subdir"
find "$dir" -maxdepth 1 -type f -links 1 -print0 | while IFS= read -r -d '' file; do
cp -al "$file" "$dest_subdir"
done
done发布于 2023-04-03 15:51:38
是的,您可以在bash中使用rsync命令而不是cp来完成此任务,并修改find命令以使用变量。下面是一个适合您需要的示例命令:
#!/bin/bash
# Set source and destination directories
src_dir="Dir1"
dest_dir="Dir2"
# Use find to locate all files in source directory with only one link
find "$src_dir" -type f -links 1 | while read file; do
# Get the directory name of the file and create the corresponding directory in the destination
dir=$(dirname "$file")
mkdir -p "$dest_dir/$dir"
# Copy the file using rsync with the -l (hardlink) option
rsync -av --link-dest="$src_dir" "$file" "$dest_dir/$file"
donehttps://unix.stackexchange.com/questions/741745
复制相似问题