Personal tips on working with CentOS servers

1 minute read

(Added yyyy.mm.dd)


Introduction

I will leave a personal memorandum that I often search in CentOS server construction.
I will add things that I thought “I searched before …” one by one.

Compress / decompress with tar command

References: [tar] command-create / extract archive file

tar -zcvf xxxx.tar.gz directory to compress#compression

tar -zxvf xxxx.tar.gz #Defrost
  • Optional supplement
    –c: (Compression only) Create new archive
    –x: (decompression only) Extracted from existing archive
    –f: Archive specification
    –v: Detailed display

Lowercase and uppercase conversion with the tr command

tr '[:upper:]' '[:lower:]' #Uppercase → lowercase

tr '[:lower:]' '[:upper:]' #Lowercase → uppercase

Shell script template

Requirements

–Repeat processing for files in a specific input directory
–Output is a specific directory
――I want to be able to run it anywhere
–I want to write to the log so that I can see the start and end
–Initialize output directory and log

Shell script template

template


#!/bin/bash
##Outline of processing####################################################################
#Roughly write what kind of processing

##variable########################################################################

#Get current directory
SCRIPT_DIR=$(cd $(dirname $0); pwd)

#Output log
LOG="${SCRIPT_DIR}/$(basename $0 ".sh")_$(date +%Y%m%d).log"

#Appropriate directory
INPUT_DIR="${SCRIPT_DIR}/xxx"

#Output directory
OUTPUT_DIR="${SCRIPT_DIR}/xxx"

#Number of suitable directories
LIST_NUM=$(ls ${INPUT_DIR} | wc -l)

##This process######################################################################

#Log generation(Initialization)
cat /dev/null > ${LOG}

#Check the existence of the output directory
#Create a directory if it does not exist, and initialize it in the directory if it exists.
if [ -e ${OUTPUT_DIR} ]; then
  rm -rf ${OUTPUT_DIR}/*
else
  mkdir ${OUTPUT_DIR}
fi

echo "$(date +%T)Start processing(Target:${LIST_NUM}Case)" >> ${LOG}
echo >> ${LOG}

#Get input file
INPUT_ARRAY=$(find ${INPUT_DIR} -maxdepth 1 -type f)

#Export for INPUT files
for file in ${INPUT_ARRAY}
do

  #Trimming as needed
  # INPUT_NAME=$(basename ${file})

  #Good processing
  COMMAND -i ${file} -o ${OUTPUT_DIR}/${INPUT_NAME} >> ${LOG} 2>&1
  echo "$(date +%T)Processing completed:${INPUT_NAME}" >> ${LOG}
done

echo >> ${LOG}
echo "$(date +%T)Processing Exit(Total number:$(ls ${OUTPUT_DIR} | wc -l)Case)" >> ${LOG}
echo "Normal termination LOG: ${LOG}"

Other