What is working directory in linux

What is $PWD? (vs current working directory)

So Wikipedia (link) tells me that the command pwd is short for «print working directory», and that makes sense. But for the environment variable, the «P» has to be an acronym for something else than print. I hear people talking about «current working directory», which sounds better and is more intuitive, but still the environment variable seems to be called $PWD, and not $CWD. Nobody ever says «Did you check the print working directory variable?». I am currently playing around with the web application server uWSGI, and when running it tells me (on the uWSGI stats page):

so they obviously like the (more intuitive acronym) cwd over pwd . I guess I am trying to figure out if I misunderstood something, or if it is just a matter of having given the environment variable an unintuitive name?

I suspect you’re just overthinking it. $PWD is equivalent to `pwd` , and that was justification enough.

2 Answers 2

That depends on what you’re doing. First of all, $PWD is an environment variable and pwd is a shell builtin or an actual binary:

$ type -a pwd pwd is a shell builtin pwd is /bin/pwd 

Now, the bash builtin will simply print the current value of $PWD unless you use the -P flag. As explained in help pwd :

    Print the name of the current working directory.

    Options:

      -L
          print the value of $PWD if it names the current working directory
            print the physical directory, without any symbolic links

          The pwd binary, on the other hand, gets the current directory through the getcwd(3) system call which returns the same value as readlink -f /proc/self/cwd . To illustrate, try moving into a directory that is a link to another one:

          $ ls -l total 4 drwxr-xr-x 2 terdon terdon 4096 Jun 4 11:22 foo lrwxrwxrwx 1 terdon terdon 4 Jun 4 11:22 linktofoo -> foo/ $ cd linktofoo $ echo $PWD /home/terdon/foo/linktofoo $ pwd /home/terdon/foo/linktofoo $ /bin/pwd /home/terdon/foo/foo 

          So, in conclusion, on GNU systems (such as Ubuntu), pwd and echo $PWD are equivalent unless you use the -P option, but /bin/pwd is different and behaves like pwd -P .

          Источник

          Home directory vs Working directory

          Your home directory is where you go to rest between work sessions. Your working directory is where you are right now. Because unix systems are multitasking, each process has its own working directory; processes can change directories, too.

          2 Answers 2

          The home directory is a directory associated with a user name in the system’s user account database. It’s the 6th field in the entry returned by getent passwd some-user .

          When you log in, the HOME environment variable is initialized from that value and applications use that variable or query the user database to know what your home directory is.

          That directory is usually owned and writable by the corresponding user, and it’s typically where applications store the user settings and where the user stores their files.

          In shells, ~ expands to your home directory, and ~user to the home directory of user user .

          The kernel has no notion of what a user name or home directory is. The home directory is entirely a user-space concept.

          On the other hand, the working directory is an attribute of each process. It can be changed with the chdir(2) / fchdir(2) system calls (and the cd / pushd / popd commands in a shell) and queried using getcwd() or pwd in a shell. The current working directory is the base for finding a file using a relative path. A relative path (as in «foo/bar.txt») is relative to the current working directory of a process.

          The working directory is inherited upon a fork and preserved upon execution of a command. For instance, ls lists the content of its working directory which is the same as the working directory of the shell that called it.

          When a user logs in, the working directory of the first process that is run under his name is set to his home directory, and as a result, unless anything changes it, every process started under that login session will have the home directory as the current working directory.

          shells don’t start in the home directory, they start wherever the current directory was when they were executed, though shells started by a terminal emulator itself started by a window manager itself started by the login manager will likely start in the home directory since there’s no reason why the window manager or terminal emulator would change their working directory.

          Источник

          In this lesson, we will introduce our first three commands: pwd (print working directory), cd (change directory), and ls (list files and directories).

          Those new to the command line will need to pay close attention to this lesson since the concepts will take some getting used to.

          File System Organization

          Like Windows, the files on a Linux system are arranged in what is called a hierarchical directory structure. This means that they are organized in a tree-like pattern of directories (called folders in other systems), which may contain files and subdirectories. The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and subdirectories and so on and so on.

          directory tree

          Most graphical environments include a file manager program used to view and manipulate the contents of the file system. Often we will see the file system represented like this:

          One important difference between Windows and Unix-like operating systems such as Linux is that Linux does not employ the concept of drive letters. While Windows drive letters split the file system into a series of different trees (one for each device), Linux always has a single tree. Different storage devices may be different branches of the tree, but there is always just a single tree.

          pwd

          Since the command line interface cannot provide graphic pictures of the file system structure, we must have a different way of representing it. To do this, think of the file system tree as a maze, and that we are standing in it. At any given moment, we are located in a single directory. Inside that directory, we can see its files and the pathway to its parent directory and the pathways to the subdirectories of the directory in which we are standing.

          The directory we are standing in is called the working directory. To see the name of the working directory, we use the pwd command.

          When we first log on to our Linux system, the working directory is set to our home directory. This is where we put our files. On most systems, the home directory will be called /home/user_name, but it can be anything according to the whims of the system administrator.

          To list the files in the working directory, we use the ls command.

          [me@linuxbox me]$ ls Desktop Downloads foo.txt Pictures Templates Documents examples.desktop Music Public Videos

          We will come back to ls in the next lesson. There are a lot of fun things you can do with it, but we have to talk about pathnames and directories a bit first.

          cd

          To change the working directory (where we are standing in the maze) we use the cd command. To do this, we type cd followed by the pathname of the desired working directory. A pathname is the route we take along the branches of the tree to get to the directory we want. Pathnames can be specified two different ways; absolute pathnames or relative pathnames. Let’s look with absolute pathnames first.

          An absolute pathname begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed. For example, there is a directory on your system in which most programs are installed. The pathname of the directory is /usr/bin . This means from the root directory (represented by the leading slash in the pathname) there is a directory called «usr» which contains a directory called «bin».

          me@linuxbox me]$ cd /usr/bin me@linuxbox bin]$ pwd /usr/bin me@linuxbox bin]$ ls ‘[‘ mshortname 2to3-2.7 mshowfat 411toppm mtools a2ps mtoolstest a2ps-lpr-wrapper mtr aa-enabled mtrace aa-exec mtr-packet aclocal mtvtoppm aclocal-1.15 mtype aconnect mutter acpi_listen mxtar add-apt-repository mzip addpart namei and many more.

          Now we can see that we have changed the current working directory to /usr/bin and that it is full of files. Notice how the shell prompt has changed? As a convenience, it is usually set up to display the name of the working directory.

          Where an absolute pathname starts from the root directory and leads to its destination, a relative pathname starts from the working directory. To do this, it uses a couple of special notations to represent relative positions in the file system tree. These special notations are «.» (dot) and «..» (dot dot).

          The «.» notation refers to the working directory itself and the «..» notation refers to the working directory’s parent directory. Here is how it works. Let’s change the working directory to /usr/bin again:

          O.K., now let’s say that we wanted to change the working directory to the parent of /usr/bin which is /usr . We could do that two different ways. First, with an absolute pathname:

          Or, with a relative pathname:

          Two different methods with identical results. Which one should we use? The one that requires the least typing!

          Likewise, we can change the working directory from /usr to /usr/bin in two different ways. First using an absolute pathname:

          Or, with a relative pathname:

          Now, there is something important that we must point out here. In most cases, we can omit the «./». It is implied. Typing:

          would do the same thing. In general, if we do not specify a pathname to something, the working directory will be assumed. There is one important exception to this, but we won’t get to that for a while.

          A Few Shortcuts

          If we type cd followed by nothing, cd will change the working directory to our home directory.

          A related shortcut is to type cd ~user_name . In this case, cd will change the working directory to the home directory of the specified user.

          Typing cd — changes the working directory to the previous one.

          Important facts about file names

          1. File names that begin with a period character are hidden. This only means that ls will not list them unless we say ls -a . When your account was created, several hidden files were placed in your home directory to configure things for your account. Later on we will take a closer look at some of these files to see how you can customize our environment. In addition, some applications will place their configuration and settings files in your home directory as hidden files.
          2. File names in Linux, like Unix, are case sensitive. The file names «File1» and «file1» refer to different files.
          3. Linux has no concept of a «file extension» like Windows systems. You may name files any way you like. However, while Linux itself does not care about file extensions, many application programs do.
          4. Though Linux supports long file names which may contain embedded spaces and punctuation characters, limit the punctuation characters to period, dash, and underscore. Most importantly, do not embed spaces in file names. If you want to represent spaces between words in a file name, use underscore characters. You will thank yourself later.

          © 2000-2023, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved.

          Linux® is a registered trademark of Linus Torvalds.

          Источник

          Читайте также:  Zyxel nwd6605 драйвер linux
Оцените статью
Adblock
detector