In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you how the linux command to extract a specific file path in the folder, the content is easy to understand, I hope you can learn, after learning there will be gains, the following let Xiaobian take you to take a look at it.
Recently, you need to automate the search for specific files in a specific folder, and you need to save the file path and file name respectively. It is possible to implement walk using python, but it feels a bit complicated. So I wanted to see if the commands that came with linux could do the job.
Environment
The directory structure you need to find 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 including road strength
. / test/test.py
. / test/regex.py
. / MongoDB/cnt_fail.py
If we only need the file name, we can use the command basename provided by linux.
Using basename to process all search results of find, we need to use the parameter of find-exec
The final command is:
Find. -name'* .py'- exec basename {}\
Results:
Test.py
Regex.py
Cnt_fail.py
Where {} is used in conjunction with the-exec option to match all results and then fetch their file names.
Goal 2: get all py file paths, repeat, and delete the first ". /" character
Linux also has the command dirname to get the file path
Slightly modify the previous command to display all file paths
Find. -name'* .py'- exec dirname {}\
Search results:
. / test
. / test
. / MongoDB
You can see that there are duplicates in the path. Linux can be used to remove duplicates by using sort to add the-u parameter, and the-u parameter is used to remove the duplicates in the sorting result.
We need to pass the output of the previous command to sort as input, so we naturally think of the pipeline.
The pipeline command operator is: |, which can only handle the correct output information sent through the previous instruction, that is, the information of standard output, for stdandard
There is no direct processing capability for error information. Then, pass it to the next command as the standard input standard input.
The command after adding sort is
Find. -name'* .py'- exec dirname {}\; | sort-u
The running result is as follows:
. / MongoDB
. / test
Finally, we use cut to delete the. / character before each road strength, parameter-c3-meaning to extract the third character of the string (starting position 1) to the last substring.
The final command is:
Find. -name'* .py'- exec dirname {}\; | sort-u | cut-c3-
Running result:
MongoDB
Test
This is about how the linux command extracts a specific file path in a folder. If you have learned knowledge or skills, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.