Managing Test Files with create_files and delete_files Bash Scripts

Table of contents

  • [Create_files Script](#Create_files Script)
  • [Delete_files Script](#Delete_files Script)

Introduction: In software development and testing, we often need to create and manage various test files. To automate this process and make it more efficient, we can use bash scripts. In this blog, we will introduce two simple bash scripts, create_files and delete_files, which help to create and delete test files respectively.

Note: "\Code" and "\Test" are sibling directories, which means they are located at the same level in the directory structure. The script or code is executed from the "\Code" directory.

Create_files Script

The create_files script is designed to automate the process of creating test files with specific naming conventions. For every file in your directory that follows the naming pattern test<num>.cmm, the script creates two new files named test<num>_1.cmm and test<num>_2.cmm, if they don't already exist.

Here is the content of the create_files script:

复制代码
#!/bin/bash

for file in test*.cmm; do
  if [[ -f "$file" ]]; then
    filename=$(basename -- "$file")
    extension="${filename##*.}"
    filename_without_extension="${filename%.*}"
    num="${filename_without_extension#test}"

    new_file1="test${num}_1.cmm"
    new_file2="test${num}_2.cmm"

    if [[ ! -f "$new_file1" ]]; then
      touch "$new_file1"
      echo "Created $new_file1"
    fi

    if [[ ! -f "$new_file2" ]]; then
      touch "$new_file2"
      echo "Created $new_file2"
    fi
  fi
done

To use the create_files script, follow these steps:

  1. Save the above script to a file named create_files in your project directory.

  2. Give execute permissions to the script:

    chmod +x create_files

  3. Run the script:

    ./create_files

Delete_files Script

The delete_files script is used to delete files that follow the naming pattern test<num>_1.cmm and test<num>_2.cmm, while keeping the original test<num>.cmm files intact.

Here is the content of the delete_files script:

复制代码
#!/bin/bash

for file in test*_1.cmm test*_2.cmm; do
  if [[ -f "$file" ]]; then
    rm "$file"
    echo "Deleted $file"
  fi
done

To use the delete_files script, follow these steps:

  1. Save the above script to a file named delete_files in your project directory.

  2. Give execute permissions to the script:

    chmod +x delete_files

  3. Run the script:

    ./delete_files

相关推荐
承渊政道13 小时前
【MySQL数据库学习】(MySQL库的操作和表的操作)
数据库·学习·mysql·ubuntu·bash·数据库架构·数据库系统
IMPYLH1 天前
Linux 常用命令列表
linux·运维·服务器·bash
承渊政道1 天前
【MySQL数据库学习】(MySQL数据库基础)
数据库·学习·mysql·ubuntu·bash·数据库架构·数据库系统
IT大白鼠2 天前
Shell基础与Bash常用技巧:命令替换、重定向、管道、作业控制
bash
0x00073 天前
Git Bash 中无法启动 Claude Code ?
开发语言·git·bash
IMPYLH3 天前
Linux 的 users 命令
linux·运维·服务器·前端·数据库·bash
IMPYLH3 天前
Linux 的 whoami 命令
linux·运维·服务器·bash
IT大白鼠4 天前
Linux用户配置文件详解:.bash_history、.bash_logout、.bash_profile与.bashrc
linux·运维·bash
承渊政道4 天前
Linux系统学习【进程概念从入门到深入理解】
linux·服务器·笔记·学习·ubuntu·系统架构·bash
IMPYLH4 天前
Linux 的 wc 命令
linux·运维·服务器·前端·bash