Execute bash script under Windows to batch process files (modify file names, delete files)

Shortly after writing the continuation, I saw this poem and wrote it down with emotion:

                                        "Linjiang Immortal" [Song Dynasty] Yan Jidao

After the dream, the balcony is high and locked, and the curtain is low when drunk. Last year, spring regret came. The falling flower man is independent, and the wisps fly together.

I remember when Xiao Ping first met her, Luo Yi had a double heart. The pipa strings speak of lovesickness. The bright moon was there at that time, and it once illuminated the colorful clouds.

Table of contents

I. Introduction

2. Procedure

1. Batch modify file names based on modification date

2. Delete files based on creation date

1) Delete the files of the day

2) Delete files in the specified folder according to the month

3) According to the document of the specified date

3. Conclusion


I. Introduction

In my daily use of the computer, I copied a bunch of files by mistake, and it was too troublesome to delete them one by one. So I wanted to be lazy and learned a bash script to achieve batch modification of file names, batch deletion, etc.

Note: The execution process of the following program is: modify the file name with the .sh suffix, then right-click in the folder where the script is located to open the git bash environment, and execute {bash fileName.sh} to successfully run the script.

2. Procedure

1. Batch modify file names based on modification date

1) Description

Because I download a large number of pictures on QQ, the picture names are very messy. It would be too slow to change them one by one manually, so as to modify the file names in batches according to the modification date.

2) Process

First, I went to learn to traverse all the files in the folder and got the following results:

 do and done are the starting and ending characters, and the fifth line is through learning this blog, which is used to obtain the last modification time And assign it as a string to the variable changeTime; then intercept the 1st to 10th string, which happens to be the year, month and day, assign it to lastChangeTime, and finally output the time and output file name.

 1. Preliminary attempt (it is recommended not to run this)

#! /bin/sh

files=$(ls $folder)

for file in $files

do

  changeTime=$(stat -c %y $file)

lastChangeTime=$(expr substr "$changeTime" 1 10)

mv "$file" "QQ screenshot_lastChangeTime.jpg"

echo $lastChangeTime

  echo $file

done

Disadvantages: If they are files of the same date, only the files that were modified later will be left. Moreover, what is ironic is that the cmd file itself is also included, so it will be gloriously sacrificed. Hahaha!

2. A more complete version

By studyingthis blog, and the blog that introduces intercepting strings< a i=4>, achieves the expected purpose, but the above drawbacks are still there, the same date cannot be excluded, only the learn.cmd file is excluded to prevent it from being swallowed (self-eating, xswl). This is directly in the folder, right-click in the directory, enter the git bash command line interface, and then enter the command {bash file name.cmd}. Remember how to modify if it is 10, it will take effect in the current directory, please do not do it randomly If you run it, important files may be lost! Please do not run it randomly! ! !

#! /bin/sh

files=$(ls $folder)
for file in $files
do
  changeTime=$(stat -c %y $file)
lastChangeTime=$(expr substr "$changeTime" 1 15)
<<BLOCK
上面这行是截取字符串的作用,调用expr中的substr函数
可以将15修改为10,15是囊括了后面的内容,避免日期重复使文件消失
BLOCK
if [ "$file" != "learn.cmd" ];
then
mv "$file" "截图_$lastChangeTime.jpg"
echo $lastChangeTime
  echo ${file:0-4}
fi
done

2. Delete files based on creation date
1) Batch delete files of the day
#!/bin/bash

# 指定要删除文件的文件夹路径
folder_path="/path/to/folder"

# 获取当前日期
current_date=$(date +%Y-%m-%d)

# 查找并删除当天创建的文件
find "$folder_path" -type f -newermt "$current_date" ! -newermt "$current_date + 1 day" -exec rm {} \;
2) Delete files in the specified folder according to the month
	#!/bin/bash
	
	# 指定要删除文件的文件夹路径
	folder_path="/path/to/folder"
	
	# 指定要删除的年份和月份,以2023年9月份的为例
	year="2023"
	month="09"
	
	# 构建日期字符串,格式为YYYY-MM-DD
	date_string="${year}-${month}-01"
	
	# 获取下个月的日期字符串,用于限制删除范围
	next_month=$(date -d "${date_string} + 1 month" +%Y-%m-%d)
	
	# 查找并删除指定日期范围内的文件
	find "$folder_path" -type f -newermt "$date_string" ! -newermt "$next_month" -exec rm {} \;
3) Delete files based on specified date
#!/bin/bash
	
# 指定要删除文件的文件夹路径
folder_path="/path/to/folder"
	
# 指定要删除的日期
year="2023"
month="11"
day="08"

# 构建日期字符串,格式为YYYY-MM-DD
date_string="${year}-${month}-${day}"

# 查找并删除指定日期的文件
find "$folder_path" -type f -newermt "$date_string" ! -newermt "$date_string + 1 day" -exec rm {} \;
3. Conclusion

More will be added later, (・∀・(・∀・(・∀・*)

Guess you like

Origin blog.csdn.net/m0_63944500/article/details/132218729