[英]Symbolically link all JPGs in a directory tree to a single directory
I have a series of directories, and images contained within: 我有一系列目录,图像包含在其中:
/Volumes/library/Originals/2012/2012-05-13 Event/filename.jpg
/Volumes/library/Originals/2011/2011-03-11 Event/filename.jpg
/Volumes/library/Originals/2011/2011-01-12 Event/filename.jpg
/Volumes/library/Originals/2009/2019-07-11 Event/filename.jpg
Using bash, how can I create symbolic links to this directory tree in a single directory? 使用bash,如何在单个目录中创建指向该目录树的符号链接?
/image-links/filename.jpg
/image-links/filename1.jpg
I need this to get my photos screen saver running on Mac OS X 10.8 which doesn't support recursive directories. 我需要它来使照片屏幕保护程序在不支持递归目录的Mac OS X 10.8上运行。 I figure I can make a cron job that does this nightly. 我认为我可以做每晚做一次的日常工作。 Thanks for your help. 谢谢你的帮助。
Give this a crack: 破解一下:
#!/bin/sh
seq=1
for f in $(find /Volumes/library/Originals -name *.jpg)
do
base=$(basename "$f" .jpg)
new=$(printf "%s%03d.jpg" "$base" $seq)
ln -fs "$file" "/image-links/$new"
seq=$(expr $seq + 1)
done
PREFIX="filename"
SOURCEDIR="/Volumes/library/Originals"
DESTDIR="image-links"
i=0
cd $DESTDIR
find "$SOURCEDIR" -name \*.jpg | while read f
do
ln -s "$f" $PREFIX$i.jpg
i=$((i+1))
done
This oneliner is working on linux very well. 这个oneliner在Linux上运行良好。
find /Volumes/library/Originals -name "*.jpg" |
awk '{print "ln -s \""$0"\" /image-links/filename_"FNR".jpg"}' | sh
To explain the pipes 解释管道
first finds files | 首先找到文件| second makes commands |third runs commands 第二条命令|第三条运行命令
Pretty simple, tested and working in cron. 非常简单,经过测试,可以在cron中工作。
Petr 彼得
Assuming the files are in the same depth, a for-loop like this should do: 假设文件位于同一深度,则应执行如下所示的for循环:
i=1
for file in /Volumes/library/Originals/*/*/*.jpg; do
base=${file##*/}
ln -s "$file" "/image-links/${base%.*}$((i++)).jpg"
done
Instead of giving the links an incrementing number, you could add the directory name to the linkname instead. 可以给目录名称添加链接名称,而不是给链接增加编号。
for file in /Volumes/library/Originals/*/*/*.jpg; do
base=${file##*/}
dir=${file%/*} dir=${dir##*/}
ln -s "$file" "/image-links/$dir $base"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.