How to pick 100 files randomly from a directory by Linux shell. I read other topic, 'shuf' command can do this: find . -type f | shuf -n100, but our environments do not have 'shuf' cmd. Is there other method to do this? use bash, awk, sed or sth else?
如何从Linux shell中随机抽取100个文件。我读过另一个话题,“shuf”命令可以做到:查找。-type f | shuf -n100,但我们的环境没有“shuf”cmd。还有别的方法吗?用bash、awk、sed还是别的什么?
3 个解决方案
#1
1
This should work on your CentOS5:
这应该适用于你的简历。
shuf() { awk 'BEGIN{srand()}{print rand()"\t"$0}' "$@" | sort | cut -f2- ;}
This comes from a comment by Meow on https://*.com/a/2153889/5844347
这来自Meow对https://*.com/a/2153889/5844347的评论
Use like so: find . -type f | shuf | head -100
像这样使用:find。-type f | shuf | head -100
#2
2
You can get a directory listing, then randomize it, then pick the top N lines.
你可以得到一个目录列表,然后随机化它,然后选择前N行。
ls | sort -R | head -n 100
Replace ls
with an appropriate find
command if you want a recursive listing or need finer control of the files to be included.
如果您想要递归清单或需要更好地控制要包含的文件,请使用适当的find命令替换ls。
#3
0
# To get a integer number between 1 to 100 :
N=`echo|awk 'srand() {print 99*rand() + 1 }' | sed -e "s/\..*$//g"`
echo $N
# To get the Nth file :
find . -type f | head -${N} | tail -1
# To get 100 files randomly :
for i in $(seq 1 100 )
N=`echo|awk 'srand() {print 99*rand() + 1 }' | sed -e "s/\..*$//g"`
find . -type f | head -${N} | tail -1
done
#1
1
This should work on your CentOS5:
这应该适用于你的简历。
shuf() { awk 'BEGIN{srand()}{print rand()"\t"$0}' "$@" | sort | cut -f2- ;}
This comes from a comment by Meow on https://*.com/a/2153889/5844347
这来自Meow对https://*.com/a/2153889/5844347的评论
Use like so: find . -type f | shuf | head -100
像这样使用:find。-type f | shuf | head -100
#2
2
You can get a directory listing, then randomize it, then pick the top N lines.
你可以得到一个目录列表,然后随机化它,然后选择前N行。
ls | sort -R | head -n 100
Replace ls
with an appropriate find
command if you want a recursive listing or need finer control of the files to be included.
如果您想要递归清单或需要更好地控制要包含的文件,请使用适当的find命令替换ls。
#3
0
# To get a integer number between 1 to 100 :
N=`echo|awk 'srand() {print 99*rand() + 1 }' | sed -e "s/\..*$//g"`
echo $N
# To get the Nth file :
find . -type f | head -${N} | tail -1
# To get 100 files randomly :
for i in $(seq 1 100 )
N=`echo|awk 'srand() {print 99*rand() + 1 }' | sed -e "s/\..*$//g"`
find . -type f | head -${N} | tail -1
done