Which command in the Linux/UNIX sh shell returns my current directory?

37

5

Using the sh shell (not bash), which command in Linux/UNIX prints out my current directory?

$ *showmewhereiam*
/sys/kernel/debug
$

Hieu Nguyen

Posted 2011-06-21T10:31:01.247

Reputation: 805

Funnily enough, this question IS the answer to the simple google search for unix whereami – Jay Killeen – 2017-09-28T01:02:32.693

4A simple google search would have given the answer,wont it? – Mad-scientist – 2011-06-21T11:12:24.937

9

@Mad-scientist While this may be true, it defeats the purpose of a question/answer site to tell people to go to Google. 90% of our traffic is from web searches.

– slhck – 2011-06-21T11:19:02.797

2http://blog.stackoverflow.com/2011/02/are-some-questions-too-simple/ – Mad-scientist – 2011-06-21T11:29:01.337

2Nothing is too simple for SEO! – Kenzo – 2013-11-18T10:24:18.637

Answers

60

Try pwd.

$ pwd
/home/<username>

loudandclear

Posted 2011-06-21T10:31:01.247

Reputation: 897

2Oh, dev means device?! Always thought up until now it just meant dev... I thought dev/null was where linux developers put the special null object. Thanks @LiuYan刘研 – 1owk3y – 2016-06-15T00:04:39.817

@LiuYan刘研 - I totally agree it's confusing if you don't know. I had to look it up. It stands for "Print Working Directory" for anyone else who's baffled by why they would choose that command for that functionality. – squarecandy – 2017-01-12T19:27:39.353

1

@squarecandy, That's why I will avoid using acronyms as possible as I can when grown older and older, even the acronym doesn't have ambiguous meaning at this time -- You never know what meaning it will have in the future. There's a joke said by Jeff Dunham's doll Walter: What happens in D.C. stays on YouTube!, where D.C. means Digital Camera but also means Washington, District of Columbia in the joke.

– LiuYan 刘研 – 2017-01-13T06:18:47.933

6when i first touch linux, i thought it's a command about password and /dev is something about development (source codes or something like that) – LiuYan 刘研 – 2011-06-22T03:59:33.137

11

While the general answer is pwd, note that this may give different results depending on how you reached a given directory, and whether the route included symbolic links.

For instance, if you have a directory called real and a symbolic link to that directory called virtual, and you cd to the virtual directory, then pwd will show that virtual directory name, even though the actual directory you are in is real.

To show the real underlying directory, use either pwd -P or readlink -f (for a arbitrary path):

$ mkdir real
$ ln -s real virtual
$ cd virtual
$ pwd
/home/username/tmp/virtual
$ pwd -P
/home/username/tmp/real
$ readlink -f .
/home/username/tmp/real

Note that shells often replace the pwd command with their own internal version, so on my system (RHEL6), even though the pwd(1) manual page suggests that --physical will work as well as -P, because I'm running bash, it doesn't:

$ pwd --physical
bash: pwd: --: invalid option
pwd: usage: pwd [-LP]
$ /bin/pwd --physical
/home/username/tmp/real
$ /usr/bin/env pwd --physical
/home/username/tmp/real

Mark Booth

Posted 2011-06-21T10:31:01.247

Reputation: 2 324

1

$ pwd
/your/current/directory
$

pwd comes from the print working directory.

From Linux man page:

Name

pwd - print name of current/working directory

Synopsis

pwd [OPTION]...

Description

Print the full filename of the current working directory.

-L, --logical

use PWD from environment, even if it contains symlinks

-P, --physical

avoid all symlinks

--help

display this help and exit

--version

output version information and exit

NOTE: your shell may have its own version of pwd, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports.

(...)

simhumileco

Posted 2011-06-21T10:31:01.247

Reputation: 431

This duplicates information in the accepted answer from over five years ago. Can you add anything else?

– bertieb – 2017-02-20T10:24:40.660

1

As others said, pwd usually does the job well enough. However, I'd like to add an idea which has helped me out.

On all shells in common use today, you're able to customize the appearance of the command prompt. I like to customize mine so that it shows me both the name of the computer I'm on and my working directory. That way, I always know where I am. (The computer name part helps me realize if the terminal window I'm using has been used to SSH into a remote server.) For example, when I open a new terminal window on my laptop, which I call Plastico, I see this:

Plastico ~> cd Desktop/
Plastico ~/Desktop> cd ~/Sites/raygunrobot.com
Plastico ~/Sites/raygunrobot.com> cd /usr
Plastico /usr> 

You can see that it's abbreviating my home directory as ~. I find doing this for my terminal prompts is incredibly handy - I always know at a glance exactly where I am, even more so than I would by looking at a Finder window.

On tcsh, I do this by adding set prompt = 'Plastico %~%# ' to my .tcshrc file in my home directory. I've done it on ksh too by adding export PS1="Plastico $PWD $ " to my .profile file. But being a Linux user, you probably use Bash, which I don't have much experience with (I'm a weirdo like that). But it looks like I might be able to do it by adding PS1="Plastico \w $ " to my .bashrc file.

Edit: Just noticed the OP states you're interested in sh, not bash. Oh well; hope folks find this useful regardless.

Garrett Albright

Posted 2011-06-21T10:31:01.247

Reputation: 177

That's a cool idea. Might work well for a simple desktop use case. Seems like it might get unwieldily for for super deep directories if your prompt alone is Plastico /var/www/vhosts/myveryexcellentwebsite.com/public_html/wp-content/themes/myveryexcellenttheme/js/vendor/excellentjs/ > or something like that. – squarecandy – 2017-01-13T17:07:04.310