Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Skills of using pwd Command in Linux system

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "skills of using pwd commands in the Linux system". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the skills of using pwd commands in the Linux system.

What is pwd?

Pwd stands for 'Print Working Directory' (print the current directory). As its name suggests, 'pwd' prints out the current working directory, or simply the directory where the current user is located. It prints out the full directory name (absolute directory) starting with the root directory (/). This command is a shell built-in command and can be used in most shell, such as bash, Bourne shell,ksh, zsh, and so on.

The basic syntax of pwd:

# pwd [OPTION]

Options for pwd

Higher priority will be given to using both'- L 'and'-Paddy'. If no argument is specified, pwd avoids all soft links, that is, it uses the'- P 'parameter.

Exit status of pwd:

The purpose of this article is to use examples to give you a deeper understanding of pwd'.

1. Print the current working directory.

The code is as follows:

Avi@tecmint:~$ / bin/pwd

/ home/avi

two。 Create a symbolic link for the folder (for example, create a htm link to / var/www/html in the home directory). Enter the newly created directory and print out the directory with and without symbolic links.

Create a htm link to / var/www/html in the home directory and enter.

The code is as follows:

Avi@tecmint:~$ ln-s / var/www/html/ htm

Avi@tecmint:~$ cd htm

3. Prints a directory from the current environment even if it contains symbolic links.

The code is as follows:

Avi@tecmint:~$ / bin/pwd-L

/ home/avi/htm

4. Parse the symbolic link and print out the physical directory.

The code is as follows:

Avi@tecmint:~$ / bin/pwd-P

/ var/www/html

5. Check to see if the output of "pwd" and "pwd-P" is the same, that is, if you don't keep up with the option, the-P option will be used automatically for "pwd".

The code is as follows:

Avi@tecmint:~$ / bin/pwd

/ var/www/html

Conclusion: the output of examples 4 and 5 above is obvious (the result is the same). When you "pwd" without parameters, pwd will use the "- P" option.

6. Print the version of the pwd command.

The code is as follows:

Avi@tecmint:~$ / bin/pwd-- version

Pwd (GNU coreutils) 8.23

Copyright (C) 2014 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later.

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Written by Jim Meyering.

Note: 'pwd' usually runs without options and does not have any parameters

Important: you may have noticed that we just ran "/ bin/pwd" instead of "pwd".

What's the difference? Using "pwd" directly means using shell's built-in pwd. Your shell may have different versions of pwd. Please refer to the manual for details. When you use / bin/pwd, we call the binary version of the command. Although the binary version has more options, both of them can print the current directory.

7. Print all paths that contain executable pwd

The code is as follows:

Avi@tecmint:~$ type-a pwd

Pwd is a shell builtin

Pwd is / bin/pwd

8. Store the value of the "pwd" command into a variable (for example: a) and print the value of the variable from it (important for observing the shell script).

The code is as follows:

Avi@tecmint:~$ aversion $(pwd)

Avi@tecmint:~$ echo "Current working directory is: $a"

Current working directory is: / home/avi

Printf can also be used instead in the following example.

9. Switch the work path to something else (such as / home) and display it on the command line. Verify all OK by executing commands such as' ls''.

The code is as follows:

Avi@tecmint:~$ cd / home

Avi@tecmint:~$ PS1='$pwd >'[note the single quotation marks in the example]

> ls

10. Set up multiline display (like the following)

The code is as follows:

/ home

123#Hello#!

Then execute a command (such as ls) to verify all OK.

The code is as follows:

Avi@tecmint:~$ PS1='

> $PWD

$123 extra Hellobread!

$'

/ home

123#Hello#!

11. Check the current work path and the previous work path at once.

The code is as follows:

Avi@tecmint:~$ echo "$PWD $OLDPWD"

/ home/ home/avi

12. The absolute path to the pwd file (starting with /).

The code is as follows:

/ bin/pwd

13. The absolute path of the pwd source file (starting with /).

The code is as follows:

/ usr/include/pwd.h

14. The absolute path of the pwd manual (start with /).

The code is as follows:

/ usr/share/man/man1/pwd.1.gz

15. Write a shell script to analyze a directory under the home directory (such as tecmint). If the current directory is tecmint, output "Well! You are in tecmint directory" followed by "Good Bye", otherwise create a directory under tecmint and prompt you to cd to enter it.

Let's first create a 'tecmint' directory and a script file called' pwd.sh' 'below.

The code is as follows:

Avi@tecmint:~$ mkdir tecmint

Avi@tecmint:~$ cd tecmint

Avi@tecmint:~$ nano pwd.sh

Next, add the following script to pwd.sh.

The code is as follows:

#! / 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 permission and run.

The code is as follows:

Avi@tecmint:~$ chmod 755 pwd.sh

Avi@tecmint:~$. / pwd.sh

Well you are in tecmint directory

Good Bye

Thank you for your reading, the above is the content of "skills for using pwd commands in the Linux system". After the study of this article, I believe you have a deeper understanding of the skills of using pwd commands in the Linux system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report