[LINUX] Process the files in the folder in order with a shell script

[1. What you want to do](#What you want to do) [2. Code introduction](# Code introduction) [3. Execution example](# Execution example) [4. Bonus](# Bonus)

Thing you want to do

Get the data (name) in the folder. In this article, I will introduce a shell script of a loop statement that changes the extension of files in a folder from .txt to .log.

Code introduction

** Current folder status **

Status before execution


$ ls
1.txt   2.txt	3.txt	4.txt	5.txt	main.sh

main.sh


#!/bin/sh
for filename in *.txt
do
	mv ${filename} ${filename%.txt}.log
done

Execution example

main.run sh


sh main.sh

Post-execution status


$ ls
1.log   2.log	3.log	4.log	5.log	main.sh

You can see that the .txt extension has been changed to .log.

bonus

You can also use it if you want to number hundreds of thousands of data. If you add a counter in the middle of the for minute, you can number a large amount of data at once.

main.sh


#!/bin/sh
cnt=1
for filename in *.txt; 
do
	mv ${filename} data_${cnt}.log;
    #Increment of cnt variable
	cnt=`expr ${cnt} + 1`
done

Post-execution status


$ ls
data_1.log  data_2.log  data_3.log  data_4.log  data_5.log  main.sh

Recommended Posts

Process the files in the folder in order with a shell script
Process the contents of the file in order with a shell script
[Python] Get the files in a folder with Python
[Note] A shell script that checks the CPU usage of a specific process in a while loop.
Get a list of files in a folder with python without a path
Read all csv files in the folder
Run the Python interpreter in a script
Manage logrotate generations with a shell script.
Write a script in Shell and Python to notify you in Slack when the process is finished
A memo organized by renaming the file names in the folder with python
View today's weather forecast with a shell script
Erase & generate serial number files with shell script
How to get the files in the [Python] folder
Configure a module with multiple files in Django
Python script that makes UTF-8 files with all BOMs under the folder without BOMs
How to get a list of files in the same directory with python
Get the file name in a folder using glob
2 ways to read all csv files in a folder
How to set a shared folder with the host OS in CentOS7 on VirtualBOX
When a file is placed in the shared folder of Raspberry Pi, the process is executed.
I replaced the Windows PowerShell cookbook with a python script.
[Linux] Copy data from Linux to Windows with a shell script
Try programming with a shell!
A script that opens the URLs written in CSV in order and takes a full screen screenshot
Let's create a script that registers with Ideone.com in Python.
Access the file with a relative path from the execution script.
Note 2 for embedding the scripting language in a bash script
Behavior when giving a list with shell = True in subprocess
Rename and replace remote files using NcFTP in the shell
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
A shell script that just emails the SQL execution result
Recursively get the Excel list in a specific folder with python and write it to Excel.
Let's try a shell script
Give the history command a date and time and collect the history files of all users with a script
Batch convert all xlsx files in the folder to CSV files
I wrote a script to combine the divided ts files
[Linux] Write a deployment tool using rsync with a shell script
A set of script files that do wordcloud in Python3
I wrote a script that splits the image in two
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
Conditional branch due to the existence of a shell script file
Create a shell script to run the python file multiple times
About psd-tools, a library that can process psd files in Python
Output the contents of ~ .xlsx in the folder to HTML with Python
Save the Pydrive authentication file in a different directory from the script
Read a file in Python with a relative path from the program
Write a script to calculate the distance with Elasticsearch 5 system painless
I tried to process the image in "pencil style" with OpenCV
Process the dotted line as a solid line with camelot (Hough transform)
How to determine if a shell script was started in bash
Solve the subset sum problem with a full search in Python
Run a multi-line script in a PDB
reload in django shell with ipython
Enumerate files with a specific extension
Get the script path in Python
Handle requests in a separate process
Install the data files with setup.py
Debug the script with Sakura Editor
Automate environment construction with Shell Script
Read files in parallel with Python
Write a batch script with Python3.5 ~