"ls -alh" 在不牺牲颜色和链接的情况下打印文件夹大小
"ls -alh" to print folder sizes without sacrificing the color and links
这是我找到的答案,显示 ls
大小 folder/file
(du -sh ./*; ls -lh --color=no) | awk '{ if( == "total") {X = 1}
else if (!X) {SIZES[] = } else { sub( "[ ]*", sprintf("%-7s ",
SIZES["./" ]), [=11=]); print [=11=]} }'
却失去了ls
颜色的凉爽。如果你输入 --color=always
那么大小就会丢失。
如何在上面的输出上有颜色?颜色必须与正常 ls -alh
输出相同
最后我写了一个脚本来打印输出和该脚本的别名。这就像变魔术一样。
alias le=~/bin/ls-ext.sh
确保 ~/bin 在你的路径中
这是 ls-ext.sh 脚本
#!/bin/bash
PURPLE=$(tput setaf 5) GREEN=$(tput setaf 2) YELLOW=$(tput setaf 3) NC=$(tput sgr0)
let ln=0
pwd=;
if [ "${pwd::1}" = "." ] && [ ${#pwd} -eq 1 ]
then
pwd=""
elif [ "${pwd: -1}" != "/" ] && [ ${#pwd} -gt 1 ]
then
pwd="$pwd/"
fi
while read line
do
ln=$((ln+1)); [ $ln -eq 1 ] && continue
read -r -a array <<< "$line"
duout=$(du -sh "$pwd${array[8]}" 2>/dev/null|cut -f1)
[ $? -eq 0 ] && array[4]="$duout"
if [ "${array[4]: -1}" = "G" ]
then
array[4]="$PURPLE${array[4]}$NC"
elif [ "${array[4]: -1}" = "M" ]
then
array[4]="$YELLOW${array[4]}$NC"
else
array[4]="$GREEN${array[4]}$NC"
fi
lsout="$(ls --color=always -d $pwd${array[8]})"
if [ "${array[0]:0:1}" = "l" ]
then
read -r -a lsarray <<< "$(ls --color=always -lsd $pwd${array[8]})"
lsout=$(echo "${lsarray[@]:9:${#lsarray[@]}}"|sed 's/[[:space:]]->[[:space:]]/->/')
fi
#echo -e "${array[@]::8} $lsout"
printf "%-11s%3s %-10s%-10s%15s%4s%3s %-4s %s\n" ${array[0]} ${array[1]} ${array[2]} ${array[3]} ${array[4]} ${array[5]} ${array[6]} ${array[7]} $lsout
done < <(ls -al --color=no $pwd )
echo "Total: $((ln-1))"
输出看起来像这样
这是我找到的答案,显示 ls
大小 folder/file
(du -sh ./*; ls -lh --color=no) | awk '{ if( == "total") {X = 1}
else if (!X) {SIZES[] = } else { sub( "[ ]*", sprintf("%-7s ",
SIZES["./" ]), [=11=]); print [=11=]} }'
却失去了ls
颜色的凉爽。如果你输入 --color=always
那么大小就会丢失。
如何在上面的输出上有颜色?颜色必须与正常 ls -alh
输出相同
最后我写了一个脚本来打印输出和该脚本的别名。这就像变魔术一样。
alias le=~/bin/ls-ext.sh
确保 ~/bin 在你的路径中 这是 ls-ext.sh 脚本
#!/bin/bash
PURPLE=$(tput setaf 5) GREEN=$(tput setaf 2) YELLOW=$(tput setaf 3) NC=$(tput sgr0)
let ln=0
pwd=;
if [ "${pwd::1}" = "." ] && [ ${#pwd} -eq 1 ]
then
pwd=""
elif [ "${pwd: -1}" != "/" ] && [ ${#pwd} -gt 1 ]
then
pwd="$pwd/"
fi
while read line
do
ln=$((ln+1)); [ $ln -eq 1 ] && continue
read -r -a array <<< "$line"
duout=$(du -sh "$pwd${array[8]}" 2>/dev/null|cut -f1)
[ $? -eq 0 ] && array[4]="$duout"
if [ "${array[4]: -1}" = "G" ]
then
array[4]="$PURPLE${array[4]}$NC"
elif [ "${array[4]: -1}" = "M" ]
then
array[4]="$YELLOW${array[4]}$NC"
else
array[4]="$GREEN${array[4]}$NC"
fi
lsout="$(ls --color=always -d $pwd${array[8]})"
if [ "${array[0]:0:1}" = "l" ]
then
read -r -a lsarray <<< "$(ls --color=always -lsd $pwd${array[8]})"
lsout=$(echo "${lsarray[@]:9:${#lsarray[@]}}"|sed 's/[[:space:]]->[[:space:]]/->/')
fi
#echo -e "${array[@]::8} $lsout"
printf "%-11s%3s %-10s%-10s%15s%4s%3s %-4s %s\n" ${array[0]} ${array[1]} ${array[2]} ${array[3]} ${array[4]} ${array[5]} ${array[6]} ${array[7]} $lsout
done < <(ls -al --color=no $pwd )
echo "Total: $((ln-1))"
输出看起来像这样