Extract specific file paths in folders based on Linux commands

Extract specific file paths in folders based on Linux commands

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

./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

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:

test.py
regex.py
cnt_fail.py

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 {} \;
Search results:

./test
./test
./MongoDB

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.
There is no direct processing capability for error messages. It is then passed to the next command as standard input.

The command after adding sort is

find . -name '*.py' -exec dirname {} \; | sort -u

The running results are:

./MongoDB
./test

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:

find . -name '*.py' -exec dirname {} \; | sort -u | cut -c3-

Running results:

MongoDB
test

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:
  • Linux command to decompress rpm package and introduction to rpm command usage
  • Summary of ten Linux command aliases that can improve efficiency
  • The difference and usage of Ctrl+z, Ctrl+c and Ctrl+d in Linux commands
  • Example code of Linux command to create date folder or file
  • How to save command output to a file in Linux terminal
  • The most comprehensive collection of commonly used Linux commands (with examples)

<<:  Element avatar upload practice

>>:  Summary of Mysql update multi-table joint update method

Recommend

Solution to MySQL error code 1862 your password has expired

The blogger hasn't used MySQL for a month or ...

Detailed explanation of Linux commands sort, uniq, tr tools

Sort Tool The Linux sort command is used to sort ...

Detailed explanation of Linux Namespace User

User namespace is a new namespace added in Linux ...

Detailed example of SpringBoot+nginx to achieve resource upload function

Recently, I have been learning to use nginx to pl...

JavaScript String Object Methods

Table of contents Methods of String Object Method...

How to use Vue to implement CSS transitions and animations

Table of contents 1. The difference between trans...

Detailed explanation of JS ES6 variable destructuring assignment

Table of contents 1. What is deconstruction? 2. A...

The front-end must know how to lazy load images (three methods)

Table of contents 1. What is lazy loading? 2. Imp...

I have compiled a few cool design sites that I think are good.

You must have inspiration to design a website. Goo...

Detailed explanation of PHP+nginx service 500 502 error troubleshooting ideas

Overview When a 500 or 502 error occurs during ac...

Introduction to Linux environment variables and process address space

Table of contents Linux environment variables and...

VMware Workstation installation Linux (Ubuntu) system

For those who don't know how to install the s...

Summary of WEBAPP development skills (notes for mobile website development)

1. To develop web responsively, the page must ada...