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

A brief discussion on JS prototype and prototype chain

Table of contents 1. Prototype 2. Prototype point...

Detailed tutorial on how to quickly install Zookeeper in Docker

Docker Quickly Install Zookeeper I haven't us...

Detailed explanation of the platform bus of Linux driver

Table of contents 1. Introduction to platform bus...

Detailed examples of float usage in HTML/CSS

1. Basic usage examples of float 1. Let's fir...

CSS complete parallax scrolling effect

1. What is Parallax scrolling refers to the movem...

JavaScript parseInt() and Number() difference case study

Learning objectives: The two functions parseInt()...

What is ssh? How to use? What are the misunderstandings?

Table of contents Preface What is ssh What is ssh...

How to specify parameter variables externally in docker

This article mainly introduces how to specify par...

Introduction to new ECMAscript object features

Table of contents 1. Object properties 1.1 Attrib...

jQuery achieves breathing carousel effect

This article shares the specific code of jQuery t...

How to inherit CSS line-height

How is Line-height inherited?Write a specific val...