LINUX: List contents of a outside folder or of a subfolder without leaving current folder

StackOverflow https://stackoverflow.com/questions/5450575

  •  12-11-2019
  •  | 
  •  

Question

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!

Was it helpful?

Solution

Just use ls?

ls /path/to/directory

Alternatively, use opendir() and readdir(), see man 3 opendir and man 3 readdir

OTHER TIPS

maybe you are looking for

ls /path/to/folder

If you want to see the contents of the parent directory you can use ls ..

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 and closedir - this is a perfectly good reference.

You can use ls just as you suggested. Check out the ls man page. Example:

$ ls /tmp/somedir
file1  file2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top