Home > Hosting > Server

What is the way to find subdirectories in linux

2023-05-05 15:12:55

<p style="text-align: left;">This article mainly introduces how Linux finds subdirectories. In daily operations, I believe that many people have doubts about the method of finding subdirectories in Linux. The editor consulted various materials and organized This is a simple and easy-to-use operation method, and I hope it will be helpful for everyone to answer the doubts about how to find out the subdirectory in Linux! Next, please follow the editor to learn together!</p><p><br/></p><blockquote><p style="text-align: left;">How to find the subdirectory in linux: 1. Log in to the linux system, open and enter the terminal; 2. Obtain the file path by executing the man ls command; 3. Enter And execute the ls -R | grep ./ command to get all subdirectories under the current directory.</p></blockquote><p style="text-align: left;">Many times, if some file paths are not created, it may cause an error when the program is executed. <br/></p><p style="text-align: left;">So we need to create the required file path in advance. <br/>The premise of this article is that there is already a file path of the test environment, but there is no such path in the new environment. At this time, the file path needs to be obtained first. <br/>When we use</p><p style="text-align: left;"><code>man ls<br/></code></p><p style="text-align: left;">a prompt will appear, when we want to find all the files in the current directory, You can use the following command:</p><p style="text-align: left;"><code>ls -R<br/></code></p><p style="text-align: left;">to get the result as shown in the figure below, but our goal is to find the path, and Not subpaths vs. subfolders. <br/></p><p style="text-align: left;"><img src="//img.freeonlinedomain.com/uploads/allimg/20230505/1-23050515150a08.jpg" title="" alt="1.jpg"/></p><p style="text-align: left;">In fact, you can use</p><p style="text-align: left;"><code>man grep<br/></code></p><p style="text-align: left;">to help with data search. <strong>In fact, we only need to enter</strong></p><p style="text-align: left;"><code>ls -R | grep ./<br/></code></p><p style="text-align: left;">to get the current directory All subdirectories under . <br/></p><p style="text-align: left;">This is the way of thinking more than learning. If we directly learn the find command, we can use</p><p style="text-align: left;"><code>find . -type f<br/></code></p><p style="text-align: left;">You can directly get the path mentioned above.</p><p style="text-align: left;">When it comes to the new environment, how to quickly create so many paths? <br/>Copy the above results and replace<br/><br/> from &quot; ./ &quot; to &quot; mkdir -p ./ &quot;<br/> from &quot; : &quot; to &quot;; &quot;</p><p style="text-align: left;"><code>mkdir -p ./xxxx ;<br/></code></p><p style="text-align: left;">The existing path is no longerCreate, without this path, its parent path can be automatically created, so there is no need to worry about the original path file being replaced and empty. <br/> All the above can be executed, or you can create a xx.sh file and enter commands in it. <br/> Execute</p><p style="text-align: left;"><code>sh xx.sh;<br/></code></p><p style="text-align: left;">If there are too many paths, they cannot be copied to the editor for replacement, How to do it? <br/>The above statement first redirects to a certain file, for example:<br/> &quot;&gt;&quot; is an overlay orientation, so when redirecting, first check whether hello.sh exists, and if it exists, it will be overwritten. <br/> &gt;&gt;&quot; is an additional orientation<br/> command analysis, the . here refers to all paths under the current path. Use / to get all directories under the root directory.</p><p style="text-align: left;">&lt; code&gt;find . -type f &gt; hello.sh;vi hello.sh;<br/></p><p style="text-align: left;">Enter the following command in the editor, it is best to type and copy by hand, and you cannot enter to command mode.</p><p style="text-align: left;"><code>:%s/.//mkdir -p /g<br/>:%s/$/;/g<br/>:wq<br/></code></p><p style="text-align: left;">You can get a sh script at this time, just create a file where you need to create a directory.</p><p style="text-align: left;"><code>sh hello.sh;</code> </p><p style="text-align: left;"><link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css"/><link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-6e43165c0a.css"/></p><p><br/></p>


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us