Learn Linux (10) ls commands every day

Reference material:
[1]Learn Linux (8) Soft and Hard Links Every Day
[2]Twenty practical examples of Linux ls commands

The instructions we learn today are the most common of our instructions, ls. The instructions refer to the man manual.

Sketch

ls (LiSt directory contents) lists directory contents.

format

ls [option]...[file]...

The format given in man's manual is usually in the form above. Everything under Linux is a file, so the directory is also a file. [1]

Explanation and default behavior

This instruction lists file information. There are two defaults:

  • The default is the current directory
  • The default is alphabetical, if no - sort option or - cftuvSUX is used

Option meaning

Options are divided into long options and short options. Long options are usually a word.

option Meaning
-a all Don't ignore. Files at the beginning (that is, hidden files)
-A almost-all All documents except. and..
author Print each document author in conjunction with - l
-b escape For non-printable characters, use C-style escape characters instead of printing
block-size=SIZE Measuring catalogue size [2]
-B ignore-backup Don't list files that end with ~
-c - lt is used in conjunction with the latest modification, sorted by the latest modification time; - l is used in conjunction with the modified filename sorting
-C List files by column (default behavior)
–color Display color (default display)
-d,directory List the file itself, ps anyway, I see it all.
-D,dired File mode generated for Emac
-f Unordered, enabled - aV, and turned off color
-F,classify

Refer specifically to man manual. There are many options that feel like they will never be used.

Common examples [2]

No parameters

ls

The results are as follows:

wodeMBP:~ wo$ ls
AnacondaProjects
Applications
CLionProjects
Desktop
Documents
Downloads
Library
Movies
Music
Pictures
Public

It was found that ubuntu is displayed in rows, and mac directly lists the usual columns!

Long List Model

ls -l

The results are as follows:

wodeMBP:~ wo$ ls -l
total 8
drwxr-xr-x   2 wo  staff    64 Apr 20 10:54 AnacondaProjects
drwx------@  6 wo  staff   192 May 21 21:17 Applications
drwxr-xr-x   3 wo  staff    96 Jul  5 18:27 CLionProjects
drwx------+ 11 wo  staff   352 Jul  5 23:10 Desktop
drwx------+ 18 wo  staff   576 Jul  4 10:43 Documents
drwx------+ 60 wo  staff  1920 Jul  6 09:39 Downloads
drwx------@ 78 wo  staff  2496 Jun 23 17:11 Library
drwx------+  7 wo  staff   224 May  3 13:59 Movies
drwx------+  7 wo  staff   224 Apr 19 15:29 Music
drwx------+  8 wo  staff   256 Jun 23 14:03 Pictures
drwxr-xr-x+  4 wo  staff   128 Apr 17 18:38 Public
drwxrwxrwx  16 wo  staff   512 Apr 18 08:41 Qt
drwxr-xr-x  14 wo  staff   448 Apr 20 10:15 anaconda

Make the size appropriate for people to read the file size

ls -lh
total xxx
drwxr-xr-x   4 wo  staff   128B Apr 18 20:29 Cplusplus
drwx------@ 29 wo  staff   928B May  4 14:26 GCC_4_7_0
drwx------@ 28 wo  staff   896B May  4 14:33 GCC_pre_C11
drwxr-xr-x   3 wo  staff    96B May  1 09:27 MATLAB
drwxr-xr-x   5 wo  staff   160B May 30 09:06 Robot

-rw-r--r--@  1 wo  staff    21K Jul  2 11:06 cat_result.png
-rw-r--r--@  1 wo  staff    54K Jul  4 10:40 cp_test.png
-rw-r--r--@  1 wo  staff    58K Jul  4 10:43 cp_test2.png

Sort by file size

ls -lhS
total xxx
-rw-r--r--@  1 wo  staff    21K Jul  2 11:06 cat_result.png
drwx------@ 29 wo  staff   928B May  4 14:26 GCC_4_7_0
drwx------@ 28 wo  staff   896B May  4 14:33 GCC_pre_C11
-rw-r--r--@  1 wo  staff   215B Jul  2 11:01 1530500452559.textClipping
drwxr-xr-x   5 wo  staff   160B May 30 09:06 Robot
drwxr-xr-x   4 wo  staff   128B Apr 18 20:29 Cplusplus
drwxr-xr-x   3 wo  staff    96B May  1 09:27 MATLAB
drwxr-xr-x   3 wo  staff    96B Jun 23 15:30 link_here

Measure file size

ls -l-block-size=M

This is available in Ubuntu, but not Mac! !

view hidden files

ls -a

List only directories

ls -d */

Print inode number

ls -li

Add / slash tag directory

ls -p

List subdirectories recursively

ls -r

Screening extensions

ls -lX
lssort=extension

Modification time filtering

 ls -lt

Keywords: Linux Ubuntu Mac MATLAB

Added by forcom on Wed, 15 May 2019 00:50:39 +0300