Linux file and directory manipulation ls command

It is no exaggeration to say that file operations are the most frequent operations. In Linux, you can use lscommands to list all the contents of the current directory. In this article, we will talk about lscommands first. The files mentioned in this article refer to files and directories.

 

ls command common options

  • -a: Display all subdirectories and files in the specified directory, including hidden files
  • -l: Display file details in list mode
  • -h: Use with -l to display file size more intuitively

-a

First of all, let's talk about the -a option, a is the abbreviation of all, and this option can display all files in the current directory, mainly to display hidden files.

Hidden files all start with . and are basically files that we do not need to operate on, such as creating a hidden file:

touch .123.txt

Use ls to view:

ls

This file is not present in the listed files at this time  .123.txt , we have to add  -a, namely:

ls -a

At this point, you see this file, and you may also see hidden files that are already there.

 

-l

After using the ls command, the results are displayed in rows and only names. After using the -l option, the file will be displayed in the form of a column with very detailed file attributes:

ls -l

result:

total 64
lrwxrwxrwx.  1 root root     7 Oct 15  2017 bin -> usr/bin
dr-xr-xr-x.  5 root root  4096 Dec 23 14:31 boot
drwxr-xr-x  20 root root  3040 Dec 23 14:30 dev
drwxr-xr-x. 80 root root  4096 Dec 23 14:30 etc
drwxr-xr-x.  2 root root  4096 Nov  5  2016 home
lrwxrwxrwx.  1 root root     7 Oct 15  2017 lib -> usr/lib
lrwxrwxrwx.  1 root root     9 Oct 15  2017 lib64 -> usr/lib64
drwx------.  2 root root 16384 Oct 15  2017 lost+found
drwxr-xr-x.  2 root root  4096 Nov  5  2016 media
drwxr-xr-x.  2 root root  4096 Nov  5  2016 mnt
drwxr-xr-x.  2 root root  4096 Nov  5  2016 opt
dr-xr-xr-x  73 root root     0 Dec 23 14:30 proc
dr-xr-x---.  9 root root  4096 Dec 24 13:33 root
drwxr-xr-x  22 root root   620 Dec 23 14:30 run
lrwxrwxrwx.  1 root root     8 Oct 15  2017 sbin -> usr/sbin
drwxr-xr-x.  2 root root  4096 Nov  5  2016 srv
dr-xr-xr-x  13 root root     0 Dec 23 22:30 sys
drwxrwxrwt.  8 root root  4096 Apr 19 03:57 tmp
drwxr-xr-x. 13 root root  4096 Oct 15  2017 usr
drwxr-xr-x. 19 root root  4096 Oct 15  2017 var
drwxr-xr-x   3 root root  4096 Dec 24 13:32 workspace

 

-h

This option should be used with -l, after adding it, the size information of the file will be more intuitive:

ls -h -l

result:

total 64K
lrwxrwxrwx.  1 root root    7 Oct 15  2017 bin -> usr/bin
dr-xr-xr-x.  5 root root 4.0K Dec 23 14:31 boot
drwxr-xr-x  20 root root 3.0K Dec 23 14:30 dev
drwxr-xr-x. 80 root root 4.0K Dec 23 14:30 etc
drwxr-xr-x.  2 root root 4.0K Nov  5  2016 home
lrwxrwxrwx.  1 root root    7 Oct 15  2017 lib -> usr/lib
lrwxrwxrwx.  1 root root    9 Oct 15  2017 lib64 -> usr/lib64
drwx------.  2 root root  16K Oct 15  2017 lost+found
drwxr-xr-x.  2 root root 4.0K Nov  5  2016 media
drwxr-xr-x.  2 root root 4.0K Nov  5  2016 mnt
drwxr-xr-x.  2 root root 4.0K Nov  5  2016 opt
dr-xr-xr-x  73 root root    0 Dec 23 14:30 proc
dr-xr-x---.  9 root root 4.0K Dec 24 13:33 root
drwxr-xr-x  22 root root  620 Dec 23 14:30 run
lrwxrwxrwx.  1 root root    8 Oct 15  2017 sbin -> usr/sbin
drwxr-xr-x.  2 root root 4.0K Nov  5  2016 srv
dr-xr-xr-x  13 root root    0 Dec 23 22:30 sys
drwxrwxrwt.  8 root root 4.0K Apr 19 03:57 tmp
drwxr-xr-x. 13 root root 4.0K Oct 15  2017 usr
drwxr-xr-x. 19 root root 4.0K Oct 15  2017 var
drwxr-xr-x   3 root root 4.0K Dec 24 13:32 workspace

 

Compare the results above.

Note: When multiple options are used together, they can be chained together, i.e.:

ls -h -l

can be written as:

ls -hl

The order can also be changed:

ls -lh

Finally, the  -a options can be combined:

ls -lha

 

Try it now~

Let’s talk about so much first, and it’s annoying to see too much. What we want is the effect of small steps and fast running~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324649159&siteId=291194637
Recommended