In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian to share with you how to use the ls command Linux, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
The ls command, or list directory contents, is the Linux command we are most familiar with. It is like a navigation map in a car or mobile phone, completely liberating our brain and not leaving our body for a moment. Otherwise, we'd have to go downstairs to pick up a delivery, and we'd have to make 18 detours to get home.
We used ls, the map viewer, extensively in the previous Linux Manual course.
1. List only the current directory file
Let's start with a specific problem, how to print out a catalog file alone.
If you use the find command, it is not only simple, but also fully semantic:
$ find . -maxdepth 1 -type d | head . ./ Templates ./. ssh ./. tldr ./. zoom ./. presage ./ Videos ./. racket ./. mplayer ./ node_modules
We can even call echo
$ echo */ anaconda3/ backup.doom.d/ Calibre Library/ Desktop/ Documents/ Downloads/ dwhelper/ emacs.d.backup2/ Mail/ Music/ News/ node_modules/ org/ Pictures/ Public/ snap/ Templates/ Test/ Videos/
Looking at the manual, we will see the option of-d:
-d, --directory list directories themselves, not their contents
Try it directly:
$ ls -d . . #Only one current directory is returned
Use wildcards instead:
$ ls -d */ anaconda3/ Downloads/ News/ snap/ backup.doom.d/ dwhelper/ node_modules/ Templates/ 'Calibre Library/' emacs.d.backup2/ org/ Test/ Desktop/ Mail/ Pictures/ Videos/ Documents/ Music/ Public/
What if D is removed?
The actual implementation is to list all subdirectories of extended wildcards.
2. List only current files
So how do I single out the current file? The easiest way to do this is to query semantically with the find command:
$ find . -maxdepth 1 -type f -iname "[^.]* " |nl 1 ./#* message*-20191127-125216# 2 ./ package-lock.json 3 ./ start.sh 4 ./ backup.gnus.el 5 ./ zero-config.el 6 ./ RMAIL 7 ./ linux-tutorial.org 8 ./ linux-tutorial-2.org 9 ./* scratch*
We know that the ls command has the-F (filter) option, which categorizes all files:
$ ls -F anaconda3/ News/ Anaconda3-2020.02-Linux-x86_64.sh node_modules/ backup.doom.d/ org/ backup.gnus.el package-lock.json 'Calibre Library'/ Pictures/ Desktop/ Public/ Documents/ RMAIL Downloads/ '*scratch*' dwhelper/ snap/ emacs.d.backup2/ start.sh* linux-tutorial-2.org Templates/ linux-tutorial.org Test/ Mail/ Videos/ '#*message*-20191127-125216#' zero-config.el Music/
So let's consider using a combination command:
$ ls -F |grep .* [^/]$ |nl 1 backup.gnus.el 2 linux-tutorial-2.org 3 linux-tutorial.org 4 #*message*-20191127-125216# 5 package-lock.json 6 RMAIL 7 *scratch* 8 start.sh* 9 zero-config.el
Or:
$ ls -l |grep ^- |nl 1 -rw-rw-r-- 1 gaowei gaowei 6042 Feb 4 21:23 backup.gnus.el 2 -rw-rw-r-- 2 gaowei gaowei 892 May 26 08:34 linux-tutorial-2.org 3 -rw-rw-r-- 2 gaowei gaowei 892 May 26 08:34 linux-tutorial.org 4 -rw-rw-r-- 1 gaowei gaowei 139 Nov 27 2019 #*message*-20191127-125216# 5 -rw-rw-r-- 1 gaowei gaowei 313 Dec 10 2019 package-lock.json 6 -rw-rw-r-- 1 gaowei gaowei 74109 Mar 17 14:27 RMAIL 7 -rw-rw-r-- 1 gaowei gaowei 497 Dec 5 2019 *scratch* 8 -rwxr-xr-x 1 gaowei gaowei 1793 May 7 06:20 start.sh 9 -rw-rw-r-- 1 gaowei gaowei 1058 Mar 30 17:10 zero-config.el
At this point we can conclude that ls does not directly list all of the file's scenarios.
3. List files of specified type
Although there is no way to list all files, we can print files with a specific suffix:
$ls *. org #org file www.example.com www.example.com $ls *. sh #shell file www.example.com
And you can say a combination of both:
$ ls *. {org,sh} linux-tutorial-2.org linux-tutorial.org start.sh
4. ubiquitous sort
Sorting is everywhere, and two elementary school students have to be ranked on exams.
The first thing that comes to mind is the sort command.
$ ls -lh | sort -k 5 -h |head total 320K -rw-rw-r-- 1 gaowei gaowei 139 Nov 27 2019 #*message*-20191127-125216# -rw-rw-r-- 1 gaowei gaowei 313 Dec 10 2019 package-lock.json -rw-rw-r-- 1 gaowei gaowei 497 Dec 5 2019 *scratch* -rw-rw-r-- 2 gaowei gaowei 892 May 26 08:34 linux-tutorial-2.org -rw-rw-r-- 2 gaowei gaowei 892 May 26 08:34 linux-tutorial.org -rw-rw-r-- 1 gaowei gaowei 1.1K Mar 30 17:10 zero-config.el -rwxr-xr-x 1 gaowei gaowei 1.8K May 7 06:20 start.sh drwx------ 5 gaowei gaowei 4.0K Feb 4 21:32 Mail drwxrwxr-x 10 gaowei gaowei 4.0K Mar 19 17:46 emacs.d.backup2
Surprisingly, ls thoughtfully prepared the-S option, and, even more thoughtfully, the output was in reverse order.
$ ls -lhS |head total 320K drwxr-xr-x 13 gaowei gaowei 100K Jun 22 15:02 Downloads -rw-rw-r-- 1 gaowei gaowei 73K Mar 17 14:27 RMAIL drwxr-xr-x 8 gaowei gaowei 20K Jun 23 20:01 Videos drwxr-xr-x 3 gaowei gaowei 12K Apr 28 04:50 Desktop drwxr-xr-x 7 gaowei gaowei 12K Jun 20 21:15 Pictures -rw-rw-r-- 1 gaowei gaowei 6.0K Feb 4 21:23 backup.gnus.el drwxrwxr-x 26 gaowei gaowei 4.0K Jun 16 09:02 anaconda3 drwxrwxr-x 2 gaowei gaowei 4.0K Jan 27 13:11 backup.doom.d drwxrwxr-x 6 gaowei gaowei 4.0K Feb 21 16:17 Calibre Library
- S is a short form of long option--sort, which can be sorted by time in addition to file size.
$ls-lh--sort=time| head #is simply ls-ht total 320K drwxr-xr-x 8 gaowei gaowei 20K Jun 23 20:01 Videos drwxr-xr-x 13 gaowei gaowei 100K Jun 22 15:02 Downloads drwxr-xr-x 7 gaowei gaowei 12K Jun 20 21:15 Pictures drwxr-x 26 gaowei gaowei 4.0K Jun 16 09:02 anaconda3 drwxr-xr-x 4 gaowei gaowei 4.0K Jun 15 18:01 snap drwxr-xr-x 15 gaowei gaowei 4.0K Jun 2 10:53 Documents drwxrwxr-x 2 gaowei gaowei 4.0K May 26 10:50 Test-rw-rw-r--2 gaowei gaowei 892 May 26 08:34 www.example.com-rw-rw-r--2 gaowei gaowei 892 May 26 08:34 www.example.com
5. minimalist model
Sometimes, we read the results under the-l option, dazzled, dizzy. Try minimalist mode:
$ ls -sS
Most of the time, we're more concerned with file size, and-s, --size is providing that service.
6. Closing summary
Summarize the above methods:
#1. List only directories ls-d */#2. List only files ls *. {txt, pdf, org}#3. File size sorting ls-lhS #4. Timestamp sorting ls-lht #5. Minimalist mode ls-Ss The above is "How to use ls command in Linux" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.