[英]copy a directory structure with file names without content
I have a huge directory structure of movie files.我有一个巨大的电影文件目录结构。 For analysis of that structure I want to copy the entire directory structure, ie folders and files however I don't want to copy all the movie files while I want to keep there file names.
为了分析该结构,我想复制整个目录结构,即文件夹和文件,但是我不想复制所有电影文件,而我想保留文件名。 Ideally I get zero-byte files with the original movie file name.
理想情况下,我会得到带有原始电影文件名的零字节文件。
I tried to and then rsync to my remote machine which didn't fetch the link files.我尝试然后 rsync 到我没有获取链接文件的远程机器。
Any ideas how to do that w/o writing scripts?任何想法如何在不编写脚本的情况下做到这一点?
You can use find: 你可以使用find:
find src/ -type d -exec mkdir -p dest/{} \; \
-o -type f -exec touch dest/{} \;
Find directory ( -d
) under ( src/
) and create ( mkdir -p
) them under dest/
or ( -o
) find files ( -f
) and touch
them under dest/
. 查找目录(
-d
()下src/
),创造( mkdir -p
)他们在dest/
或( -o
)查找文件( -f
)和touch
下他们dest/
。
This will result in: 这将导致:
dest/src/<file-structre>
You can user mv
creatively to resolve this issue. 您可以创造性地使用
mv
来解决此问题。
Other (partial) solution can be achieved with rsync: 使用rsync可以实现其他(部分)解决方案:
rsync -a --filter="-! */" sorce_dir/ target_dir/
The trick here is the --filter=RULE
option that excludes ( -
) everything that is not ( !
) a directory ( */
) 这里的技巧是
--filter=RULE
选项,它排除( -
)所有不是( !
)目录( */
)的东西
On ubuntu you can try: 在ubuntu上你可以尝试:
cp -r --attributes-only <source_dir> <target_dir>
It doesn't copy file data. 它不会复制文件数据。 From manpage of
cp
从
cp
页
--attributes-only
don't copy the file data, just the attributes
Note: I'm not sure this option available for other distributions, if anybody can confirm please update the answer. 注意:我不确定此选项是否可用于其他发行版,如果有人可以确认请更新答案。
I needed an alternative to this to sync only the file structure:我需要一个替代方法来仅同步文件结构:
rsync --recursive --times --delete --omit-dir-times --itemize-changes "$src_path/" "$dst_path"
This is how I realized it:这就是我意识到的方式:
# 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)
As you can see I use echo
and ls
to display changes.如您所见,我使用
echo
和ls
来显示更改。
ls > listOfMovie.txt; ls> listOfMovie.txt; You will have the list of your films in a .txt file .For multiple directories see the man page.
您将在.txt文件中获得电影列表。对于多个目录,请参见手册页。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.