zsh / bash - 如何将所有打开的终端选项卡的目录写入文件?

时间:2017-05-08 20:29:16

标签: bash zsh

我想将每个终端选项卡的工作目录写入单个文件。我想我已接近以下内容,但问题是同一(当前)目录路径被多次写入文件。

for x in $(ps | grep zsh)
do
  echo "$(pwd)" >> "tabs open $(date +%Y_%m_%d_%H_%M_%S).txt"
done

提前感谢我提出我的新问题。

1 个答案:

答案 0 :(得分:0)

这不是你需要的100%解决方案,但它是我能理解的最接近的。

# For each PID of the "-bash" processes...
for x in $(ps -ef | grep -v grep | grep -- "-bash" | awk '{print $2}'); do 
    # check if the file is readable (only user has access)
    if [[ -r /proc/$x/cwd ]]; then
        # print details including the working directory
        ls -ld /proc/$x/cwd
        # ... or just PID and it's working directory
        echo $x : $(ls -ld /proc/$x/cwd | awk '{print $11}')
    fi
done > "tabs_open_$(date +%Y_%m_%d_%H_%M_%S).txt"

“ - bash”是进程名称,因为它实际上位于终端内部。该脚本不会考虑用户在终端窗口内手动启动的“bash”进程。

相关问题