【问题标题】:Linux shell script to copy and rename multiple files用于复制和重命名多个文件的 Linux shell 脚本
【发布时间】:2016-10-21 16:07:56
【问题描述】:

我有这个sn-p:

#!/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 复制,并且目标和源目录都将由用户输入。我已经写了其他所有东西,它除了实际重命名之外还可以工作,我认为我可以调整这个 sn-p。

谢谢

附言sn-p 是 Jahid 写的,在 stackoverflow 上可以找到

【问题讨论】:

标签: shell


【解决方案1】:

你可以试试这样的;

#!/bin/bash
parent=/root
a=1
for file in $parent/Case22*; do
filename="${file%.*}"
extension="${file##*.}"
  newfilename=$(printf "$filename"_"$a"."$extension") 
  mv -- "$file" "$newfilename"
  let a=a+1
done

【讨论】:

    【解决方案2】:

    感谢您的帮助。我找到了解决方案,并认为我可以将其发布在这里,以防其他人看到这个问题。 正如标题所示,我需要一个 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

    【讨论】:

    • 标记为已接受,因为它正在工作并且做了它必须做的事情。并不意味着它是达到所需结果的最优雅或最佳方式。
    猜你喜欢
    • 2018-06-09
    • 1970-01-01
    • 2016-09-27
    • 2016-03-14
    • 2021-11-30
    • 1970-01-01
    • 2013-05-02
    • 2011-06-19
    • 2011-04-20
    相关资源
    最近更新 更多