V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
pOH8
V2EX  ›  Linux

路径中「~」的意思是什么?

  •  
  •   pOH8 · 2016-05-04 17:25:51 +08:00 · 8701 次点击
    这是一个创建于 2906 天前的主题,其中的信息可能已经有所发展或是发生改变。
    $ cd /Users/zhanglei/工作库 /Demo/LuceneDemo/~/Downloads/LucenseDemo

    上面这条命令中「~」是什么意思?

    $ rm -rf /Users/zhanglei/工作库/Demo/LuceneDemo/~/
    shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

    $ cd ..
    cd: Could not set PWD variable
    shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
    第 1 条附言  ·  2016-05-04 18:58:40 +08:00
    问题解决,就是一个「~」字符
    $ cd /Users/zhanglei/工作库 /Demo/LuceneDemo
    $ ll -a
    total 20K
    drwxr-xr-x 9 zhanglei staff 306 5 4 18:51 ./
    drwxr-xr-x 17 zhanglei staff 578 5 4 11:50 ../
    -rw-r--r-- 1 zhanglei staff 8.1K 5 4 18:49 .DS_Store
    drwxr-xr-x 12 zhanglei staff 408 5 4 09:40 .idea/
    -rw-r--r-- 1 zhanglei staff 2.4K 9 17 2015 LuceneDemo.iml
    -rw-r--r-- 1 zhanglei staff 1.8K 9 17 2015 pom.xml
    drwxr-xr-x 4 zhanglei staff 136 9 17 2015 src/
    drwxr-xr-x 5 zhanglei staff 170 5 4 18:49 target/
    drwxr-xr-x 3 zhanglei staff 102 5 4 18:51 '~'/

    $ cd /Users/zhanglei/工作库 /Demo/LuceneDemo/'~'
    $ ll -a
    total 0
    drwxr-xr-x 3 zhanglei staff 102 5 4 18:51 ./
    drwxr-xr-x 9 zhanglei staff 306 5 4 18:51 ../
    drwxr-xr-x 3 zhanglei staff 102 5 4 18:51 Downloads/

    $ cd /Users/zhanglei/工作库 /Demo/LuceneDemo/~
    $ pwd
    /Users/zhanglei/工作库 /Demo/LuceneDemo/~
    第 2 条附言  ·  2016-05-04 19:01:29 +08:00
    路径是这么来的
    mvn dependency:copy-dependencies -DoutputDirectory=~/Downloads/LucenseDemo
    13 条回复    2016-05-05 11:50:46 +08:00
    julyclyde
        1
    julyclyde  
       2016-05-04 17:31:37 +08:00
    在某些 shell 里,波浪号代表$HOME 这个意思
    n6DD1A640
        2
    n6DD1A640  
       2016-05-04 17:34:25 +08:00
    指你当前用户的 $HOME 目录。如果真的需要删除一个带 ~ 字符的文件 /目录,需要反斜杠 \
    misaka19000
        3
    misaka19000  
       2016-05-04 17:36:23 +08:00 via Android
    比如你用 root 用户登录,辣么 cd ~就意味着打开 /home/root ,其他用户也是同样的道理
    secfun
        4
    secfun  
       2016-05-04 17:36:34 +08:00
    是 当前用户目录的别名
    debiann
        5
    debiann  
       2016-05-04 17:39:44 +08:00 via iPhone
    什么情况下会有楼主这种命令?还是说这只是楼主随便写的?
    Havee
        6
    Havee  
       2016-05-04 17:43:32 +08:00
    root 的 home 为 /root
    bp0
        7
    bp0  
       2016-05-04 18:02:15 +08:00
    怎么会有这么奇怪的路径,一般~都是放在最前面的,比如 cd ~/test/ 表示进入当前用户的 home 目录下的 test 。

    一般用户的 home 目录为 /home/username/

    root 的 home 目录为 /root
    irenicus
        8
    irenicus  
       2016-05-04 18:35:15 +08:00 via Android
    你家!(严肃脸)
    SoloCompany
        9
    SoloCompany  
       2016-05-04 21:24:37 +08:00   ❤️ 1
    ~ 是由 shell 解释的
    只有在特定语境下,才会被自动替换成 ${HOME}

    特定语境是指
    - 必须是作为任意参数(包括 $0 )的开始字符
    - 后面要么没有内容要么紧跟着一个 /

    示例
    echo $HOME
    /Users/foo

    echo a-$HOME-b
    a-/Users/foo-b

    echo ~
    /Users/foo

    echo ~/
    /Users/foo

    echo ~/b
    /Users/foo/b

    echo a-~
    a-~

    echo ~-b
    ~-b
    raysonx
        10
    raysonx  
       2016-05-04 21:27:56 +08:00   ❤️ 1
    @SoloCompany ~username 也是可以的
    SoloCompany
        11
    SoloCompany  
       2016-05-04 21:29:48 +08:00
    @raysonx 对!我忘了这个了,后置字符的也要满足同样的规则
    satoru
        12
    satoru  
       2016-05-05 08:01:27 +08:00
    我昨天刚删了这个目录
    hei1000
        13
    hei1000  
       2016-05-05 11:50:46 +08:00 via Android
    有可能是你不小心新建的一个名字叫~的文件夹,删除的时候要小心了,之前我不小心新建过名字叫-"的文件夹
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5722 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:15 · PVG 10:15 · LAX 19:15 · JFK 22:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.