linux显示父目录,linux – 仅列出文件的公共父目录

此脚本读取行并在每次迭代中存储公共前缀:

# read a line into the variable "prefix",split at slashes

IFS=/ read -a prefix

# while there are more lines,one after another read them into "next",# also split at slashes

while IFS=/ read -a next; do

new_prefix=()

# for all indexes in prefix

for ((i=0; i < "${#prefix[@]}"; ++i)); do

# if the word in the new line matches the old one

if [[ "${prefix[i]}" == "${next[i]}" ]]; then

# then append to the new prefix

new_prefix+=("${prefix[i]}")

else

# otherwise break out of the loop

break

fi

done

prefix=("${new_prefix[@]}")

done

# join an array

function join {

# copied from: https://stackoverflow.com/a/17841619/416224

local IFS="$1"

shift

echo "$*"

}

# join the common prefix array using slashes

join / "${prefix[@]}"

例:

$./x.sh <

/home/nicool/Desktop1/file1.txt

/home/nicool/Desktop2/dir1/file1.txt

/home/nicool/Desktop3/dir1/dir2/file1.txt

eof

/home/nicool

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值