lz: linux ls 变种 只显示大小和名称(包括目录)

本次输入法使用: 手心输入法 for Mac 1.0版

测试环境为:Ubuntu 14.14.2 LTS updates

测试时间为:2015年5月28日,感觉死亡将至的夜晚,独自一人坐在一个角落,戴着耳机盖着帽子,穿着衬衫的那一天,外面下着雨,不小不大,不紧不慢,却分外的让人不自然,写到这里放的歌是:苏运莹的《野子》

主要解决了特殊用法(为制定参数,. ,..,*的使用)

-d 检测是否为directory时,如果参数为空,也会被错误的认为目录,这是bash的一个诟病吗?应该吧,不过结合-z选项就可以解决。但是问题的关键是我以为-d是会识别的,其实我高估了-d,[see line 34-36]

 1 function lz() {
 2     function du_dir() {
 3         echo -e "33[01;33m$133[00m" # $((RANDOM%6+1))
 4         (cd $1; ls -1 2>/dev/null | sed -e 's/^/"/' -e 's/$/"/'| xargs du -h -d 0 2>/dev/null)
 5     }
 6     function du_file() {
 7         (ls -lha $1 | awk '{print $5,$9}')
 8     }
 9     function getpath() { # support . and .. as the special path
10             cd $1
11             echo `pwd`
12     }
13     function usage(){
14         echo -e " 33[01;37musage:33[00m le [dir]/[file] ..."
15         echo " if no arguement is given, 'lz' will print the current working dir"
16         echo " .(current dir), ..(parent dir), *(metachar) are supported"
17         echo -e "
 -V/-h for this help page"
18         echo -e " 
--------------------------------------------------
 lz v0.1 by Ray Lee - March 30, 2015"
19         echo -e " updates:
  1.clone 'le' as 'lz', new feature added
  2.empty parameter doesn't work"
20         echo -e " ^
 lz v0.2 by Ray Lee - may 28, 2015"
21         echo -e " updates: 
  1.make the empty arg work, printing the current directory
  2.-V and -h works
  3.add comments for code block
  4.modify the help page"
22     }
23     default='.'
24     args=($@)
25     if [[ ${#args[@]} -gt 1 ]]; then
26         ## the * meta means all the things in the current directory, length of it is gt 1 when current dir contains
27         ## more than 1 thing, 1 if there is only one thing, le 1 when empty
28         ## 
29         for each in ${args[@]}; do
30             [ -d $each ] && du_dir `getpath $each` || du_file `getpath $each`
31         done
32     elif [  ! -z $1 ]; then
33         ## The oddness of the -d option for file test is that it recognises the empty as a directory
34         ## So the -z should be used to make -d solid for empty args.
35         ## 
36         if [ -d $1 ]; then # support . and .. as the special path
37             du_dir `getpath $1`
38         elif [[ $1 == '-V' ]]; then
39             usage
40         elif [[ $1 == '-h' ]]; then
41             usage
42         fi
43     else
44         du_dir `getpath "."`
45     fi
46 }

Results:

 

Reference:

1. http://www.cnblogs.com/raybiolee/p/4240363.html

天和地是灰色的,砖和瓦也是灰色的。临街的墙几经风化,几经修补,刷过黑灰、白灰,涂过红漆,书写过不同内容的标语,又终于被覆盖;风雨再把覆盖层胡乱地揭下来,形成一片斑驳的杂色,融汇于灰色的笼罩之中。路旁的树木苍黑,瓦楞中芳草青青。 远处,炊烟缭绕。迷蒙的曙色中,矗立着...
原文地址:https://www.cnblogs.com/raybiolee/p/linux_ls_command_variant_with_only_size_and_name_dir_included.html