LINUX:列出外部文件夹或子文件夹的内容而不离开当前文件夹
我有一个 ac 程序,可以监视目录中某处的文件夹(程序启动时给出位置)。它的任务之一是告诉用户该指定文件夹中存储了哪些内容。我正在考虑通过管道传输 ls 命令,但我不确定如何获取您当前不在其中的文件夹的内容。帮助?
谢谢!
I have a c program that watches over a folder somewhere in your directory (location is given when program starts up). One of its tasks is to tell the user what contents are stored in that specified folder. I was thinking of piping the ls command but I'm unsure how to get contents of a folder in which you are not currently in. Help?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就用ls吗?
或者,使用
opendir()
和readdir()
,请参阅man 3 opendir
和man 3 readdir
Just use ls?
Alternatively, use
opendir()
andreaddir()
, seeman 3 opendir
andman 3 readdir
也许你正在寻找
maybe you are looking for
如果您想查看父目录的内容,可以使用 ls ..
If you want to see the contents of the parent directory you can use
ls ..
如果您在终端,其他答案是合适的,但您可能需要 C API,而不是昂贵的调用来分叉进程并列出目录。
对于 C API,您需要查看
opendir
、readdir
和linedir
- 这是一个非常好的参考。The other answers are suitable if you are at the terminal, but you would probably like a C API, rather than an expensive call to fork the process and list a directory.
For a C API, you'll want to take a look at
opendir
,readdir
andclosedir
- this is a perfectly good reference.您可以按照您的建议使用
ls
。查看ls
手册页。例子:You can use
ls
just as you suggested. Check out thels
man page. Example: