Linux编写Shell脚本获取指定目录下所有文件并处理

发布时间 2023-12-07 13:19:18作者: sowler

Linux编写Shell脚本获取指定目录下所有文件进行处理并保存到新目录

#!/bin/bash

app_name="shell"

path="/dir" #原目录
last_path="/newDir" #新目录



echo "======================start ${app_name}=============================="

if [ -n "$(ls $path)" ]; then
	#echo "recordings is not empty"
	cd $path
	
	for file in *; do
		if [ -f "$file" ]; then # 如果是文件
			#echo "this is a file:$file"
			extension="${file##*.}" # 获取文件名中最后一个点号之后的部分作为后缀
			case $extension in #判断文件类型
				txt)
					mv $file $last_path
					echo $file" is nfo";;
				mp4)
					filename=$(basename "$file" .mjr)
					#echo "文件名(不包含后缀):$filename"
					if [[ "${file}" == *video* ]]; then #视频
						/usr/local/bin/pp-rec $file  $last_path/$filename.mp4
					  
						echo $file" 文件名包含 video"
					elif [[ "${file}" == *audio* ]]; then #语音
					
						/usr/local/bin/pp-rec $file  $last_path/$filename.mp3
						echo  $file" this is audio"  
					fi
					
					rm $file
					echo $file" is mp3";;
				*)
					echo $file" not file type";;
			esac
		elif [ -d "$file" ]; then # 如果是目录
			echo "this is a dir:$file"
		fi
	done

else
    echo "shell is empty"
fi

echo "======================end ${app_name}================================="