Linux tip: the LS command only lists subdirectory names and prints one file name per line

Time:2020-9-26

Only the subdirectory names under the specified directory are listed

Only subdirectory names are listed in the LS command

In Linux,lsBy default, the command lists all file names in the given directory, including subdirectory names. If you only want to list the subdirectory names under the current directory, you can use thels -d */Command.

Specific examples are as follows:

$ ls
test_dir  test_text
$ ls -d */
test_dir/
$ ls -d *
test_dir  test_text

In this example, there is one in the current directorytest_dirSubdirectories, and atest_textText file.

As you can see,ls -d */Commands are listed onlytest_dirThe name of the subdirectory, and the directory name is/ending.

andls -d *The command also lists all file names in the current directory.

The difference between the two commands will be explained later.

View info LS pair-dThe options are described as follows:

-d, –directory

List just the names of directories, as with other types of files, rather than listing their contents.

That is,ls -dOption lists only the name of the given parameter itself. If the parameter contains a directory name, only the directory name is listed, and all file names in the directory are not listed. If the parameter contains a file name, the file name is listed.

be carefullsWhen the command parameter is a directory name, the default is to list the file names in the directory, including the subdirectory names. If there is no parameter, the default parameter is’. Which is to list the file name in the current directory.

andls -dOption changes this behavior. Instead of getting the file information under the given directory, only the given directory name is listed.

Specific examples are as follows:

$ ls
test_dir  test_text
$ ls -d
.
$ ls test_dir
$ ls -d test_dir
test_dir

As in the example above, there is one in the current directorytest_dirSubdirectories, and atest_textText file.

lsWhen the command does not provide any parameters, equivalent tols .Command to list the file information in the current directory.

ls -dOnly one command was printed., corresponding to the current directory.

ls -damount tols -d .Command, and-dOption specifies to list the given directory name, not the files in the directory, so print one.

ls test_dirDirectory printing is empty becausetest_dirIs an empty directory. There are no files in this directory.

ls -d test_dirThe directory printed test_ Dir, which is giventest_dirDirectory name.

Based on the above description, after opening the debugging information of bash, you can see thatls -d */Command andls -d *The differences between commands are as follows:

$ set -x
$ ls -d */
+ ls --color=auto -d test_dir/
test_dir/
$ ls -d *
+ ls --color=auto -d test_dir test_text
test_dir  test_text
$ set +x

As you can see,*/The result of the expansion istest_dir/, only subdirectory name, no text file name. thatls -d test_dir/Only this directory name is listed.

and*The result of the expansion istest_dir test_textContains the subdirectory name and the text file name. thatls -d test_dir test_textThe given subdirectory name and file file name are listed.

Use the asterisk * wildcard to get the subdirectory name

The biggest difference between the two commands is that*/and*The pathname extension results for are different.

Check out man bash’sPathname ExpansionIn this section, it is explained as follows:

If followed by a /, two adjacent *s will match only directories and subdirectories.

That is, when the asterisk*Wildcard followed by/The extension result of pathname is only directory name and subdirectory name.

If you just want to get the name of the subdirectory under the current directory, directly assign the*/Yes, do not use quotation marks.

Specific examples are as follows:

$ subdirs=*/
$ echo $subdirs
test_dir/

As you can see,subdirs=*/The sentence will putsubdirsThe variable is assigned to the name of the subdirectory under the current directory. The directory name will be assigned with/ending.

When used in a shell script, the working directory of the shell script may not be fixed, so it can be addressed through an absolute path.

Specific examples are as follows:

$ subdirs=/home/sample/*/
$ echo $subdirs
/home/sample/test_dir/

Use LS – 1 to list file names line by line

In general, the LS command prints multiple file names on a single line. If you want to print only one file name per line, you can use-1Options. Notice that this is the number 1, not the letter L.

Check the man LS description of the – 1 option as follows:

-1

list one file per line. Avoid ‘n’ with -q or -b

That is, the – 1 option causes only one file name to be printed per line.

Recommended Today

Redis and Memcache

1. The difference between redis and Memcache Redis supports complex data structures Redis supports native cluster mode Redis only uses a single core, while Memcache can use multiple cores. So on average, the efficiency of storing small data on each core is higher than that of Memcache. For data storage above 100k, Memcache is more […]