[Shell] Count the number of files with ls and grep|ls count the number of lines|statistics

Linux ls command

The Linux ls command is used to display the contents of the specified working directory (list the files and subdirectories contained in the current working directory).

grammar

ls [-alrtAFR] [name...]

Parameters:

  • -a Display all files and directories (ls defaults to treat file names or directory names beginning with "." as hidden files and will not be listed)
  • -l In addition to the document name, the document type, permission, owner, document size and other information are also listed in detail
  • -r Display the files in reverse order (it was originally planned to be in alphabetical order)
  • -t List the files in order of creation time
  • -A Same as -a, but does not list "." (current directory) and "..." (parent directory)
  • -F Add a symbol after the listed file name; for example, add "*" for executable files, and add "/" for directories
  • -R If there are files in the directory, the following files are also listed in sequence

After running the ls -l command, the terminal output is a line of characters, and each line of characters corresponds to a directory or file. If it is a file, the first character of the string information on the line displays "-". If it is a directory, the first character in the line is "d", which means directory, find the difference between the two, and run the command that can distinguish the first character

Count the number of files

Count the number of files in the current folder, including those in subfolders

ls -lR | grep "^-" | wc -l

Count the number of directories under the folder, including those in subfolders

ls -lR | grep "^d" | wc -l

Count the number of files in the current folder

ls -l |grep "^-"|wc -l
  • Count the number of directories in the current folder

Count the number of files in a folder, excluding subfolders
eg count the number of .JPEG files in /home

ls -l "/home" | grep ".jpeg" | wc -l

Count the number of files in a folder, including subfolders
eg, under /home, including the number of .JPEG files in subfolders

ls -lR "/home" | grep ".jpeg" | wc -l

Attached:

Count the number of rows of output information

wc -l

Part of the long list output information is filtered, and only general files are retained. If only the directory is retained, it is ^d

grep "^-"

 

Sort 

  1. ls -lS: Arrange from the largest according to the size of the file
  2. ls -lSr: Sort by file size from small to large
  3. ls -lt: Sort by the date of file modification from newest to oldest
  4. ls -lrt: Sort by the date of file modification from oldest to newest

Note : -r is the reverse order

 

statistics

  1. ls -l|grep ^d|wc -l 10: Count the number of folders in this directory
  2. ls -l|grep ^-|wc -l 2: Count the number of files in this directory

original:

https://blog.csdn.net/qq_36653505/article/details/86480309

https://blog.csdn.net/sumengnan/article/details/109259579

 

Guess you like

Origin blog.csdn.net/bandaoyu/article/details/113757725