简体   繁体   English

有没有办法在bash中进入随机目录?

[英]Is there a way to cd into a random directory in bash?

I wish to figure out a way to change the working directory to a random directory in bash. 我希望找到一种方法将工作目录更改为bash中的随机目录。 By this I mean a pre-created directory that is not specified in any way. 我的意思是指未以任何方式指定的预先创建的目录。 I've already tried just using cd * , but it doesn't recognize the wildcard character unless I have a letter in front of it, ie, cd a* . 我已经尝试过只使用cd * ,但它不识别通配符,除非我前面有一个字母,即cd a* Does anyone know a quick, easy way to do this? 有谁知道一个快速,简单的方法来做到这一点? A one-line solution would be great, but any answer will do. 一线解决方案会很棒,但任何答案都可以。 Thanks. 谢谢。

shopt -s nullglob
dirs=(*/)
[[ $dirs ]] && cd -- "${dirs[RANDOM%${#dirs[@]}]}"

Whenever you want a single line, use a function. 只要您想要一条线,就可以使用一个函数。 This also allows using locals and has other advantages. 这也允许使用当地人并具有其他优点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM