我有一个目录,其中包含多个包含mov和jpg文件的子目录。
/dir/
/subdir-a/ # contains a-1.jpg, a-2.jpg, a-1.mov
/subdir-b/ # contains b-1.mov
/subdir-c/ # contains c-1.jpg
/subdir-d/ # contains d-1.mov
... # more directories with the same pattern
我需要找到一种使用命令行工具(在Mac上,理想情况下)将所有mov文件移动到新位置的方法。然而,一个要求是保持目录结构,即:
/dir/
/subdir-a/ # contains a-1.mov
/subdir-b/ # contains b-1.mov
# NOTE: subdir-c isn't copied because it doesn't have mov files
/subdir-d/ # contains d-1.mov
...
我对find
, grep
, and xargs
很熟悉,但不确定如何解决这个问题。在此之前非常感谢!
发布于 2012-12-31 12:00:40
作为大文件,如果它们在同一个文件系统上,您不想复制它们,而只是在移动时复制它们的目录结构。您可以使用此函数:
# moves a file (or folder) preserving its folder structure (relative to source path)
# usage: move_keep_path source destination
move_keep_path () {
# create directories up to one level up
mkdir -p "`dirname "$2"`"
mv "$1" "$2"
}
或者,添加对合并现有目录的支持:
# moves a file (or folder) preserving its folder structure (relative to source path)
# usage: move_keep_path source destination
move_keep_path () {
# create directories up to one level up
mkdir -p "`dirname "$2"`"
if [[ -d "$1" && -d "$2" ]]; then
# merge existing folder
find "$1" -depth 1 | while read file; do
# call recursively for all files inside
mv_merge "$file" "$2/`basename "$file"`"
done
# remove after merge
rmdir "$1"
else
# either file or non-existing folder
mv "$1" "$2"
fi
}
发布于 2014-10-12 19:17:36
断言:文件中没有换行符。然而,空格是可以接受的。
# TEST FIRST: CREATION OF FOLDERS
find . -type f -iname \*.mov -printf '%h\n' | sort | uniq | xargs -n 1 -d '\n' -I '{}' echo mkdir -vp "/TARGET_FOLDER_ROOT/{}"
# EXECUTE CREATION OF EMPTY TARGET FOLDERS
find . -type f -iname \*.mov -printf '%h\n' | sort | uniq | xargs -n 1 -d '\n' -I '{}' mkdir -vp "/TARGET_FOLDER_ROOT/{}"
# TEST FIRST: REVIEW FILES TO BE MOVED
find . -type f -iname \*.mov -exec echo mv {} /TARGET_FOLDER_ROOT/{} \;
# EXECUTE MOVE FILES
find . -type f -iname \*.mov -exec mv {} /TARGET_FOLDER_ROOT/{} \;
发布于 2012-01-10 11:36:25
从"dir“的父目录中执行以下命令:
find ./dir -name "*.mov" | xargs tar cif mov.tar
然后cd到要将文件移动到的目录,并执行以下命令:
tar xvf /path/to/parent/directory/of"dir"/mov.tar
https://stackoverflow.com/questions/8798153
复制相似问题