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
The blogger hasn't used MySQL for a month or ...
Sort Tool The Linux sort command is used to sort ...
User namespace is a new namespace added in Linux ...
Recently, I have been learning to use nginx to pl...
Table of contents Methods of String Object Method...
I'll record the problems I encountered during...
Table of contents 1. The difference between trans...
Table of contents 1. What is deconstruction? 2. A...
Table of contents 1. What is lazy loading? 2. Imp...
You must have inspiration to design a website. Goo...
Overview When a 500 or 502 error occurs during ac...
Table of contents Linux environment variables and...
For those who don't know how to install the s...
Table of contents 1. Download the system image fi...
1. To develop web responsively, the page must ada...