复制一个不包含文件内容但保留文件名的目录结构

19

我有一个非常庞大的电影文件目录结构。为了分析这个结构,我想要复制整个目录结构,包括文件夹和文件,但不想复制所有的电影文件,同时希望保留它们的文件名。理想情况下,我会得到零字节大小的文件,文件名与原始电影文件相同。

我尝试过链接文件,然后使用rsync将其同步到远程机器,但没有获取到链接文件。

有没有不编写脚本就能做到这一点的想法呢?

4个回答

20
您可以使用find:
find src/ -type d -exec mkdir -p dest/{} \; \
       -o -type f -exec touch dest/{} \;

查找 (-d) 在 (src/) 下的目录并在 dest/ 下创建 (mkdir -p) 这些目录;或者 (-o) 查找 (-f) 文件并在 dest/ 下创建这些文件 (touch)。

执行后将会得到:

dest/src/<file-structre>

你可以巧妙地使用mv来解决这个问题。


使用rsync也可以实现其他(部分)解决方案:

rsync -a --filter="-! */" sorce_dir/ target_dir/

这里的技巧在于使用--filter=RULE选项,该选项排除(-)不是目录(*/)的所有内容(!)。


13
在Ubuntu上,您可以尝试以下操作:
cp -r --attributes-only <source_dir> <target_dir>

它不会复制文件数据。 来自cp的man页面。
--attributes-only
          don't copy the file data, just the attributes

注意:我不确定其他发行版是否有此选项,如果有人能确认,请更新答案。

谢谢你的回答,Rohan。不幸的是,我的Netgear ReadyNAS上的Debian不支持这个选项 :( - Juergen Riemer
在 Debian GNU/Linux 10 (buster) 下运行良好。 - jkeys

0

我需要一种替代方案来仅同步文件结构:

rsync --recursive --times --delete --omit-dir-times --itemize-changes "$src_path/" "$dst_path"

这是我意识到的方式:

# sync source to destination
while IFS= read -r -d '' src_file; do
  dst_file="$dst_path${src_file/$src_path/}"
  # new files
  if [[ ! -e "$dst_file" ]]; then
    if [[ -d "$src_file" ]]; then
      mkdir -p "$dst_file"
    elif [[ -f $src_file ]]; then
      touch -r "$src_file" "$dst_file"
    else
      echo "Error: $src_file is not a dir or file"
    fi
    echo -n "+ "
    ls -ld "$src_file"
  # modification time changed (files only)
  elif [[ -f $dst_file ]] && [[ $(date -r "$src_file") != $(date -r "$dst_file") ]]; then
    touch -r "$src_file" "$dst_file"
    echo -n "+ "
    ls -ld "$src_file"
  fi
done < <(find "$src_path" -print0)

# delete files in destination if they disappeared in source
while IFS= read -r -d '' dst_file; do
  src_file="$src_path${dst_file/$dst_path/}"
  # file disappeard on source
  if [[ ! -e "$src_file" ]]; then
    delinfo=$(ls -ld "$dst_file")
    if [[ -d "$dst_file" ]] && rmdir "$dst_file" 2>/dev/null; then
      echo -n "- $delinfo"
    elif [[ -f $dst_file ]] && rm "$dst_file"; then
      echo -n "- $delinfo"
    fi
  fi
done < <(find "$dst_path" -print0)

正如您所看到的,我使用echols来显示更改。


-3

ls > listOfMovie.txt; 你将在一个 .txt 文件中得到你的电影列表。如需查看多个目录,请参阅 man 手册。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接