-
为什么我的Linux ls命令不能用了?
2016-06-21 11:56:18 -
Linux ls命令
2018-01-03 10:39:50RAC能够提供实例级的冗余,但是不能保护数据,只能使用多个实例来共享一个存储,实际上还是一个数据库。Dg就是数据的冗余,数据层面的一种保护。 现在流行的其他技术如Stream(流复制),Golden Gate(OGG),其实...ls 命令,list 的缩写,是最常见的目录操作命令,其主要功能是显示当前目录下的内容。此命令的基本格式为:
[root@localhost ~]# ls [选项] 目录名称
表 1 列出了 ls 命令常用的选项以及各自的功能。
表 1 ls 命令常用选项及功能 选项 功能 -a 显示全部的文件,包括隐藏文件(开头为 . 的文件)也一起罗列出来,这是最常用的选项之一。 -A 显示全部的文件,连同隐藏文件,但不包括 . 与 .. 这两个目录。 -d 仅列出目录本身,而不是列出目录内的文件数据。 -f ls 默认会以文件名排序,使用 -f 选项会直接列出结果,而不进行排序。 -F 在文件或目录名后加上文件类型的指示符号,例如,* 代表可运行文件,/ 代表目录,= 代表 socket 文件,| 代表 FIFO 文件。 -h 以人们易读的方式显示文件或目录大小,如 1KB、234MB、2GB 等。 -i 显示 inode 节点信息。 -l 使用长格式列出文件和目录信息。 -n 以 UID 和 GID 分别代替文件用户名和群组名显示出来。 -r 将排序结果反向输出,比如,若原本文件名由小到大,反向则为由大到小。 -R 连同子目录内容一起列出来,等於将该目录下的所有文件都显示出来。 -S 以文件容量大小排序,而不是以文件名排序。 -t 以时间排序,而不是以文件名排序。 --color=never
--color=always
--color=autonever 表示不依据文件特性给予颜色显示。
always 表示显示颜色,ls 默认采用这种方式。
auto 表示让系统自行依据配置来判断是否给予颜色。--full-time 以完整时间模式 (包含年、月、日、时、分)输出 --time={atime,ctime} 输出 access 时间或改变权限属性时间(ctime),而不是内容变更时间。 注意,当 ls 命令不使用任何选项时,默认只会显示非隐藏文件的名称,并以文件名进行排序,同时会根据文件的具体类型给文件名配色(蓝色显示目录,白色显示一般文件)。除此之外,如果想使用 ls 命令显示更多内容,就需要使用表 1 相应的选项。
【例 1】
[root@www ~]# ls -al ~ total 156 drwxr-x--- 4 root root 4096 Sep 24 00:07 . drwxr-xr-x 23 root root 4096 Sep 22 12:09 .. -rw------- 1 root root 1474 Sep 4 18:27 anaconda-ks.cfg -rw------- 1 root root 955 Sep 24 00:08 .bash_history -rw-r--r-- 1 root root 24 Jan 6 2007 .bash_logout -rw-r--r-- 1 root root 191 Jan 6 2007 .bash_profile -rw-r--r-- 1 root root 176 Jan 6 2007 .bashrc drwx------ 3 root root 4096 Sep 5 10:37 .gconf -rw-r--r-- 1 root root 42304 Sep 4 18:26 install.log -rw-r--r-- 1 root root 5661 Sep 4 18:25 install.log.syslog
注意,Linux 系统中,隐藏文件不是为了把文件藏起来不让其他用户找到,而是为了告诉用户这些文件都是重要的系统文件,如非必要,不要乱动!所以,不论是 Linux 还是 Windows 都可以非常简单地査看隐藏文件,只是在 Windows 中绝大多数的病毒和木马都会把自己变成隐藏文件,给用户带来了错觉,以为隐藏文件是为了不让用户发现。
不仅如此,这里的 ls 命令还使用了 -l 选项,因此才显示出了文件的详细信息,此选项显示的这 7 列的含义分别是:- 第一列:规定了不同的用户对文件所拥有的权限
- 第二列:引用计数,文件的引用计数代表该文件的硬链接个数,而目录的引用计数代表该目录有多少个一级子目录。
- 第三列:所有者,也就是这个文件属于哪个用户。默认所有者是文件的建立用户。
- 第四列:所属组,默认所属组是文件建立用户的有效组,一般情况下就是建立用户的所在组。
- 第五列:大小,默认单位是字节。
- 第六列:文件修改时间,文件状态修改时间或文件数据修改时间都会更改这个时间,注意这个时间不是文件的创建时间。
- 第七列:文件名或目录名。
【例 2】如果我们想查看某个目录的详细信息,例如: [root@localhost ~]# ls -l /root/ 总用量44 -rw-------.1 root root 1207 1 月 14 18:18 anaconda-ks.cfg -rw-r-r--.1 root root 24772 1 月 14 18:17 install.log -rw-r-r--.1 root root 7690 1 月 14 18:17 install.log.syslog 这个命令会显示目录下的内容,而不会显示这个目录本身的详细信息。如果想显示目录本身的信息,就必须加入 "-d" 选项。 [root@localhost ~]# ls -ld /root/ dr-xr-x---.2 root root 4096 1 月 20 12:30 /root/
【例 3】"ls-l" 显示的文件大小是字节,但是我们更加习惯的是千字节用 KB 显示,兆字节用 MB 显示,而 "-h" 选项就是按照人们习惯的单位显示文件大小的,例如: [root@localhost ~]# ls -lh 总用量44K -rw-------.1 root root 1.2K 1 月 14 18:18 anaconda-ks.cfg -rw-r-r--.1 root root 25K 1 月 14 18:17 install.log -rw-r-r--.1 root root 7.6K 1 月 14 18:17 install.log.syslog
-
unix/linux ls命令的实现
2017-03-19 11:25:28Understanding Unix/Linux Programming(Unix/Linux编程实践教程) ...(3)能不能自己编写一个? (实验环境:gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) ) ls命令学习 (1)ls命令能做什么Understanding Unix/Linux Programming(Unix/Linux编程实践教程)
学习模式:
(1)它能做什么?
(2)它是如何实现的?
(3)能不能自己编写一个?
(实验环境:gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) )
ls命令学习(1)ls命令能做什么
查看联机帮助:ls
可知ls默认列出当前目录的文件夹跟文件。按字典序排序。
ls还可以跟其他参数:ls -l
cong@ubantu:/$ ls -l total 100 drwxr-xr-x 2 root root 4096 Mar 8 03:48 bin drwxr-xr-x 3 root root 4096 Mar 10 02:39 boot drwxrwxr-x 2 root root 4096 Aug 22 2016 cdrom drwxr-xr-x 19 root root 4320 Mar 10 22:09 dev drwxr-xr-x 132 root root 12288 Mar 10 22:09 etc drwxr-xr-x 3 root root 4096 Aug 22 2016 home lrwxrwxrwx 1 root root 32 Mar 8 03:56 initrd.img -> boot/initrd.img-4.4.0-66-generic lrwxrwxrwx 1 root root 32 Oct 24 23:51 initrd.img.old -> boot/initrd.img-4.4.0-45-generic drwxr-xr-x 22 root root 4096 Aug 22 2016 lib drwxr-xr-x 2 root root 4096 Jul 19 2016 lib64 drwx------ 2 root root 16384 Aug 22 2016 lost+found drwxr-xr-x 4 root root 4096 Aug 22 2016 media drwxr-xr-x 3 root root 4096 Oct 15 04:39 mnt drwxr-xr-x 2 root root 4096 Jul 19 2016 opt dr-xr-xr-x 223 root root 0 Mar 10 22:08 proc drwx------ 4 root root 4096 Oct 14 09:10 root drwxr-xr-x 27 root root 860 Mar 11 00:45 run drwxr-xr-x 2 root root 12288 Mar 8 03:48 sbin drwxr-xr-x 2 root root 4096 Jun 29 2016 snap drwxr-xr-x 2 root root 4096 Jul 19 2016 srv dr-xr-xr-x 13 root root 0 Mar 10 22:08 sys drwxrwxrwt 12 root root 4096 Mar 11 00:45 tmp drwxr-xr-x 11 root root 4096 Jul 19 2016 usr drwxr-xr-x 14 root root 4096 Jul 19 2016 var lrwxrwxrwx 1 root root 29 Mar 8 03:56 vmlinuz -> boot/vmlinuz-4.4.0-66-generic lrwxrwxrwx 1 root root 29 Oct 24 23:51 vmlinuz.old -> boot/vmlinuz-4.4.0-45-generic
(2)ls是如何实现的?
目录是一种特殊的文件,目录是文件的列表。每个目录都至少包含两个特殊的项。“.”(当前目录),“..”(上一级目录)。
如何读取目录的信息?如何获取文件的信息?
man 3 readdir
struct dirent结构
man 3 opendir
有了上面的信息,我们很容易编写自己的ls命令。
(3)如何编写ls
#include<stdio.h> #include<dirent.h> #include<string.h> void show_ls(char filename[]); int main(int argc,char* argv[]) { if(argc == 1) show_ls("."); while(--argc) { printf("%s: \n",*++argv); show_ls(*argv); printf("\n"); } return 0; } void show_ls(char filename[]) { DIR* dir_ptr; //the directory struct dirent* direntp; //each entry if((dir_ptr = opendir(filename)) == NULL) fprintf(stderr,"ls1: cannot open%s \n",filename); while((direntp = readdir(dir_ptr)) != NULL) printf("%-10s",direntp->d_name); closedir(dir_ptr); }
我们已经把当前目录文件的名称打印出来了。(默认打印“.”,"..",这完成可以通过程序过滤,字典序可以通过qsort)接下来实现ls -l命令
ls -l命令需要文件的当前信息。
使用stat获得文件信息。
stat结构体
现在我们基本已经获得我们需要所有信息了,但是有些信息需要转换一下。才能跟shell标准下ls -l一致。
1.将模式串转为字符串。
在stat.h文件下可以看到很多关于模式判断的宏定义。这只是一部分, 可自行查阅。
相关实现。#define S_IRUSR __S_IREAD /* Read by owner. */ #define S_IWUSR __S_IWRITE /* Write by owner. */ #define S_IXUSR __S_IEXEC /* Execute by owner. */ /* Read, write, and execute by owner. */ #define S_IRWXU (__S_IREAD|__S_IWRITE|__S_IEXEC) #ifdef __USE_MISC # define S_IREAD S_IRUSR # define S_IWRITE S_IWUSR # define S_IEXEC S_IXUSR #endif #define S_IRGRP (S_IRUSR >> 3) /* Read by group. */ #define S_IWGRP (S_IWUSR >> 3) /* Write by group. */ #define S_IXGRP (S_IXUSR >> 3) /* Execute by group. */ /* Read, write, and execute by group. */ #define S_IRWXG (S_IRWXU >> 3) #define S_IROTH (S_IRGRP >> 3) /* Read by others. */
2.用用户ID/组ID转成字符串。
在struct stat中,文件所有者都是以ID形式存在的,然而ls要求输出用户名和组名。
(1)用户名: man 3 getpwuid
(2)组名: man 3 getgrgid
有了上面的信息之后,编码实现ls - l
运行结果:已经非常接近shell标准ls -l 命令#include<stdio.h> #include<sys/stat.h> #include<dirent.h> #include<sys/types.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<pwd.h> #include<grp.h> #include<time.h> void do_ls(char []); void do_stat(char* ); void show_file_info(char* ,struct stat*); void mode_to_letters(int ,char[]); char* uid_to_name(uid_t); char* gid_to_name(gid_t); int main(int argc,char*argv[]) { if(argc == 1) do_ls("."); else { while(--argc) printf("%s:\n",*++argv); do_ls(*argv); } return 0; } void do_ls(char dirname[]) { /*list files in directory called dirname*/ DIR* dir_ptr; struct dirent * direntp; /*each entry*/ if((dir_ptr = opendir(dirname)) == NULL) perror("opendir fails"); while((direntp = readdir(dir_ptr)) !=NULL) do_stat(direntp->d_name); closedir(dir_ptr); } void do_stat(char* filename) { struct stat info; if((stat(filename,&info)) == -1) perror(filename); else show_file_info(filename,&info); } void show_file_info(char* filename,struct stat * info_p) { /*display the info about filename . the info is stored in struct at * info_p*/ char modestr[11]; mode_to_letters(info_p->st_mode,modestr); printf("%s",modestr); printf("%4d ",(int)info_p->st_nlink); printf("%-8s ",uid_to_name(info_p->st_uid)); printf("%-8s ",gid_to_name(info_p->st_gid)); printf("%8ld ",(long)info_p->st_size); printf("%.12s ",ctime(&info_p->st_mtime)+4); printf("%s\n",filename); } void mode_to_letters(int mode,char str[]) { strcpy(str,"----------"); if(S_ISDIR(mode)) str[0] = 'd'; //"directory ?" if(S_ISCHR(mode)) str[0] = 'c'; //"char decices"? if(S_ISBLK(mode)) str[0] = 'b'; //block device? //3 bits for user if(mode&S_IRUSR) str[1] = 'r'; if(mode&S_IWUSR) str[2] = 'w'; if(mode&S_IXUSR) str[3] = 'x'; //3 bits for group if(mode&S_IRGRP) str[4] = 'r'; if(mode&S_IWGRP) str[5] = 'w'; if(mode&S_IXGRP) str[6] = 'x'; //3 bits for other if(mode&S_IROTH) str[7] = 'r'; if(mode&S_IWOTH) str[8] = 'w'; if(mode&S_IXOTH) str[9] = 'x'; } char* uid_to_name(uid_t uid) { struct passwd* pw_ptr; static char numstr[10]; if((pw_ptr =getpwuid(uid)) == NULL) { sprintf(numstr,"%d",uid); printf("world"); return numstr; } return pw_ptr->pw_name; } char* gid_to_name(gid_t gid) { /*returns pointer to group number gid, used getgrgid*/ struct group* grp_ptr; static char numstr[10]; if((grp_ptr =getgrgid(gid)) == NULL) { printf("hello wofjl"); sprintf(numstr,"%d",gid); return numstr; } else return grp_ptr->gr_name; }
/** sol03.18.c ** ------------------------------------------------------------ sol03.18.c is a version of ls2.c that supports the -R option. This program also supports the suid, sgid, and sticky bit handling from solution 3.12. ** ------------------------------------------------------------ * * ls2.c - supports -R option, * handles suid, sgid, sticky bits, also fixes bug, * * purpose list contents of directory or directories * action if no args, use . else list files in args * note uses lstat and pwd.h and grp.h * NOTE uses lstat to avoid chasing symlink loops * * build: cc sol03.18.c -o sol03.18 */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> void do_ls(char[],int); void dostat(char *,char *); void show_file_info( char *, struct stat *); void mode_to_letters( int , char [] ); char *uid_to_name( uid_t ); char *gid_to_name( gid_t ); main(int ac, char *av[]) { int R_flag = 0; int anyfiles = 0; while ( --ac ){ if ( strcmp("-R", *++av) == 0 ) R_flag = 1; else { do_ls( *av , R_flag ); anyfiles = 1; } } if ( !anyfiles ) do_ls(".", R_flag); } void do_ls( char dirname[] , int subdirs ) /* * list files in directory called dirname * lists subdirs if `subdirs' is true * First: list contents of dirname, then * recurse to call each of the subdirs */ { DIR *dir_ptr; /* the directory */ struct dirent *direntp; /* each entry */ char *fullpath; /* * get the problem case out of the way up front */ if ( ( dir_ptr = opendir( dirname ) ) == NULL ){ fprintf(stderr,"ls2: cannot open %s\n", dirname); return; } /* * ok, we can read the directory, make two passes through it */ printf("%s:\n", dirname); fullpath = (char *)malloc(strlen(dirname) + 1 + MAXNAMLEN + 1); /* * pass 1: list the contents */ while ( ( direntp = readdir( dir_ptr ) ) != NULL ){ sprintf(fullpath,"%s/%s",dirname,direntp->d_name); dostat( fullpath, direntp->d_name ); } /* * pass 2: rewind, traverse, and for each subdir, recurse */ /*The rewinddir() function resets the position of the directory stream dirp to the beginning of the directory*/ if ( subdirs ){ rewinddir(dir_ptr); while ( ( direntp = readdir( dir_ptr ) ) != NULL ){ /* skip . and .. */ if ( strcmp(".",direntp->d_name) == 0 || strcmp("..",direntp->d_name) == 0 ) continue; sprintf(fullpath,"%s/%s",dirname,direntp->d_name); if ( isadir(fullpath) ){ putchar('\n'); do_ls( fullpath, subdirs ); } } } /* all done with this dir, close up and go */ closedir(dir_ptr); free(fullpath); } void dostat( char *fullpath, char *filename ) { struct stat info; if ( lstat(fullpath, &info) == -1 ) /* cannot stat */ perror(filename); /* say why */ else /* else show info */ show_file_info(filename, &info); } void show_file_info( char *filename, struct stat *info_p ) /* * display the info about 'filename'. The info is stored in struct at *info_p */ { char *uid_to_name(), *ctime(), *gid_to_name(), *filemode(); void mode_to_letters(); char modestr[11]; mode_to_letters( info_p->st_mode, modestr ); printf( "%s" , modestr ); printf( "%4d " , (int) info_p->st_nlink); printf( "%-8s " , uid_to_name(info_p->st_uid) ); printf( "%-8s " , gid_to_name(info_p->st_gid) ); printf( "%8ld " , (long)info_p->st_size); printf( "%.12s ", 4+ctime(&info_p->st_mtime)); printf( "%s\n" , filename ); } /* * utility functions */ /* * This function takes a mode value and a char array * and puts into the char array the file type and the * nine letters that correspond to the bits in mode. * NOTE: It now codes suid, sgid, and sticky bits * see manual for details. */ void mode_to_letters( int mode, char str[] ) { strcpy( str, "----------" ); /* default=no perms */ if ( S_ISDIR(mode) ) str[0] = 'd'; /* directory? */ if ( S_ISCHR(mode) ) str[0] = 'c'; /* char devices */ if ( S_ISBLK(mode) ) str[0] = 'b'; /* block device */ if ( mode & S_IRUSR ) str[1] = 'r'; /* 3 bits for user */ if ( mode & S_IWUSR ) str[2] = 'w'; if ( (mode & S_ISUID) && (mode & S_IXUSR) ) str[3] = 's'; else if ( (mode & S_ISUID) && !(mode & S_IXUSR) ) str[3] = 'S'; else if ( mode & S_IXUSR ) str[3] = 'x'; if ( mode & S_IRGRP ) str[4] = 'r'; /* 3 bits for group */ if ( mode & S_IWGRP ) str[5] = 'w'; if ( (mode & S_ISGID) && (mode & S_IXGRP) ) str[6] = 's'; else if ( (mode & S_ISGID) && !(mode & S_IXGRP) ) str[6] = 'S'; else if ( mode & S_IXGRP ) str[6] = 'x'; if ( mode & S_IROTH ) str[7] = 'r'; /* 3 bits for other */ if ( mode & S_IWOTH ) str[8] = 'w'; if ( (mode & S_ISVTX) && (mode & S_IXOTH) ) str[9] = 't'; else if ( (mode & S_ISVTX) && !(mode & S_IXOTH) ) str[9] = 'T'; else if ( mode & S_IXOTH ) str[9] = 'x'; } #include <pwd.h> char *uid_to_name( uid_t uid ) /* * returns pointer to username associated with uid, uses getpw() */ { struct passwd *getpwuid(), *pw_ptr; static char numstr[10]; if ( ( pw_ptr = getpwuid( uid ) ) == NULL ){ sprintf(numstr,"%d", uid); return numstr; } else return pw_ptr->pw_name ; } #include <grp.h> char *gid_to_name( gid_t gid ) /* * returns pointer to group number gid. used getgrgid(3) */ { struct group *getgrgid(), *grp_ptr; static char numstr[10]; if ( ( grp_ptr = getgrgid(gid) ) == NULL ){ sprintf(numstr,"%d", gid); return numstr; } else return grp_ptr->gr_name; } /* * boolean: tells if arg names a directory */ isadir(char *str) { struct stat info; return ( lstat(str,&info) != -1 && S_ISDIR(info.st_mode) ); }
-
linux 命令 尖括号_精通 Linux 的 ls 命令
2020-12-29 13:05:32Linux 的 ls 命令拥有数量惊人的选项,可以提供有关文件的重要信息。-- Seth Kenlon(作者)ls 命令可以列出一个 POSIX 系统上的文件。这是一个简单的命令,但它经常被低估,不是它能做什么(因为它确实只做了一件事...Linux 的 ls 命令拥有数量惊人的选项,可以提供有关文件的重要信息。
-- Seth Kenlon(作者)ls
命令可以列出一个 POSIX 系统上的文件。这是一个简单的命令,但它经常被低估,不是它能做什么(因为它确实只做了一件事),而是你该如何优化对它的使用。要知道在最重要的 10 个终端命令中,这个简单的
ls
命令可以排进前三,因为ls
不会只是列出文件,它还会告诉你有关它们的重要信息。它会告诉你诸如拥有文件或目录的人、每个文件修改的时间、甚至是什么类型的文件。它的附带功能能让你了解你在哪里、附近有些什么,以及你可以用它们做什么。如果你对
ls
的体验仅限于你的发行版在.bashrc
中的别名,那么你可能错失了它。GNU 还是 BSD?
在了解
ls
的隐藏能力之前,你必须确定你正在运行哪个ls
命令。有两个最流行的版本:包含在 GNU coreutils 包中的 GNU 版本,以及 BSD 版本。如果你正在运行 Linux,那么你很可能已经安装了 GNU 版本的ls
(LCTT 译注:几乎可以完全确定)。如果你正在运行 BSD 或 MacOS,那么你有的是 BSD 版本。本文会介绍它们的不同之处。你可以使用
--version
选项找出你计算机上的版本:$ ls --version
如果它返回有关 GNU coreutils 的信息,那么你拥有的是 GNU 版本。如果它返回一个错误,你可能正在运行的是 BSD 版本(运行
man ls | head
以确定)。你还应该调查你的发行版可能具有哪些预设选项。终端命令的自定义通常放在
$HOME/.bashrc
或$HOME/.bash_aliases
或$HOME/.profile
中,它们是通过将ls
别名化为更复杂的ls
命令来完成的。例如:alias ls='ls --color'
发行版提供的预设非常有用,但它们确实很难分辨出哪些是
ls
本身的特性,哪些是它的附加选项提供的。你要是想要运行ls
命令本身而不是它的别名,你可以用反斜杠“转义”命令:$ ls
分类
单独运行
ls
会以适合你终端的列数列出文件:$ ls ~/example bunko jdk-10.0.2 chapterize otf2ttf.ff despacer overtar.sh estimate.sh pandoc-2.7.1 fop-2.3 safe_yaml games tt
这是有用的信息,但所有这些文件看起来基本相同,没有方便的图标来快速表示出哪个是目录、文本文件或图像等等。
使用
-F
(或 GNU 上的长选项--classify
)以在每个条目之后显示标识文件类型的指示符:$ ls ~/example bunko jdk-10.0.2/ chapterize* otf2ttf.ff* despacer* overtar.sh* estimate.sh pandoc@ fop-2.3/ pandoc-2.7.1/ games/ tt*
使用此选项,终端中列出的项目使用简写符号来按文件类型分类:
- 斜杠(
/
)表示目录(或“文件夹”)。 - 星号(
*
)表示可执行文件。这包括二进制文件(编译代码)以及脚本(具有可执行权限的文本文件)。 - 符号(
@
)表示符号链接(或“别名”)。 - 等号(
=
)表示套接字。 - 在 BSD 上,百分号(
%
)表示 涂改(whiteout)(某些文件系统上的文件删除方法)。 - 在 GNU 上,尖括号(
>
)表示 门(door)(Illumos 和 Solaris上的进程间通信)。 - 竖线(
|
)表示 FIFO 管道。 这个选项的一个更简单的版本是-p
,它只区分文件和目录。
(LCTT 译注:在支持彩色的终端上,使用
--color
选项可以以不同的颜色来区分文件类型,但要注意如果将输出导入到管道中,则颜色消失。)长列表
从
ls
获取“长列表”的做法是如此常见,以至于许多发行版将ll
别名为ls -l
。长列表提供了许多重要的文件属性,例如权限、拥有每个文件的用户、文件所属的组、文件大小(以字节为单位)以及文件上次更改的日期:$ ls -l -rwxrwx---. 1 seth users 455 Mar 2 2017 estimate.sh -rwxrwxr-x. 1 seth users 662 Apr 29 22:27 factorial -rwxrwx---. 1 seth users 20697793 Jun 29 2018 fop-2.3-bin.tar.gz -rwxrwxr-x. 1 seth users 6210 May 22 10:22 geteltorito -rwxrwx---. 1 seth users 177 Nov 12 2018 html4mutt.sh [...]
如果你不想以字节为单位,请添加
-h
标志(或 GNU 中的--human
)以将文件大小转换为更加人性化的表示方法:$ ls --human -rwxrwx---. 1 seth users 455 Mar 2 2017 estimate.sh -rwxrwxr-x. 1 seth seth 662 Apr 29 22:27 factorial -rwxrwx---. 1 seth users 20M Jun 29 2018 fop-2.3-bin.tar.gz -rwxrwxr-x. 1 seth seth 6.1K May 22 10:22 geteltorito -rwxrwx---. 1 seth users 177 Nov 12 2018 html4mutt.sh
要看到更少的信息,你可以带有
-o
选项只显示所有者的列,或带有-g
选项只显示所属组的列:$ ls -o -rwxrwx---. 1 seth 455 Mar 2 2017 estimate.sh -rwxrwxr-x. 1 seth 662 Apr 29 22:27 factorial -rwxrwx---. 1 seth 20M Jun 29 2018 fop-2.3-bin.tar.gz -rwxrwxr-x. 1 seth 6.1K May 22 10:22 geteltorito -rwxrwx---. 1 seth 177 Nov 12 2018 html4mutt.sh
也可以将两个选项组合使用以显示两者。
时间和日期格式
ls
的长列表格式通常如下所示:-rwxrwx---. 1 seth users 455 Mar 2 2017 estimate.sh -rwxrwxr-x. 1 seth users 662 Apr 29 22:27 factorial -rwxrwx---. 1 seth users 20697793 Jun 29 2018 fop-2.3-bin.tar.gz -rwxrwxr-x. 1 seth users 6210 May 22 10:22 geteltorito -rwxrwx---. 1 seth users 177 Nov 12 2018 html4mutt.sh
月份的名字不便于排序,无论是通过计算还是识别(取决于你的大脑是否倾向于喜欢字符串或整数)。你可以使用
--time-style
选项和格式名称更改时间戳的格式。可用格式为:full-iso
:ISO 完整格式(1970-01-01 21:12:00)long-iso
:ISO 长格式(1970-01-01 21:12)iso
:iso 格式(01-01 21:12)locale
:本地化格式(使用你的区域设置)posix-STYLE
:POSIX 风格(用区域设置定义替换STYLE
)
你还可以使用
date
命令的正式表示法创建自定义样式。按时间排序
通常,
ls
命令按字母顺序排序。你可以使用-t
选项根据文件的最近更改的时间(最新的文件最先列出)进行排序。例如:
$ touch foo bar baz $ ls bar baz foo $ touch foo $ ls -t foo bar baz
列出方式
ls
的标准输出平衡了可读性和空间效率,但有时你需要按照特定方式排列的文件列表。要以逗号分隔文件列表,请使用
-m
:ls -m ~/example bar, baz, foo
要强制每行一个文件,请使用
-1
选项(这是数字 1,而不是小写的 L):$ ls -1 ~/bin/ bar baz foo
要按文件扩展名而不是文件名对条目进行排序,请使用
-X
(这是大写 X):$ ls bar.xfc baz.txt foo.asc $ ls -X foo.asc baz.txt bar.xfc
隐藏杂项
在某些
ls
列表中有一些你可能不关心的条目。例如,元字符.
和..
分别代表“本目录”和“父目录”。如果你熟悉在终端中如何切换目录,你可能已经知道每个目录都将自己称为.
,并将其父目录称为..
,因此当你使用-a
选项显示隐藏文件时并不需要它经常提醒你。要显示几乎所有隐藏文件(
.
和..
除外),请使用-A
选项:$ ls -a . .. .android .atom .bash_aliases [...] $ ls -A .android .atom .bash_aliases [...]
有许多优秀的 Unix 工具有保存备份文件的传统,它们会在保存文件的名称后附加一些特殊字符作为备份文件。例如,在 Vim 中,备份会以在文件名后附加
~
字符的文件名保存。这些类型的备份文件已经多次使我免于愚蠢的错误,但是经过多年享受它们提供的安全感后,我觉得不需要用视觉证据来证明它们存在。我相信 Linux 应用程序可以生成备份文件(如果它们声称这样做的话),我很乐意相信它们存在 —— 而不用必须看到它们。
要隐藏备份文件,请使用
-B
或--ignore-backups
隐藏常用备份格式(此选项在 BSD 的ls
中不可用):$ ls bar.xfc baz.txt foo.asc~ foo.asc $ ls -B bar.xfc baz.txt foo.asc
当然,备份文件仍然存在;它只是过滤掉了,你不必看到它。
除非另有配置,GNU Emacs 在文件名的开头和结尾添加哈希字符(
#
)来保存备份文件(#file#
)。其他应用程序可能使用不同的样式。使用什么模式并不重要,因为你可以使用--hide
选项创建自己的排除项:$ ls bar.xfc baz.txt #foo.asc# foo.asc $ ls --hide="#*#" bar.xfc baz.txt foo.asc
递归地列出目录
除非你在指定目录上运行
ls
,否则子目录的内容不会与ls
命令一起列出:$ ls -F example/ quux* xyz.txt $ ls -R quux xyz.txt ./example: bar.xfc baz.txt #foo.asc# foo.asc
使用别名使其永久化
ls
命令可能是 shell 会话期间最常使用的命令。这是你的眼睛和耳朵,为你提供上下文信息和确认命令的结果。虽然有很多选项很有用,但ls
之美的一部分就是简洁:两个字符和回车键,你就知道你到底在哪里以及附近有什么。如果你不得不停下思考(更不用说输入)几个不同的选项,它会变得不那么方便,所以通常情况下,即使最有用的选项也不会用了。解决方案是为你的
ls
命令添加别名,以便在使用它时,你可以获得最关心的信息。要在 Bash shell 中为命令创建别名,请在主目录中创建名为
.bash_aliases
的文件(必须在开头包含.
)。 在此文件中,列出要创建的别名,然后是要为其创建别名的命令。例如:alias ls='ls -A -F -B --human --color'
这一行导致你的 Bash shell 将
ls
命令解释为ls -A -F -B --human --color
。你不必仅限于重新定义现有命令,还可以创建自己的别名:
alias ll='ls -l' alias la='ls -A' alias lh='ls -h'
要使别名起作用,shell 必须知道
.bash_aliases
配置文件存在。在编辑器中打开.bashrc
文件(如果它不存在则创建它),并包含以下代码块:if [ -e $HOME/.bash_aliases ]; then source $HOME/.bash_aliases fi
每次加载
.bashrc
(这是一个新的 Bash shell 启动的时候),Bash 会将.bash_aliases
加载到你的环境中。你可以关闭并重新启动 Bash 会话,或者直接强制它执行此操作:$ source ~/.bashrc
如果你忘了你是否有别名命令,
which
命令可以告诉你:$ which ls alias ls='ls -A -F -B --human --color' /usr/bin/ls
如果你将
ls
命令别名为带有选项的ls
命令,则可以通过将反斜杠前缀到ls
前来覆盖你的别名。例如,在示例别名中,使用-B
选项隐藏备份文件,这意味着无法使用ls
命令显示备份文件。 可以覆盖该别名以查看备份文件:$ ls bar baz foo $ ls bar baz baz~ foo
做一件事,把它做好
ls
命令有很多选项,其中许多是特定用途的或高度依赖于你所使用的终端。在 GNU 系统上查看info ls
,或在 GNU 或 BSD 系统上查看man ls
以了解更多选项。你可能会觉得奇怪的是,一个以每个工具“做一件事,把它做好”的前提而闻名的系统会让其最常见的命令背负 50 个选项。但是
ls
只做一件事:它列出文件,而这 50 个选项允许你控制接收列表的方式,ls
的这项工作做得非常、非常好。 - 斜杠(
-
Linux ls 命令小记和软硬链接的区别
2018-01-17 00:32:41linux ls -al 列出来的文件,会发现大小都是4096,其实ls -al是不能列出文件夹的大小的,所以默认都是4096 如果想要列出文件夹中的文件大小,那么你需要进入到文件夹中 使用命令 ls -lh 小记B: 软连接相当于... -
at命令不生效 linux_帮你精通Linux:简约却不简单的ls命令
2020-12-22 02:08:24它就如车载或者手机中的导航地图,彻底解放了我们的大脑,片刻不能离身。否则我们下楼取个快递,都得绕18道弯才能勉强找回家。我们在前面的“Linux手册”课程中,大量应用ls这个地图查看器。1.只列出当前的目录文件... -
linux备份文件_精通 Linux 的 ls 命令 | Linux 中国
2020-11-10 04:29:26Linux 的 ls 命令拥有数量惊人的选项,可以提供有关文件的重要信息。...要知道在最重要的 10 个终端命令中,这个简单的ls命令可以排进前三,因为ls不会只是列出文件,它还会告诉你有关它们的重要信... -
asc码转换 linux_精通 Linux 的 ls 命令
2021-01-09 16:32:16Linux 的 ls 命令拥有数量惊人的选项,可以提供有关文件的重要信息。-- Seth Kenlon(作者)ls 命令可以列出一个 POSIX 系统上的文件。这是一个简单的命令,但它经常被低估,不是它能做什么(因为它确实只做了一件事),... -
asc怎么用 linux zip_精通 Linux 的 ls 命令 | Linux 中国
2020-12-25 15:55:10Linux 的 ls 命令拥有数量惊人的选项,可以提供有关文件的重要信息。...要知道在最重要的 10 个终端命令中,这个简单的ls命令可以排进前三,因为ls不会只是列出文件,它还会告诉你有关它们的重要信... -
linux所有命令不能用显示-bash: ls: command not found
2018-09-11 09:45:00所有的命令都显示找不到了,原因是修改了/etc/profile造成的 解决方法 1.修正属性文件中的错误 /usr/bin/vi /etc/profile 2.生效属性文件 source /etc/profile 3.重新开一个连接窗口就可以了 转载于:... -
linux系统下,ll,ls命令突然不能用了
2019-07-17 14:19:28输入一下两条命令即可解决: export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin vim /etc/profile -
解决Linux系统的ls/vi等基本命令不能用的问题
2021-01-18 22:50:16解决Linux系统的ls/vi等基本命令不能用的问题 这两天在配置项目到我的Linux服务器,输入命令的时候发现了一个bug,我的Linux系统的ls、vi等基本命令都不能用了,能理解是/etc/profile文件配置错误,我就以为是我不... -
my_ls实现Linux shell命令ls
2019-07-26 01:00:20自己的命令和系统的ls命令重定向输入到文件中去大小相差不能太多,1.25.倍以内; 实现过程 一些重要的函数 lstat()/stat()/fstat()函数解析; opendir()函数; readdir()函数; -R与-Rl的递归调用 链接文件的解析 ... -
linux ls 中文乱码_5个相见恨晚的Linux命令
2020-12-30 21:00:21作者 | 李火清转载自【CU技术社区】编者按:说到Linux命令相信大家都不陌生,就连前端现在也要经常在 terminal 敲一些 node,gulp等命令,本文详细介绍了五个相见恨晚,能极大提高工作效率的命令。tldr(命令手册)... -
Linux 基本命令不能用的解决方法
2018-03-24 14:53:05问题描述最近某次,新建一个ssh客户端后,发现好多命令都不能用了比如:ls, vi, cat等,提示:-bash: XX: No...切换root权限准备修改profile文件后,发现vi命令不能用,最后百度到了解决方法。解决方法在ssh终端中执... -
linux-获取帮助-ls命令-date命令-man命令
2019-06-23 00:22:53ls命令 短选项,长选项,明确 -d参数 如果查看命令后面直接跟一个目录名称 表示要查询这个目录下面有哪些内容 如果带上了选项d 代表要查询 目录的信息 -h 表示人类可读的 默认的查询结果看不懂这个... -
Linux命令之ls
2015-05-12 23:02:28掐指一算,本屌丝进入Linux界已然两年了,虽然如今在shell上敲命令敲得风生水起,...然而,人不能一直停留在那个水平,Linux命令行博大精深,我要将这种享受升级,就不可避免的回炉重造——将以前常用的命令重新认识 -
Linux下,改过/etc/profile文件导致ls vi等命令不能使用解决方法
2020-11-06 15:40:04配置jdk环境变量时,修改了/etc/profile执行后,发现ls等命令不可用了,也不能重新编辑vi了,执行如下命令即可再次重新编辑/etc/profile,修改正确即可正常使用 export PATH=/usr/local/sbin:/usr/local/bin:/sbin... -
linux ftp命令找不到_Linux命令还能这么玩?你一定想不到!
2020-11-28 19:04:34五组有趣的Linux命令,即使你是小白,get后也能装大神。以下内容使用阿里云的Ecs服务器以及Xshell实现。01蒸汽火车小伙子,想学开火车吗?在安装小火车后,我们只需要输入sl,屏幕上便会出现一个从右向左开的小火车... -
Linux常用命令之ls、cd、pwd、mkdir命令讲解
2020-07-31 11:25:10ls命令令是Linux最常用的命令之一,也是一条非常古老的命令。在开始学习Linux命令之前,还是想给大家一条建议: 很多同学可能刚开始学习Linux,然后就去买一些教材去学习,教材上面有可能收集了Linux的上千条甚至几... -
linux ls没有反应_Linux入门②“命令格式”
2021-01-08 17:53:25Linux终端敲命令是一件“爽”的事情。 对于我这样的新手来说,这个过程是我一直...本次我就带领大家入门Linux命令,让大家能切身感受一下按住 Ctrl+alt+T 打开终端。我们看到这样一个界面,显示:fei@ubuntu:~$这...
-
牛牛量化策略交易
-
MySQL 事务和锁
-
清除Maven缓存.bat
-
data1_final.anns
-
OnlineClass-源码
-
转行做IT-第15章 Collection、泛型、Iterator
-
MySQL 管理利器 mysql-utilities
-
MySQL 性能优化(思路拓展及实操)
-
【爱码农】C#制作MDI文本编辑器
-
基于Qt的LibVLC开发教程
-
朱老师鸿蒙系列课程第1期-3.鸿蒙系统Harmonyos源码配置和管理
-
mysql 子查查不能注意事项 报错1093
-
打印出100到200内所有素数
-
如何修复“您的系统已耗尽应用程序内存”错误法
-
使用vue搭建微信H5公众号项目
-
nohup命令详解
-
SDK和API
-
解决No architectures to compile for...
-
HTML5游戏_基于DOM平台跳跃小游戏开发_5.生成关卡地图
-
Ag-聚乙烯吡咯烷酮纳米复合材料对染料金属增强荧光的研究