Linux file and directory programs

What is the Linux equivalent to Windows’ Program Files?

Under Windows, most applications and application data are stored in a special directory known as C:\Program Files (and occasionally C:\Program Files (x86) ). What is the Ubuntu/Linux equivalent to this path? Is there even one?

If you want to know where are the files of specific package, you can use Synaptic package manager. Just search for package, and see its installed files.

I think this question would be improved by a reason for wanting an equivalent. There is a difference between, for instance, «lets see what I have installed and what I can run», and «I’m trying to find the save-game of this/that piece of software, so I can fiddle with it» and maybe even «I suspect some setting-file / ini-file for this program that I can tweak». Those could all be «Program Files» question, but have different answers here.

Related (but probably shouldn’t be considered a duplicate): How to understand the Ubuntu file system layout?

10 Answers 10

Late Answer — I’ve created a roadmap for beginners to follow. If they are looking for a file but don’t know where to look, they can use the map to roughly navigate around. You can download a hi-res PNG here. You can find the related post here.

When I see it laid out like this, I realise it’s no wonder modern distros try to hide this stuff. What a mess (beautiful mess). And I don’t agree with this layout completely — it’s brilliant, but for example /etc holds application configs as much as O/S configs, so it feels like it should be to the left of the second-from-right-most yellow «User Specific» question as a «no» branch. For example, apache is an application, it’s not O/S essential. Neither is Docker, Folding@Home, VMware, or a host of others that use /etc for configs. Great diagram though.

[EDIT: You should probably check out d4nyll’s answer instead, which is a nifty map, acting as an extensive beginner’s guide to the Linux file system]

/bin and /usr/bin is where the scripts are that start the programs. The direct equivalent of «Program Files» though is probably /opt or maybe /usr/share (see Filesystem Hierarchy Standard). That directory contains the various support files for most programs.

Nautilus showing /usr/share

There probably isn’t a direct equivalent however, since, for example, library files are shared across the system (in /lib ) and options are either user specified (in the user’s home directory) or universally located in /etc .

Читайте также:  Изменение разрешения экрана linux через терминал

So installing a program via a deb file, repository or build will likely place files in all of these locations.

[EDIT] And as others note, there is also /sbin and /usr/sbin . Plus /usr/local/bin , /opt/bin and even /usr/games/ . So definitely not a direct comparison to c:\program files !

How about /opt ? I’ve seen that hold many (usually closed-source or app-based) programs before in a similar way to Program Files .

Seems to be going that way, with the advent of the Ubuntu Software Centre delivering its games there, certainly. But even then, hardly a direct comparison, really, is it? Might the closest thing these days though.

/usr/local/bin is a semi-common location for software that might be not commonly used by a user/company.

If you’re looking for productivity in the workplace, (1) Everything is terminal. Terminal is much faster than navigating through a hierarchy of options in a program. You usually have to google how to change a setting anyway which involves following a sequence of screenshots, now google gives you a copy-pastable command (2) cron’s let you setup anything to run in the background and do whatever you want it to do (3) It’s easy to use bash to automate anything you want to be automated

EDIT: See also d4nyll’s answer below above for an excellent and beginner-friendly map!

Read my answer below for more info on what the PATH environment variable is, what .desktop files are, and how to find a specific program using various linux commands.

Original answer:

There is no easy answer.

/bin , /usr/bin , and /usr/share

As mentioned in the other answers, you can find most executables under /bin or /usr/bin , and the support files are installed in /usr/share .

/usr/local and /opt

There are however more directories in which Ubuntu installs applications. The PATH variable, which determines where to search for an entered command, might give you a clue, mine looks like ( echo $PATH in a terminal):

/usr/local/cuda/bin:/usr/local/texlive/2012/bin/x86_64-linux:/usr/games:/home/gerhard/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games 

As you can see some software is installed in /usr/local and have their own directory and bin . Another place where many programs are installed is /opt . The properties of these locations are explained by the Filesystem Hierarchy Standard, which is a very good read. Unfortunately, the difference between /opt and /usr/local is not very well explained, someone on the unix stackexchange had a more elaborate explanation:

  • /usr/local is a place to install files built by the administrator, usually by using the make command. The idea is to avoid clashes with files that are part of the operating systems that would either be overwritten or overwrite the local ones otherwise. eg. /usr/bin/foo is part of the OS while /usr/local/bin/foo is a local alternative,
  • /opt is a directory to install unbundled packages each in their own subdirectory. They are already built whole packages provided by an independent third party software distributor. For example someapp would be installed in /opt/someapp , one of its command would be in /opt/someapp/bin/foo [and then usually a symbolic link is made in one of the bin directories in the PATH , or the program is called from a desktop file (see below)].
