Linux show current directory

Commandline shortcut for current directory similar to ~ for home directory?

When i use the cp or move command in a terminal window, i’m currently with bash in a certain folder like this.

And now i wanna copy a file from here ~/anotherdir/dir2 into the current chosen folder in bash (dir1) i would use the command

cp ~/anotherdir/dir2/file ~/Desktop/dir1 

does a shortcut string exist to refer to the current chosen directory? So that in this example i don’t have to provide the full path to the target dir, but the command knows it should use the current chosen directory in bash? i.e. as ~ stands for the home directory?

8 Answers 8

Your current directory is . . So, cp /foo/fam/foo . copies the file to your current directory.

The analogous construction for «one directory up,» or the parent directory of your current working directory, is two dots, i.e., .. . (Thanks @djeikyb .)

So, from /usr/house/firstfloor/basement , cd .. takes you one level up to /usr/house/firstfloor .

In the same example (starting from /usr/house/firstfloor/basement , the command cd ../.. would take you to /usr/house .

You can also use $PWD with echo to get your current directory:

Incidentally, $OLDPWD will give you your previous directory. (Which in bash you can also reach by typing cd — .)

Good a time as any to learn that .. means one directory lower. Can be used multiple times, ie cd ../..

@djeikyb — you meant above, I’m sure. So, cd .. will take you to the parent directory of your current directory. Good point.

One small difference to remember: . and .. are filesystem-level, so any program will accept them. On the other hand, shortcuts such as ~ and ~- are part of the shell.

You can use $(pwd), it will resolve to the output from the pwd command.

./ represents the current directory. So you can use command cp ~/anotherdir/dir2/file ./ This will copy the file «file» into currect working directory.

You don’t need the slash, «.» is enough too. slash (as path component delimiter) is only needed if you want to put some other path component after it, but then it’s easier to just left ./ since relative paths are meant starting with the current directory anyway 🙂

i know / is not required but . alone feels confusing to me coz . is also used to execute commands from a file in current shell e.g. «. envsetup.sh». So adding a / at the end just makes it clearer.

Tangentially, if you do ln -s /some/long/path/to/get/to/cake with no second argument, it will put the cake symbolic link in your current directly. Very good for the lazy. Saves a . .

To use the current directory as the destination directory use a single dot ‘ .

Long Answer

Using your example you would type: cp ~/anotherdir/dir2/file .

To see the dot . , .. and ../../ directory names in action, copy and paste the following commands into your Terminal:

mkdir a && mkdir a/b && mkdir a/b/c && mkdir a/b/c2 cd a/b/c cp /etc/default/grub . cp /etc/default/grub .. cp /etc/default/grub ../c2 cd ../../ tree 

The output from tree command appears like this:

. └── b ├── c │ └── grub ├── c2 │ └── grub └── grub 3 directories, 3 files 

The . at the top of tree output represents the new current directory a which is the grandparent of a/b/c which we navigated to using the cd ../../ command. Underneath a we see the sub-directories a/b , a/b/c and a/b/c2

Читайте также:  Linux manjaro обновление ядра

Line by line analysis

First we created 4 directories on one line by using && to join multiple lines together.

Then we changed to the directory a/b/c , which is the current directory for the following copy commands:

  • In the first copy command ( cp ) we set the destination to our current directory (c) with . .
  • In the second copy command we set the destination to the parent directory (b) with .. .
  • In the third copy command we set the destination to the sibling directory (c2) with ../c2

Then, as stated earlier, we changed the current directory to a and ran the tree command to display all directories and files under a .

Cleanup

After we are done, we remove the three directories and files with:

The environment variable for the current directory is $PWD

You can use the dot ( . ), the ~+ tilde expansion, the pwd command or the $PWD variable to represent the current working directory (CWD). All these commands can do that:

  1. mv file_old_dir .
  2. mv file_old_dir ~+
  3. mv file_old_dir $(pwd)
  4. mv file_old_dir $PWD

Yes (as others noted), current directory is «.», that’s why you can start programs/script from the current directory with ./script (just script won’t work unless . is not the part of PATH, which is not recommended though). Using $PWD or $(pwd) is a bit overkill, even if others mentioned that, using a single dot character is shorter, for sure 🙂 «..» is the parent directory, for sure «/» is the root. Also nice to mention that «cd -» will put you in the previous directory where you were before you changed cwd (current working directory). It can be also useful in the daily work. A single «cd» command without any other in the command line will put you into «~» (your home).

I thought about the «cp» example where it is, for sure. Even with symlinks it depends on the situation: «ln -s /this/file .» will create a symlink in the current directory pointing file /this/file with name «file» (people often think it’s like with «cp» just not copy data, only some kind of link: it’s not a correct definition but it’s a good start). However, the opposite construction: «ln -s . /this/dir» is a somewhat interesting (create a symlink as /this/dir with refers for the current directory), but I think it’s out of the scope of the original topic, and it was not the question either.

cp ~/anotherdir/dir2/file `pwd` 

The back ticks mean to run the command that is between them and replace it all by the resulting output, in this case the working directory («pwd» means «print working directory»).
Of course, I prefer the «.» version. Added this one only for completeness.

Читайте также:  Astra linux поставить wine

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How can I get the current working directory? [duplicate]

I want to have a script that takes the current working directory to a variable. The section that needs the directory is like this dir = pwd . It just prints pwd how do I get the current working directory into a variable?

This is not a duplicate of the question for which it is currently marked as one. The two questions should be compared, at least, based on their titles (as well as their answers). That the answer to this question is already covered by another is, or should be, irrelevant.

@KennyEvitt actually, one of the main reasons we close is precisely because an answer has been given elsewhere. And, in fact, the main question here is actually how to assign the output of a command to a variable, which is covered by the dupe. I have also given the answer to this specific case, so all bases are covered. There would be no benefit in opening this again.

@terdon As a resource available, and intended, for the entire population of Unix & Linux users, this is a valuable question, even if the original asker really just needed an answer already covered elsewhere. If anything, I think this question should be edited to more closely match its title and it should be re-opened, not to allow further activity, but to not imply that this question is ‘bad’.

@KennyEvitt closing as a duplicate in no way implies that the question is bad! This question will remain here, answered, for ever. If you really want to know how to get the current working directory, you will find your answer here. If you just want to know how to save the output of a command in a variable, you will also find the answer here by following the link to the dupe. In any case, this isn’t really something I should do alone, if you feel strongly that it should be reopened, please open a discussion on Unix & Linux Meta where such things should be resolved.

5 Answers 5

There’s no need to do that, it’s already in a variable:

The PWD variable is defined by POSIX and will work on all POSIX-compliant shells:

Set by the shell and by the cd utility. In the shell the value shall be initialized from the environment as follows. If a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory that is no longer than bytes including the terminating null byte, and the value does not contain any components that are dot or dot-dot, then the shell shall set PWD to the value from the environment. Otherwise, if a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory, and the value does not contain any components that are dot or dot-dot, then it is unspecified whether the shell sets PWD to the value from the environment or sets PWD to the pathname that would be output by pwd -P. Otherwise, the sh utility sets PWD to the pathname that would be output by pwd -P. In cases where PWD is set to the value from the environment, the value can contain components that refer to files of type symbolic link. In cases where PWD is set to the pathname that would be output by pwd -P, if there is insufficient permission on the current working directory, or on any parent of that directory, to determine what that pathname would be, the value of PWD is unspecified. Assignments to this variable may be ignored. If an application sets or unsets the value of PWD, the behaviors of the cd and pwd utilities are unspecified.

For the more general answer, the way to save the output of a command in a variable is to enclose the command in $() or ` ` (backticks):

Читайте также:  Linux start program in background

Of the two, the $() is preferred since it is easier to build complex commands like:

command0 "$(command1 "$(command2 "$(command3)")")" 

Whose backtick equivalent would look like:

command0 "`command1 \"\`command2 \\\"\\\`command3\\\`\\\"\`\"`" 

Источник

How to display the current working directory in the Linux system?

To print the current working directory, we use the pwd command in the Linux system.

pwd (print working directory) – The pwd command is used to display the name of the current working directory in the Linux system using the terminal. This is a shell building command that is available in most Unix shells such as Bourne shell, ash, bash, kash, and zsh.

Syntax

The general syntax of the pwd command is as follows −

A brief description of options available in the pwd command.

Sr.No. Option & Description
1 -L (logical)
Display the value of $pwd if it names the current working directory
2 -P (physical)
Display the physical directory, without any soft link
3 —help
Displays a help message and then exits.

By default, the pwd works as if -L option was specified.

Exit Status

The pwd command returns true unless an invalid option supplied or the current directory could not be read.

To display the current working directory, we use the pwd command in the Linux/Unix system as shown below.

vikash@tutorialspoint:~ pwd /home/vikash

To display the physical directory instead of symbolic links or soft links, we use -P option with the pwd command in the Linux/Unix system as shown below.

vikash@tutorialspoint:~ pwd -P /home/vikash

To display more about the pwd command we use -help option with the pwd command as shown below.

vikash@tutorialspoint:~ pwd --help

After execution of above command. It will be prompt a short description with available options available in the pwd command.

Источник

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