2016-10-21 9 views
-1

私はこのスニペットを持っていますたとえば/ root/Case22ファイルに移動すると、case22_1.jpg、case22_2.docx、case22_3.JPGなどに名前が変更されます)。ファイルはUSBからコピーされ、宛先と宛先の両方の宛先がユーザーによって入力されます。私は他のすべてを書いています。実際の名前の変更とは別に動作し、このスニペットに順応できると思いました。Linuxのシェルスクリプトは、複数のファイルをコピーし、名前を変更する

ありがとう

p.s.スニペットはJahidによって書かれ、stackoverflowで見つけた

+0

これではありませんコードライティングサービス。あなたは、番号をつけ直すものを追加しようとします。私たちは多分、それを修正しようと試みます。 –

+0

@Marc建設的な批判のおかげで。私はいくつかのポストとスタックオーバーフローの答えと私の道を失った;)によって誤解された;) –

答えて

0

ありがとうございました。私は解決策を見つけて、他の誰かがこれを見ている場合に私がここに投稿するかもしれないと考えました。 タイトルが示唆しているように、元のディレクトリツリー(ファイルのソースとアーカイブの場所はスクリプトのユーザによって指定される)を保持したまま、複数のファイルをコピーして名前を変更するLinuxシェルスクリプトが必要でした。ここで私は、さまざまなソースの数日の調査の後に思い付いたコードは(それがこれだけのスクリプトの1つのインスタンスが同時に実行されるだろうトラップを含みます)です。

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’’ 
 
\t   echo "$1" "is not a directory" 
 
\t   echo "" 
 
\t   exit 
 
     else 
 
\t   if [ ! -d "$2" ]; then #testing if second entry is a valid directory 
 
\t \t   echo "archive directory non existant" 
 
\t \t   exit 
 
     else 
 
\t   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 
 
\t \t   echo "such folder already there and it's not empty" 
 
\t \t   exit 
 
       else 
 
\t     if [ ! -d "$findir" ] ; then #last archive directory argument to create final archive directory 
 
\t       mkdir "$findir" 
 
\t     else true 
 
\t     fi 
 
\t   fi 
 
\t   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 
 
\t   counter="$((counter+1))" #incrementation variable 
 
\t   source_name="$(basename "$1")" #variable that captures the name of the source directory 
 
\t   new_name="$source_name"_"$counter" #variable that puts start of the file name and incrementation element together 
 
\t   if echo "$file" | grep "\." #argument that captures the extension of the file 
 
\t \t   then 
 
\t \t \t   extension="$(echo "$file" | cut -f2 -d.)" 
 
\t \t   else 
 
\t \t \t   extension= 
 
\t   fi 
 
\t   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

+0

それは働いていたために受け入れられたとマークし、それはしなければならなかった。必要な結果を達成する最もエレガントな方法であるとは限りません。 –

0

このようなものを試すことができます。

#!/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 
関連する問題