Recently, there is a need to automatically search for specific files in a specific folder, and the file path and file name need to be saved separately. Although it can be achieved using Python's walk, it feels a bit complicated. So I want to see if the built-in commands of Linux can complete this task. environment The directory structure to be searched is as follows . |____test | |____test2.txt | |____test.py | |____test.txt | |____regex.py |____MongoDB | |____.gitignore | |____cnt_fail.py | |____db Goal 1: Get all py file names If you only use find . -name '*.py' to search, the result is the path
If we only need the file name, we can use the command basename provided by linux To process all the search results of find using basename, we need to use the parameter -exec of find. The final command is: find . -name '*.py' -exec basename {} \; result:
The {} are used in conjunction with the -exec option to match all results and then extract their file names. Objective 2: Get all py file paths, remove duplicates, and delete the leading "./" character Linux also has a command dirname to get the file path Slightly modify the previous command to display all file paths find . -name '*.py' -exec dirname {} \;
We can see that there are duplicate paths. To remove duplicates in Linux, we can use sort and add the -u parameter. The -u parameter is used to remove duplicates in the sorting results. We need to pass the output of the previous command to sort as input, and naturally we think of pipes. The pipe command operator is: |, which can only process the correct output information transmitted by the previous command, that is, the standard output information. The command after adding sort is The running results are:
Finally, we use cut to delete the ./ character before each path. The parameter -c3- means to extract the substring from the third character of the string (starting position is 1) to the end. The final command is: Running results:
The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Element avatar upload practice
>>: Summary of Mysql update multi-table joint update method
Table of contents 1. Prototype 2. Prototype point...
Docker Quickly Install Zookeeper I haven't us...
Table of contents 1. Introduction to platform bus...
1. Basic usage examples of float 1. Let's fir...
1. What is Parallax scrolling refers to the movem...
Learning objectives: The two functions parseInt()...
Table of contents Preface What is ssh What is ssh...
This article mainly introduces how to specify par...
Install the unzipped version of MySql database un...
Table of contents 1. Object properties 1.1 Attrib...
1. Why do we need to divide tables and partitions...
This article shares the specific code of jQuery t...
How is Line-height inherited?Write a specific val...
Let's briefly sort out the configuration of s...
The previous article on Docker mentioned the cons...