我正在写一个bash脚本.我需要当前的工作目录始终是脚本所在的目录.
默认行为是脚本中的当前工作目录是我运行它的shell的目录,但我不想要这种行为.
在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) 是否有命令在给定相对路径的情况下检索绝对路径?
例如,我希望$ line包含dir中每个文件的绝对路径 ./etc/
find ./ -type f | while read line; do
echo $line
done
Run Code Online (Sandbox Code Playgroud) 我试图获取OS X上当前运行脚本的绝对路径.
我看到很多回复readlink -f $0.但是,由于OS X readlink与BSD相同,它只是不起作用(它适用于GNU的版本).
有没有开箱即用的解决方案?
在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方式来做到这一点?
什么是bash相当于os.path.normpath?具体来说,我有兴趣删除./执行时给出的前导find.
matt@stanley:~/src/libtelnet-0.20/test$ find . ./Makefile ./Makefile.in ./Makefile.am ...
我有以下脚本,当我得到一堆需要重命名为包含它们的目录名的文件时,我通常会使用它.
现在的问题是我需要将文件重命名为两个级别的目录.如何获取祖父目录以使其工作?
有了以下我得到的错误就像这个例子:"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)