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

相关推荐
难以触及的高度2 天前
source ~/.bash_profile有什么用
开发语言·bash
Licky133 天前
Centos中dnf和yum区别对比
linux·运维·架构·centos·bash
fengyehongWorld3 天前
Linux bash脚本本地开发环境(Git Bash)配置
linux·bash
异构算力老群群3 天前
从零到一:构建你的第一个AI项目(实战教程)
人工智能·学习·bash·gnu
悟空不是猴子3 天前
VS Code终端命令执行后老是出现 __vsc_prompt_cmd_original: command not found
windows·vscode·prompt·bash
EterNity_TiMe_4 天前
【Linux进程】Linux Shell编程实战:构建简易脚本示例与技巧详解
linux·运维·服务器·学习·centos·bash
the丶only5 天前
获取zabbix API 监控数据shell脚本,自动日常巡检服务器信息、并发送指定群组
linux·运维·服务器·自动化·bash·zabbix
秋刀prince6 天前
简单说说关于shell中zsh和bash的选择
bash
promise5247 天前
Linux cat命令详解使用:高效文本内容管理
linux·运维·服务器·后端·bash
Mostcow8 天前
Linux运维_Bash脚本_源码编译Moby(Docker-CE)-20240803
linux·docker·bash