Читайте также:  Sed in linux with examples

Finding a specific program or command

.desktop files

To find out where a specific program is installed, you can do a number of steps. First you need to locate its .desktop file. Desktop files are simular to shortcuts in Windows, and for system applications they are located in /usr/share/applications . The desktop files for applications that are only available for the current user are in ~/.local/share/applications . Take for example Google Chrome, which has the desktop file /usr/share/applications/google-chrome.desktop and look for the line that starts with Exec= , this determines how to start Google Chrome. It says:

Exec=/opt/google/chrome/google-chrome 

So you know Google Chrome is in /opt .

Now for Mozilla Firefox which is located in /usr/share/applications/firefox.desktop . It simply says

At first this doesn’t seem to help that much, but then you realize that firefox must be in a directory that is in the PATH variable (most likely a bin ), and we can look it up (see below).

Looking up commands

To look up commands you can use one or more of the following: type , which and whereis (I’ve included a link to their manual pages online).

  • type: it describes a command, and indicates how it would be interpreted if used as a command name. Possible types for a command are:
    1. alias (shell alias)
    2. function (shell function)
    3. builtin (shell builtin)
    4. file (disk file)
    5. keyword (shell reserved word)

(type itself is a shell builtin, try it with type type :P)

Executing type firefox gives us

which is what we wanted to know

If a command is a file (which you checked with type ) you can then also use:

    which: shows the full path of the command, Executing which firefox gives us

firefox: /usr/bin/firefox /etc/firefox /usr/lib/firefox /usr/lib64/firefox /usr/bin/X11/firefox /usr/share/man/man1/firefox.1.gz 

You can inspect /usr/bin/firefox closer with ls -l /usr/bin/firefox and this gives:

/usr/bin/firefox -> ../lib/firefox/firefox.sh* 

It appears that /usr/bin/firefox is ‘only’ a symbolic link to the script /usr/lib/firefox/firefox.sh . If you inspect the script you discover that the script calls /usr/lib/firefox/firefox .
You may rest in peace now 🙂

Источник

How to list files in a directory in a C program?

I’m trying to write an ftp server on Linux. In this matter how can I list files in the directory on terminal by a C program? Maybe I can use exec function to run find command but I want file name as a string to send client program. How can I do this? Thanks for answers.

4 Answers 4

An example, available for POSIX compliant systems :

/* * This program displays the names of all files in the current directory. */ #include #include int main(void) < DIR *d; struct dirent *dir; d = opendir("."); if (d) < while ((dir = readdir(d)) != NULL) < printf("%s\n", dir->d_name); > closedir(d); > return(0); > 

Beware that such an operation is platform dependent in C.

I encounter few problems with this. First, «.» and «..» appear at the top of every directory, and although they are «directories» they have the dir->d_type set to DT_REG. Also, I dont seem to get all the files. Is there a more comprehensive «directory scanner» code somewhere? Maybe some poor-mans implementation of «ls» ? I need this on Mac — OS-X

This is missing error handling. For that, you would need to set errno = 0 before each operation and always check if once an operation returns NULL (see man pages of opendir and readdir). closedir on the other hand returns 0 when an error occurs.

One tiny addition to JB Jansen’s answer — in the main readdir() loop I’d add this:

 if (dir->d_type == DT_REG) < printf("%s\n", dir->d_name); > 

Just checking if it’s really file, not (sym)link, directory, or whatever.

NOTE: more about struct dirent in libc documentation.

Just an aside: Not all platforms will fill out d_type , but Linux and BSD will (I know the question is tagged Linux, just extending on the answer a little); even then, not all filesystems are supported uniformly, however it should work with most FSs.

Here is a complete program how to recursively list folder’s contents:

#include #include #include #define NORMAL_COLOR "\x1B[0m" #define GREEN "\x1B[32m" #define BLUE "\x1B[34m" /* let us make a recursive function to print the content of a given folder */ void show_dir_content(char * path) < DIR * d = opendir(path); // open the path if(d==NULL) return; // if was not able, return struct dirent * dir; // for the directory entries while ((dir = readdir(d)) != NULL) // if we were able to read somehting from the directory < if(dir->d_type != DT_DIR) // if the type is not directory just print it with blue color printf("%s%s\n",BLUE, dir->d_name); else if(dir -> d_type == DT_DIR && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0 ) // if it is a directory < printf("%s%s\n",GREEN, dir->d_name); // print its name in green char d_path[255]; // here I am using sprintf which is safer than strcat sprintf(d_path, "%s/%s", path, dir->d_name); show_dir_content(d_path); // recall with the new path > > closedir(d); // finally close the directory > int main(int argc, char **argv)

Источник

Оцените статью
Adblock
detector