|
For those who use the Linux command line is, 'pwd' command is very useful, it tells you that you are now in the directory from the root directory (/) how to reach. Especially for switching between directories might easily confused Linux novice, 'pwd' can save them.
What is pwd?
'Pwd' stands for 'Print Working Directory' (Print catalog). As its name implies, 'pwd' will print out the current working directory, or simply means that the user is currently located. It will print a complete directory name (absolute directory) to the root directory (/) as a starting point. This command is a shell built-in commands, and can be used in most of the shell, such as bash, Bourne shell, ksh, zsh, and so on.
pwd basic syntax:
# Pwd [OPTION]
pwd option
Option Description
-L (Ie logical path logical) path using the environment, even if it contains a symbolic link
-P (Ie, the physical path physical) to avoid all symlinks
-help display this help and exit
-version output version information and exit
If you use the '-L' and '-P', '- L' will have a higher priority. If you do not specify the parameters, pwd will avoid all soft link, that will use the '-P' parameter.
pwd exit status:
0 Success
Nonzero value failed
The purpose of this Title is to use examples to get you 'pwd' have a deeper insight.
1. Print the current working directory.
avi @ tecmint: ~ $ / bin / pwd
/ Home / avi
2. Create a symbolic link to the folder (for example, htm create a link to the / var / www / html in the home directory). Into the newly created directory and print out containing symbolic links and free directory.
Htm create a link to the / var / www / html in the home directory and enter.
avi @ tecmint: ~ $ ln -s / var / www / html / htm
avi @ tecmint: ~ $ cd htm
3. Print Directory even though it contains a symbolic link from the current environment.
avi @ tecmint: ~ $ / bin / pwd -L
/ Home / avi / htm
4. Parse the symbolic link and print out the physical directory.
avi @ tecmint: ~ $ / bin / pwd -P
/ Var / www / html
5. check "pwd" and "pwd -P" output is consistent, that is, if there is no option to keep up, "pwd" automatically when using the -P option.
avi @ tecmint: ~ $ / bin / pwd
/ Var / www / html
Conclusion: The above example output 4 and 5, it is clear (same result), when you "pwd" Without an argument later, pwd will use the "-P" option.
6. Print pwd command version.
avi @ tecmint: ~ $ / bin / pwd --version
pwd (GNU coreutils) 8.23
Copyright (C) 2014FreeSoftwareFoundation, Inc.
LicenseGPLv3 +: GNU GPL version 3or later < http://gnu.org/licenses/gpl.html>.
Thisis free software: you are free to change and redistribute it.
Thereis NO WARRANTY, to the extent permitted by law.
WrittenbyJimMeyering.
Note: 'pwd' generally run with no options and no arguments
Important: You may have noticed that we just are running "/ bin / pwd" instead of "pwd".
What's the difference? Direct use "pwd" means using shell built-in pwd. Your shell may have a different version of pwd. For details, please refer to the manual. When you use the / bin / pwd, we call the binary version of the command. Although the binary version has more options, but they both can print the current directory.
7. Print all paths containing the executable pwd
avi @ tecmint: ~ $ type -a pwd
pwd is a shell builtin
pwd is / bin / pwd
8. The value of the variable to store "pwd" command (for example: a), and print from the value of the variable (for observation shell script is very important).
avi @ tecmint: ~ $ a = $ (pwd)
avi @ tecmint: ~ $ echo "Current working directory is: $ a"
Current working directory is: / home / avi
The following examples can also use printf instead.
9. The working path is switched to another place (for example, / home), and displayed in the command line. By executing the command (for example, 'ls') to verify everything OK.
avi @ tecmint: ~ $ cd / home
avi @ tecmint: ~ $ PS1 = '$ pwd>' [Note the example of single quotes]
> Ls
10. Set the multi-line display (like this below),
/ Home
123 # Hello #!
Then execute the command (for example, ls) to test everything OK.
avi @ tecmint: ~ $ PS1 = '
> $ PWD
$ 123 # Hello #!
$ '
/ Home
123 # Hello #!
11. Check the current working directory and all of a sudden the previous working path.
avi @ tecmint: ~ $ echo "$ PWD $ OLDPWD"
/ Home / home / avi
12. pwd absolute path of the file (in / start).
/ Bin / pwd
13. pwd absolute path to the source file (in / start).
/usr/include/pwd.h
Absolute path 13. pwd manual (start with /).
/usr/share/man/man1/pwd.1.gz
15. Write a shell script that analyzes a directory in your home directory (eg tecmint). If the current directory is tecmint outputs "Well! You are in tecmint directory" then output "Good Bye", otherwise they create a directory tecmint below and prompts you cd into it.
Let's first create a 'tecmint' directory, create a script file called 'pwd.sh' in the following.
avi @ tecmint: ~ $ mkdir tecmint
avi @ tecmint: ~ $ cd tecmint
avi @ tecmint: ~ $ nano pwd.sh
Next, add the following script pwd.sh in.
#! / Bin / bash
x = "$ (pwd)"
if [ "$ x" == "/ home / $ USER / tecmint"]
then
{
echo "Well you are in tecmint directory"
echo "Good Bye"
}
else
{
mkdir / home / $ USER / tecmint
echo "Created Directory tecmint you may now cd to it"
}
fi
Give execute permissions and run.
avi @ tecmint: ~ $ chmod 755 pwd.sh
avi @ tecmint: ~ $ ./pwd.sh
Well you are in tecmint directory
GoodBye
to sum up
pwd is one of the most simple and will be widely used in the command. Master pwd is the basis for the use of Linux terminal. That's it. I'll keep you bring another interesting article, please do not go away Stay tuned. |
|
|
|