相关疑难解决方法(0)

如何将当前工作目录设置为脚本目录?

我正在写一个bash脚本.我需要当前的工作目录始终是脚本所在的目录.

默认行为是脚本中的当前工作目录是我运行它的shell的目录,但我不想要这种行为.

bash scripting path

500
推荐指数
7
解决办法
35万
查看次数

如何在Mac上获取GNU的readlink -f的行为?

在Linux上,该readlink实用程序接受一个-f跟随其他链接的选项.这似乎不适用于Mac和可能基于BSD的系统.相当于什么?

这是一些调试信息:

$ which readlink; readlink -f
/usr/bin/readlink
readlink: illegal option -f
usage: readlink [-n] [file ...]
Run Code Online (Sandbox Code Playgroud)

macos freebsd sh

341
推荐指数
13
解决办法
11万
查看次数

Bash:检索给定相对的绝对路径

是否有命令在给定相对路径的情况下检索绝对路径?

例如,我希望$ line包含dir中每个文件的绝对路径 ./etc/

find ./ -type f | while read line; do
   echo $line
done
Run Code Online (Sandbox Code Playgroud)

bash shell path absolute

135
推荐指数
10
解决办法
16万
查看次数

OS X的Bash脚本绝对路径

我试图获取OS X上当前运行脚本的绝对路径.

我看到很多回复readlink -f $0.但是,由于OS X readlink与BSD相同,它只是不起作用(它适用于GNU的版本).

有没有开箱即用的解决方案?

macos bash path

85
推荐指数
10
解决办法
6万
查看次数

如何检查Bash中两条路径是否相等?

在Bash中检查两条路径是否相等的最佳方法是什么?例如,给定目录结构

~/
  Desktop/
    Downloads/ (symlink to ~/Downloads)
  Downloads/
    photo.png
Run Code Online (Sandbox Code Playgroud)

并假设当前目录是主目录,以下所有内容都是等效的:

./                    and ~
~/Desktop             and /home/you/Desktop
./Downloads           and ~/Desktop/Downloads
./Downloads/photo.png and ~/Downloads/photo.png
Run Code Online (Sandbox Code Playgroud)

有没有本地Bash方式来做到这一点?

unix linux bash shell sh

18
推荐指数
2
解决办法
7650
查看次数

bash相当于Python的os.path.normpath?

什么是bash相当于os.path.normpath?具体来说,我有兴趣删除./执行时给出的前导find.

matt@stanley:~/src/libtelnet-0.20/test$ find
.
./Makefile
./Makefile.in
./Makefile.am
...

python bash path normalization find

5
推荐指数
1
解决办法
2069
查看次数

在bash脚本中获取祖父目录 - 为其路径中的目录重命名文件

我有以下脚本,当我得到一堆需要重命名为包含它们的目录名的文件时,我通常会使用它.

现在的问题是我需要将文件重命名为两个级别的目录.如何获取祖父目录以使其工作?

有了以下我得到的错误就像这个例子:"mv:无法移动./48711/zoom/zoom.jpg./48711/zoom/./48711/zoom.jpg:没有这样的文件或目录".这是在CentOS 5.6上运行的.

我希望最终文件命名为:48711.jpg

#!/bin/bash

function dirnametofilename() {
  for f in $*; do
    bn=$(basename "$f")
    ext="${bn##*.}"
    filepath=$(dirname "$f")
    dirname=$(basename "$filepath")
    mv "$f" "$filepath/$dirname.$ext"
  done
}

export -f dirnametofilename

find . -name "*.jpg" -exec bash -c 'dirnametofilename "{}"'  \;

find .
Run Code Online (Sandbox Code Playgroud)

linux bash centos path batch-rename

3
推荐指数
2
解决办法
7789
查看次数

标签 统计

bash ×6

path ×5

linux ×2

macos ×2

sh ×2

shell ×2

absolute ×1

batch-rename ×1

centos ×1

find ×1

freebsd ×1

normalization ×1

python ×1

scripting ×1

unix ×1