ls 指令可以说是Linux下最常用的指令之一。它有许多的选项,其中有许多是很有用的,你能否了解呢?下面列出了 ls 指令的绝大多数选项。
一运用举例:
1.列出具体文件 #ls -al //这个最常用,可是往往又不彻底符合要求,
2.列出一切子目录的文件:#ls -R //上个指令仅仅列出了本目录下的一切目录和文件,可是目录下的文件不会循环的列出。
3.若是文件过多一屏看不完,怎么办?并且,关于想找到许多文件中的某个文件的姓名。
3.1分屏显现:#ls -l|more //能在当时屏退出,很有用,但不能一页一页翻屏
#ls -l|less //能用pgup,pgdw 翻页,但不能在当时方位退出
3.2横向输出竟能够多的显现文件姓名:#ls -m
-a 列出目录下的一切文件,包含以 . 最初的隐含文件。//常用
-m 横向输出文件名,并以“,”作分格符。
-c 输出文件的 i 节点的修正时刻,并以此排序。
-R 列出一切子目录下的文件。
-l 列出文件的具体信息。
-s 在每个文件名后输出该文件的巨细。
-k 以 k 字节的方式表明文件的巨细。
-i 输出文件的 i 节点的索引信息。
2.只显现目录:#ls -l |grep ^d 只显现非目录文件:#ls-l |grep ^[^d]
3.对输出文件进行各种排序;
3.1 依照字符序(default): #ls -l
3.2 依照修正时刻排序: #ls -t //近来修正在最上面
3.3 逆排序 : #ls -r
3.4 按文件巨细排序 :#ls -S //大文件的在上
3.5 依照文件扩展名(最终一个.后的字符)排序: #ls -X
3.6 按数字排序 :#ls -lv
-t 以时刻排序。
-r 对目录反向排序
-f -U 对输出的文件不排序。
-X 以文件的扩展名(最终一个 . 后的字符)排序。
-S 以文件巨细排序。
-u 以文件前次被拜访的时刻排序。
-n 用数字的 UID,GID 替代称号。
-o 显现文件的除组信息外的具体信息。
-p -F 在每个文件名后附上一个字符以阐明该文件的类型,“*”表明可执行的一般
文件;“/”表明目录;“@”表明符号连接;“|”表明FIFOs;“=”表明套
接字(sockets)。
-q 用?替代不行输出的字符。
。
下面来看看busybox对ls命令的实现
流程比较简单
先看下ls_main
/*----------------------------------------------------------------------*/ int ls_main(int argc, char **argv) { struct dnode **dnd; struct dnode **dnf; struct dnode **dnp; struct dnode *dn; struct dnode *cur; long opt; int nfiles = 0; int dnfiles; int dndirs; int oi; int ac; int i; char **av; #ifdef CONFIG_FEATURE_AUTOWIDTH char *tabstops_str = NULL; char *terminal_width_str = NULL; #endif #ifdef CONFIG_FEATURE_LS_COLOR char *color_opt; #endif all_fmt = LIST_SHORT | (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_ORDER_FORWARD)); #ifdef CONFIG_FEATURE_AUTOWIDTH /* Obtain the terminal width. */ get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL); /* Go one less... */ terminal_width--; #endif #ifdef CONFIG_FEATURE_LS_COLOR bb_applet_long_options = ls_color_opt; #endif /* process options */ #ifdef CONFIG_FEATURE_AUTOWIDTH opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str #ifdef CONFIG_FEATURE_LS_COLOR , &color_opt #endif ); if (tabstops_str) { tabstops = atoi(tabstops_str); } if (terminal_width_str) { terminal_width = atoi(terminal_width_str); } #else opt = bb_getopt_ulflags(argc, argv, ls_options #ifdef CONFIG_FEATURE_LS_COLOR , &color_opt #endif ); #endif for (i = 0; opt_flags[i] != (1U<<31); i++) { if (opt & (1 << i)) { unsigned int flags = opt_flags[i]; if (flags & LIST_MASK_TRIGGER) { all_fmt &= ~LIST_MASK; } if (flags & STYLE_MASK_TRIGGER) { all_fmt &= ~STYLE_MASK; } if (ENABLE_FEATURE_LS_SORTFILES && (flags & SORT_MASK_TRIGGER)) { all_fmt &= ~SORT_MASK; } if (flags & DISP_MASK_TRIGGER) { all_fmt &= ~DISP_MASK; } if (flags & TIME_MASK) { all_fmt &= ~TIME_MASK; } if (flags & LIST_CONTEXT) { all_fmt |= STYLE_SINGLE; } #ifdef CONFIG_FEATURE_HUMAN_READABLE if (opt == 'l') { all_fmt &= ~LS_DISP_HR; } #endif all_fmt |= flags; } } #ifdef CONFIG_FEATURE_LS_COLOR { /* find color bit value - last position for short getopt */ #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT char *p; if ((p = getenv ("LS_COLORS")) != NULL && (*p == '\0' || (strcmp(p, "none") == 0))) { ; } else if (isatty(STDOUT_FILENO)) { show_color = 1; } #endif if((opt & (1 << i))) { /* next flag after short options */ if (color_opt == NULL || strcmp("always", color_opt) == 0) show_color = 1; else if (color_opt != NULL && strcmp("never", color_opt) == 0) show_color = 0; else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO)) show_color = 1; } } #endif /* sort out which command line options take precedence */ #ifdef CONFIG_FEATURE_LS_RECURSIVE if (all_fmt & DISP_NOLIST) all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */ #endif if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) { if (all_fmt & TIME_CHANGE) all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME; if (all_fmt & TIME_ACCESS) all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME; } if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */ all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC); #ifdef CONFIG_FEATURE_LS_USERNAME if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC)) all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */ #endif /* choose a display format */ if (!(all_fmt & STYLE_MASK)) all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE); /* * when there are no cmd line args we have to supply a default "." arg. * we will create a second argv array, "av" that will hold either * our created "." arg, or the real cmd line args. The av array * just holds the pointers- we don't move the date the pointers * point to. */ ac = argc - optind; /* how many cmd line args are left */ if (ac < 1) { static const char * const dotdir[] = { "." }; av = (char **) dotdir; ac = 1; } else { av = argv + optind; } /* now, everything is in the av array */ if (ac > 1) all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */ /* stuff the command line file names into an dnode array */ dn = NULL; for (oi = 0; oi < ac; oi++) { cur = my_stat(av[oi], av[oi]); if (!cur) continue; cur->allocated = 0; cur->next = dn; dn = cur; nfiles++; } /* now that we know how many files there are ** allocate memory for an array to hold dnode pointers */ dnp = dnalloc(nfiles); for (i = 0, cur = dn; i < nfiles; i++) { dnp[i] = cur; /* save pointer to node in array */ cur = cur->next; } if (all_fmt & DISP_NOLIST) {//仅显示指定目录信息,不显示子目录 if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnp, nfiles); if (nfiles > 0) showfiles(dnp, nfiles); } else { dnd = splitdnarray(dnp, nfiles, SPLIT_DIR); dnf = splitdnarray(dnp, nfiles, SPLIT_FILE); dndirs = countdirs(dnp, nfiles); dnfiles = nfiles - dndirs; if (dnfiles > 0) { if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnf, dnfiles); showfiles(dnf, dnfiles); if (ENABLE_FEATURE_CLEAN_UP) free(dnf); } if (dndirs > 0) { if (ENABLE_FEATURE_LS_SORTFILES) dnsort(dnd, dndirs); showdirs(dnd, dndirs, dnfiles == 0); if (ENABLE_FEATURE_CLEAN_UP) free(dnd); } } if (ENABLE_FEATURE_CLEAN_UP) dfree(dnp, nfiles); return (status); }
ls_main也是先对参数进行解析,然后看是否需要递归显示该目录下的所以字目录,我们这里看下只显示该目录下的文件,而不显示他们的字目录,调用showfiles
/*----------------------------------------------------------------------*/ static void showfiles(struct dnode **dn, int nfiles) { int i, ncols, nrows, row, nc; int column = 0; int nexttab = 0; int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */ if (dn == NULL || nfiles < 1) return; if (all_fmt & STYLE_ONE_RECORD_FLAG) {//格式的一些判断 ncols = 1; } else { /* find the longest file name- use that as the column width */ for (i = 0; i < nfiles; i++) { int len = strlen(dn[i]->name) + #ifdef CONFIG_SELINUX ((all_fmt & LIST_CONTEXT) ? 33 : 0) + #endif ((all_fmt & LIST_INO) ? 8 : 0) + ((all_fmt & LIST_BLOCKS) ? 5 : 0); if (column_width < len) column_width = len; } column_width += tabstops; ncols = (int) (terminal_width / column_width); } if (ncols > 1) { nrows = nfiles / ncols; if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */ } else { nrows = nfiles; ncols = 1; } for (row = 0; row < nrows; row++) { for (nc = 0; nc < ncols; nc++) { /* reach into the array based on the column and row */ i = (nc * nrows) + row; /* assume display by column */ if (all_fmt & DISP_ROWS) i = (row * ncols) + nc; /* display across row */ if (i < nfiles) { if (column > 0) { nexttab -= column; while (nexttab--) { putchar(' '); column++; } } nexttab = column + column_width; column += list_single(dn[i]);//显示一行信息 } } putchar('\n'); column = 0; } }
Showfiles先对显示中间的一些空格,TAB长度做一些判断,然后调用list_single显示一行信息。
/*----------------------------------------------------------------------*/ static int list_single(struct dnode *dn) { int i, column = 0; #ifdef CONFIG_FEATURE_LS_USERNAME char scratch[16]; #endif #ifdef CONFIG_FEATURE_LS_TIMESTAMPS char *filetime; time_t ttime, age; #endif #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR) struct stat info; char append; #endif if (dn->fullname == NULL) return (0); #ifdef CONFIG_FEATURE_LS_TIMESTAMPS ttime = dn->dstat.st_mtime; /* the default time */ if (all_fmt & TIME_ACCESS) ttime = dn->dstat.st_atime; if (all_fmt & TIME_CHANGE) ttime = dn->dstat.st_ctime; filetime = ctime(&ttime); #endif #ifdef CONFIG_FEATURE_LS_FILETYPES append = append_char(dn->dstat.st_mode); #endif for (i = 0; i <= 31; i++) { switch (all_fmt & (1 << i)) { case LIST_INO: column += printf("%7ld ", (long int) dn->dstat.st_ino); break; case LIST_BLOCKS: #if _FILE_OFFSET_BITS == 64 column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1); #else column += printf("%4ld ", dn->dstat.st_blocks >> 1); #endif break; case LIST_MODEBITS: column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode)); break; case LIST_NLINKS: column += printf("%4ld ", (long) dn->dstat.st_nlink); break; case LIST_ID_NAME://显示用户名 #ifdef CONFIG_FEATURE_LS_USERNAME bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch)); printf("%-8.8s ", scratch); bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch)); printf("%-8.8s", scratch); column += 17; break; #endif case LIST_ID_NUMERIC: column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid); break; case LIST_SIZE: case LIST_DEV: if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) { column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev), (int) minor(dn->dstat.st_rdev)); } else { #ifdef CONFIG_FEATURE_HUMAN_READABLE if (all_fmt & LS_DISP_HR) { column += printf("%9s ", make_human_readable_str(dn->dstat.st_size, 1, 0)); } else #endif { #if _FILE_OFFSET_BITS == 64 column += printf("%9lld ", (long long) dn->dstat.st_size); #else column += printf("%9ld ", dn->dstat.st_size); #endif } } break; #ifdef CONFIG_FEATURE_LS_TIMESTAMPS case LIST_FULLTIME: printf("%24.24s ", filetime); column += 25; break; case LIST_DATE_TIME: if ((all_fmt & LIST_FULLTIME) == 0) { age = time(NULL) - ttime; printf("%6.6s ", filetime + 4); if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) { /* hh:mm if less than 6 months old */ printf("%5.5s ", filetime + 11); } else { printf(" %4.4s ", filetime + 20); } column += 13; } break; #endif #ifdef CONFIG_SELINUX case LIST_CONTEXT: { char context[80]; int len = 0; if (dn->sid) { /* I assume sid initilized with NULL */ len = strlen(dn->sid)+1; safe_strncpy(context, dn->sid, len); freecon(dn->sid); }else { safe_strncpy(context, "unknown", 8); } printf("%-32s ", context); column += MAX(33, len); } break; #endif case LIST_FILENAME: #ifdef CONFIG_FEATURE_LS_COLOR errno = 0; if (show_color && !lstat(dn->fullname, &info)) { printf("\033[%d;%dm", bgcolor(info.st_mode), fgcolor(info.st_mode)); } #endif column += printf("%s", dn->name); #ifdef CONFIG_FEATURE_LS_COLOR if (show_color) { printf("\033[0m"); } #endif break; case LIST_SYMLINK: if (S_ISLNK(dn->dstat.st_mode)) { char *lpath = xreadlink(dn->fullname); if (lpath) { printf(" -> "); #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR) if (!stat(dn->fullname, &info)) { append = append_char(info.st_mode); } #endif #ifdef CONFIG_FEATURE_LS_COLOR if (show_color) { errno = 0; printf("\033[%d;%dm", bgcolor(info.st_mode), fgcolor(info.st_mode)); } #endif column += printf("%s", lpath) + 4; #ifdef CONFIG_FEATURE_LS_COLOR if (show_color) { printf("\033[0m"); } #endif free(lpath); } } break; #ifdef CONFIG_FEATURE_LS_FILETYPES case LIST_FILETYPE: if (append != '\0') { printf("%1c", append); column++; } break; #endif } } return column; }
list_single根据all_fmt值(即ls的参数),显示相关的信息,这里主要看用户名是如何显示的。
这里linux和android的实现是不一样的,因为Android没有/etc/passwd、也就没有办法保存uid和他对应的name,而且在Android上面我们不需要使用adduser去为每个应用新建一个用户的,我们看一下Android里面的用户名是怎么来的。
这是获取用户名的流程,那getgrgid_r是在那实现的呢,没错,是bonic
在\bionic\libc\bionic\Stubs.cpp中
最终是通过print_app_name_from_appid_userid来获取用户名的
static void print_app_name_from_appid_userid(const uid_t appid, const uid_t userid, char* buffer, const int bufferlen) { __libc_android_log_print(ANDROID_LOG_ERROR, "leaves app_id_from_name", "leave appid = %d", appid); if (appid >= AID_ISOLATED_START) { snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); } else if (userid == 0 && appid >= AID_SHARED_GID_START) { snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START); } else if (appid < AID_APP) {//小于应用id的起始值 for (size_t n = 0; n < android_id_count; n++) { if (android_ids[n].aid == appid) { snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); return; } } } else { snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP);//应用程序的uid name } __libc_android_log_print(ANDROID_LOG_ERROR, "leaves app_id_from_name", "leave buffer = %s", buffer); }
从代码中可以看到,应用程序最终的name 都是u +uid + _ + a + appid –AID_APP
如下图:
Uid都是0
Linux下面的实现也是类似的,其getpwuid_r是在glic中实现的,这里就没去研究。