首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复制文件名不含内容的目录结构

复制文件名不含内容的目录结构
EN

Stack Overflow用户
提问于 2012-08-14 13:53:13
回答 4查看 11.6K关注 0票数 17

我有一个巨大的电影文件目录结构。为了分析这种结构,我想复制整个目录结构,即文件夹和文件,但是我不想复制所有的电影文件,而我想保留它们的文件名。理想情况下,我得到的是带有原始电影文件名的零字节文件。

我尝试然后rsync到我的远程机器,它没有获取链接文件。

有什么想法可以在不写脚本的情况下做到这一点吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-08-14 20:46:30

您可以使用find:

代码语言:javascript
复制
find src/ -type d -exec mkdir -p dest/{} \; \
       -o -type f -exec touch dest/{} \;

在(src/)下找到目录(-d)并在dest/下创建(mkdir -p),或(-o)在dest/下找到文件(-f)并对其执行touch操作。

这将导致:

代码语言:javascript
复制
dest/src/<file-structre>

您可以创造性地使用mv来解决此问题。

其他(部分)解决方案可以通过rsync实现:

代码语言:javascript
复制
rsync -a --filter="-! */" sorce_dir/ target_dir/

这里的诀窍是--filter=RULE选项,它排除(-)不是(!)目录(*/)的所有内容

票数 18
EN

Stack Overflow用户

发布于 2012-08-14 13:59:48

在ubuntu上,你可以尝试:

代码语言:javascript
复制
cp -r --attributes-only <source_dir> <target_dir>

它不会复制文件数据。来自cp的手册页

代码语言:javascript
复制
--attributes-only
          don't copy the file data, just the attributes

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

票数 11
EN

Stack Overflow用户

发布于 2022-01-21 09:56:09

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

代码语言:javascript
复制
rsync --recursive --times --delete --omit-dir-times --itemize-changes "$src_path/" "$dst_path"

我是这样认识到这一点的:

代码语言:javascript
复制
# 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来显示更改。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11946465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档