Replace the directory name and the file name in the directory together with a Linux command.

The problem you want to solve

There are multiple texts with the name Hoge in the directory Hoge. Here, I want to replace both the directory name and the file name with hoge.

Description of the replace command

find `pwd` -name "*Hoge*" -print | perl -nle 'rename $_, s|Hoge|hoge|r'

Until print

At this point, a list of target directories and files will appear. I don't use -print0 because I want perl to process each line.

% find `pwd` -name "*Hoge*" -print                                                 
/Users/omokawayasuaki/test/Hoge
/Users/omokawayasuaki/test/Hoge/Hoge1.txt
/Users/omokawayasuaki/test/Hoge/Hoge3.txt
/Users/omokawayasuaki/test/Hoge/Hoge2.txt

perl part

s|Hoge|hoge|r'ofrIs the default variable$_Option to replace and return as a scalar value. The feature is that the original character string replaced is not changed.

reference

https://perldoc.jp/docs/perl/5.18.1/perlreref.pod#OPERATORS

State before conversion

% tree                                                                             
.
└── Hoge
    ├── Hoge1.txt
    ├── Hoge2.txt
    └── Hoge3.txt

Hit the conversion command twice

% find `pwd` -name "*Hoge*" -print | perl -nle 'rename $_, s|Hoge|hoge|r'
% find `pwd` -name "*Hoge*" -print | perl -nle 'rename $_, s|Hoge|hoge|r'

State after conversion

% tree                                                                             
.
└── hoge
    ├── hoge1.txt
    ├── hoge2.txt
    └── hoge3.txt

that's all

Further challenges

I couldn't overwrite the old directory name in one go, so I ended up hitting the command twice. To be honest, it's annoying. If you have a smarter solution, please let me know: bow:

Recommended Posts

Replace the directory name and the file name in the directory together with a Linux command.
A command to specify a file with a specific name in a directory with find and mv, cp, or gzip it (linux)
Search the file name including the specified word and extension in the directory
Extract only the file name excluding the directory in the directory
Adjust file permissions with the Linux command chmod
View the full path (absolute path) of a file in a directory in Linux Bash
Zip-compress any file with the [shell] command to create a file and delete the original file.
Replace the named entity in the read text file with a label (using GiNZA)
Get the file name in a folder using glob
Linux file and directory permissions
When the variable name conflicts with the Devaga command in pdb
Save the pystan model and results in a pickle file
[Note] Import of a file in the parent directory in Python
[Linux] File and directory operation commands
Process the contents of the file in order with a shell script
How to make a command to read the configuration file with pyramid
Save the Pydrive authentication file in a different directory from the script
How to output the output result of the Linux man command to a file
Read a file in Python with a relative path from the program
Dig the directory and create a list of directory paths + file names
Format the Git log and get the committed file name in csv format
Try rewriting the file with the less command
A memo organized by renaming the file names in the folder with python
[Linux] Command to get a list of commands executed in the past
The file name was bad in Python and I was addicted to import
A story when a directory is buggy with the django-admin start project command in a virtual environment using Pipenv
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.
Function to extract the maximum and minimum values ​​in a slice with Go
Create a Python image in Django without a dummy image file and test the image upload
[Python] Get the files in a folder with Python
Linux [directory command]
Set a fixed IP in the Linux environment
Compile and run Rust with a single command
Save the object to a file with pickle
It's a Mac. What is the Linux command Linux?
Archive and compress the entire directory with python
What! The unzipped file name was SJIS! command?
I want to get the file name, line number, and function name in Python 3.4
How to read a file in a different directory
A tool to insert the country name and country code in the IP address part
How to get a list of files in the same directory with python
Describe the multi-stage ssh destination in the config, log in easily, and copy the file with scp
I want to replace the variables in the python template file and mass-produce it in another file.
For Debian users who are having trouble with a bug in the Linux kernel 5.10
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
When a local variable with the same name as a global variable is defined in the function
Specify the volume on linux and make a sound
Create Amazon Linux with AWS EC2 and log in
[Linux] Execute git pull regularly with the crontab command
Read the csv file and display it in the browser
Load the module with the same name in another location
Run a Python file with relative import in PyCharm
Output a binary dump in binary and revert to a binary file
Get a participant's username and screen name in Slack
[Linux] Create a self-signed certificate with Docker and apache
Install security update (KB) individually on Windows Server with Ansible Part 2 ~ Dynamically acquire multiple MSU files under the directory with the ls command and install in a loop
Create a docx file with thumbnails of photos in the folder pasted with the Python python-docx library and the free software "Reduction only."
Linux command [File operation]
A server that returns the number of people in front of the camera with bottle.py and OpenCV
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
[Golang] Command to check the supported GOOS and GOARCH in a list (Check the supported platforms of the build)