4 ways to get the current directory name in Linux __linux

Source: Internet
Author: User
Reproduced in this article from: http://www.linuxdiyf.com/viewarticle.php?id=66575 when writing a shell script, sometimes we need to get the name of the current directory, the PWD command can return the full path of the current directory, Instead of the directory name, if you want to get the directory name, there are 4 methods: Method One, using the basename command to use basename is the easiest way: Example 1.0 basename/usr/local/bin Result: Bin BaseName and PWD, can be obtained to the current directory. Example 1.1:cd/usr/local/bin basename ' pwd ' results: Bin method Two, using # # split pwd variable ${var# #pattern} can remove strings that match pattern maximum length from $var variables, so , to obtain the directory name, you can: Sample 2.0:cd/var/log/squid echo ${pwd##*/} result: Squid $PWD is the environment variable to save the full path of the directory, the pattern is * * *, that is to remove all the previous ancestor directory. Method three, using awk and Rev Samples 3.0:cd/usr/share/cups/data PWD | Rev | Awk–f \/' {print $} ' | Rev results: Data to show the results step-by-step, you know the rationale: Pwd/usr/share/cups/data pwd | Rev Atad/supc/erahs/rsu/pwd | Rev | Awk–f \/' {print $} ' Atad pwd | Rev | Awk–f \/' {print $} ' | The Rev data-f option is to specify the separator and the separator between directories is/. Method four, using the SED example 4.0 cd/home/smith/music pwd | Sed ' s,^\ (. */\) \?\ ([^/]*\), \2, ' Result: Music the first pattern,^\ (. */\)?, match/symbol before all things, second [^/]*, match the remainder of the part not included/.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.