Linux shell脚本用于复制和重命名多个文件
Linux shell脚本用于复制和重命名多个文件
我有以下代码片段:
#!/bin/bash parent=/parent newfolder=/newfolder mkdir "$newfolder" for folder in "$parent"/*; do if [[ -d $folder ]]; then foldername="${folder##*/}" for file in "$parent"/"$foldername"/*; do filename="${file##*/}" newfilename="$foldername"_"$filename" cp "$file" "$newfolder"/"$newfilename" done fi done
我确实需要以某种方式将其转换,使得复制的文件将以它们被移动到的文件夹命名(例如,移动到/root/Case22文件夹中的文件将被重命名为case22_1.jpg、case22_2.docx、case22_3.JPG等)。文件将从USB中复制,用户将输入目标和源目录。我已经编写了其他所有内容,只是实际重命名部分不起作用,我想我可以改编这段代码。
谢谢
p.s. 这段代码由Jahid编写,可以在stackoverflow上找到。
admin 更改状态以发布 2023年5月20日
感谢你们的帮助。我已经找到了解决方法,并且想在这里发布它,以防其他人查看它。正如标题所示,我需要一个Linux shell脚本来复制和重命名多个文件,保留原始目录结构(脚本的用户将指定文件来源和归档位置)。在研究不同来源几天后,我想出了以下代码(它包括一个陷阱,仅在同一时间运行一个脚本实例):
lockdir=/var/tmp/mylock #directory for lock file creation pidfile=/var/tmp/mylock/pid #directory to get the process ID number if ( mkdir ${lockdir} ) 2> /dev/null; then #main argument to create lock file echo $$ > $pidfile #if successful script will proceed, otherwise it will skip to the else part of the statement at the end of the script trap 'rm -rf "$lockdir"; exit $?' INT TERM EXIT #trap to capture reasons of script termination and removal of the lock file so it could be launched again #start of the main script body, part of successful "if" statement # read entry_for_testing_only #request for user entry to keep script running and try to run another script instance findir="$2/$(basename "$1")" #variable that defines final directory to which files from USB will be copied if [ ! -d "$1" ]; then #testing if first directory entry is a valid directory’’ echo "$1" "is not a directory" echo "" exit else if [ ! -d "$2" ]; then #testing if second entry is a valid directory echo "archive directory non existant" exit else if [ -d "$findir" ] && [ "$(ls -A "$findir")" ]; then #testing if second entry directory contains the same name folders and if the folders are empty - to avoid file overwriting echo "such folder already there and it's not empty" exit else if [ ! -d "$findir" ] ; then #last archive directory argument to create final archive directory mkdir "$findir" else true fi fi fi fi rsync -a "$1"/ "$findir" #command to copy all files from the source to the archive retaining the directory tree moved_files="$(find "$findir" -type f)" #variable that finds all files that have been copied to the archive directory for file in $moved_files; do #start of the loop that renames copied files counter="$((counter+1))" #incrementation variable source_name="$(basename "$1")" #variable that captures the name of the source directory new_name="$source_name"_"$counter" #variable that puts start of the file name and incrementation element together if echo "$file" | grep "\." #argument that captures the extension of the file then extension="$(echo "$file" | cut -f2 -d. )" else extension= fi full_name="$new_name"."$extension" #variable that defines the final new name of the file dir="$(dirname "${file}")" #variable that captures the directorry address of currently looped file mv "$file" "$dir/$full_name" #move command to rename currently looped file with the final new name done #end of the main script body, unsuccessful "if" statement continues here else echo "Another instance of this script is already running. PID: $(cat $pidfile)" fi