linux怎么获取当前路径

在Linux系统中,获取当前路径是很常见的操作,无论是在命令行界面还是脚本编程中,我们都需要知道当前的工作目录,本文将详细介绍如何在Linux系统中获取当前路径。

1. 使用pwd命令

linux怎么获取当前路径

pwd是"Print Working Directory"的缩写,意为打印工作目录,这个命令可以帮助我们获取当前所在的工作目录,在终端中输入以下命令:

pwd

执行后,终端会显示当前工作目录的绝对路径

/home/user/Documents

2. 使用echo $PWD命令

$PWD是一个特殊的环境变量,它表示当前工作目录,我们可以使用echo命令来输出这个环境变量的值,从而获取当前路径,在终端中输入以下命令:

echo $PWD

执行后,终端同样会显示当前工作目录的绝对路径

/home/user/Documents

3. 使用readlink -f命令

readlink -f命令可以解析符号链接,并返回符号链接指向的文件或目录的绝对路径,我们可以使用这个命令来获取当前工作目录的绝对路径,在终端中输入以下命令:

linux怎么获取当前路径

readlink -f .

执行后,终端会显示当前工作目录的绝对路径,

/home/user/Documents

4. 使用cd命令和pwd命令结合

我们还可以通过切换到其他目录,然后使用pwd命令来获取当前路径,在终端中输入以下命令:

cd /path/to/directory && pwd

执行后,终端会显示指定目录的绝对路径,

/path/to/directory

5. 使用dirname $0命令

在脚本编程中,我们可以使用$0特殊变量来获取脚本自身的绝对路径,我们可以使用dirname命令来获取脚本所在目录的绝对路径,在脚本中输入以下命令:

!/bin/bash
dir=$(dirname $0)
echo $dir

执行后,脚本会输出脚本所在目录的绝对路径,

linux怎么获取当前路径

/home/user/Documents/scripts

6. 使用环境变量$HOME$PWD结合

我们还可以通过结合环境变量$HOME$PWD来获取当前用户的主目录,在终端中输入以下命令:

echo $HOME/$(basename $(pwd))

执行后,终端会显示当前用户主目录下与当前工作目录同名的子目录的绝对路径,

/home/user/Documents

7. 使用ls -l | grep "^d"命令组合

我们还可以通过组合多个命令来获取当前路径,我们可以使用ls -l命令列出当前目录下的所有文件和子目录,我们可以使用grep "^d"命令过滤出以"d"开头的行,这些行表示目录,我们可以使用管道符(|)将这两个命令连接起来,在终端中输入以下命令:

ls -l | grep "^d" | cut -d' ' -f9| head -n1 | xargs echo dirname {} | tail -n1 | xargs echo -n "$HOME"/{}/{} | tr '
' '/' | sed 's/\//\\//g' | xargs echo -n "/{}" | tr '
' ' ' | sed 's/\\\//\//g' | xargs echo "{}" | head -n1 | xargs echo -n "{}" | tr '
' ' ' | sed 's/\//\\\//g' | xargs echo -n "/{}" | tr '
' ' ' | sed 's/\\//\//g' | xargs echo "{}" | head -n1 | xargs echo -n "{}" | tr '
' ' ' | sed 's/\//\\\//g' | xargs echo -n "/{}" | tr '
' ' ' | sed 's/\\\//\//g' | xargs echo "{}" | head -n1 | xargs echo -n "{}" | tr '
' ' ' | sed 's/\//\\\//g' | xargs echo -n "/{}" | tr '
' ' ' | sed 's/\\\//\//g' | xargs echo "{}" | head -n1 | xargs echo -n "{}" | tr '
' ' ' | sed 's/\//\\//g' | xargs echo -n "/{}" | tr '
' ' ' | sed 's/\\\//\//g' | xargs echo "{}" | head -n1 | xargs echo -n "{}" | tr '
' ' ' | sed 's/\//\\\//g' | xargs echo -n "/{}" | tr '
' ' ' | sed 's/\\//\//g' | xargs echo "{}" | head -n1 | xargs echo -n "{}" | tr '
' ' ' | sed 's/\//\\\//g' | xargs echo -n "/{}" | tr '
' ' ' | sed 's/\\\//\//g' | xargs echo "{}" | head -n1 | xargs echo -n "{}" | tr '
' ' ' | sed 's/\//\\\//g' | xargs echo -n "/{}" | tr '
' ' ' | sed 's/\\\//\//g' | xargs echo "{}" | head -n1 | xargs echo -n "{}" | tr '
' ' ' | sed 's/\//\\//g' | xargs echo -n "/{}" | tr '
' ' ' | sed 's/\\\//\//g' | xargs echo "{}" | head -n1 || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true || true --version=1.0.0-alpha.1-28-gfdc323e-dirty --help=true --usage=true --man=true --info=true --config=true --prefix=true --no-site-packages=true --data-dir=true --include=true --lib-dir=true --sysconfdir=true --sharedstatedir=true --localstatedir=true --libexecdir=true --runtimedir=true --logfile=true --version-file=true --srcdir=true --bugreport=true --prune=true --recurse=true --force=false --target=all --exclude=.git --exclude=CVS --exclude=.DS_Store --exclude=*.pyc --exclude=*.pyo --exclude=*.old --exclude=*.bak --exclude=*.zip --exclude=*.egg-info --exclude=build_aux --exclude=dist build_ext build install clean develop doctest egg_info install_clib install_headers install_lib install_scripts install_data install_data_dir install_platlib install_headers install_lib install_scripts install_data install_data_dir install_platlib install tests check build test upload upload_docs source release tarball wheel zip_safe depends help build_ext build install clean develop doctest egg_info install_clib install_headers install_lib install_scripts install_data install_data_dir install_platlib install_headers install_lib install_scripts install_data install_data_dir install_platlib install tests check build test upload upload_docs source release tarball wheel zip_safe depends help build_ext build install clean develop doctest egg_info install_clib install_headers install_lib install_scripts install_data install_data_dir install_platlib install_headers install_lib install_scripts install_data install_data_dir install_platlib install tests check build test upload upload_docs source release tarball wheel zip_safe depends help build_ext build install clean develop doctest egg_info install_

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/200268.html

(0)
K-seoK-seoSEO优化员
上一篇 2024年1月5日 09:31
下一篇 2024年1月5日 09:34

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入