【问题标题】:zsh / bash - How to write directories of all open terminal tabs to file?zsh / bash - 如何将所有打开的终端选项卡的目录写入文件?
【发布时间】:2017-05-10 04:12:06
【问题描述】:

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

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

提前感谢您接受我的新问题。

【问题讨论】:

  • 每个终端标签是什么意思?也很确定你正在尝试的是不可能的。进程看不到其他进程的环境变量。您可能会得到最好的结果是检查/proc/$pid/environ 的进程,但我很确定shell 在启动后不会更新它,所以它不会包含PWD。​​span>
  • 好吧。 . . .
  • /proc/$pid/cwd 是指向给定进程当前工作目录的符号链接。当然,这假设您有一个 proc 文件系统。
  • @123 每次提示 eh... 提示时,都可以更改 bash 配置文件以执行命令。因此,$PWD 可以被跟踪...(关于 zsh,我不知道。)
  • @chepner 哦,是的,忘记了。

标签: bash zsh


【解决方案1】:

这不是您需要的 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”进程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-26
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2018-06-28
    • 2021-10-05
    • 2015-11-23
    相关资源
    最近更新 更多