I have large number of report.txt files in different different folder in a disk. Some file are named as Report.txt and some with suffix like reportmonday.txt. So i want to find and copy all the files contain report to a single folder with rename if already exists. Like report.txt report1.txt report2.txt
I tried some command
find . -type f -iname 'report' -exec mv {} {}_renamed /home/ram/allreport*
this command does not renaming my filename but overrighting. Please help me
CodePudding user response:
A somewhat convoluted way to solve this:
- execute the following:
find . -type f -iname \*report\* | awk 'BEGIN{com="ls /home/ram/allreport/*[Rr]eport*|awk -F. \047{print $NF}\047|sort -n|tail -n1"; com | getline result}{result ; printf "mv \"%s\" /home/ram/allreport/report.%s\n",$0,result}'
- if the output of the above looks sensible add a
| bash
to the end of the above command and run it again ...find . -type f -iname \*report\* | awk 'BEGIN{com="ls /home/ram/allreport/*[Rr]eport*|awk -F. \047{print $NF}\047|sort -n|tail -n1"; com | getline result}{result ; printf "mv \"%s\" /home/ram/allreport/report.%s\n",$0,result}'|bash
Here a pretty version of the awk part w/ explanatory words:
BEGIN { # prerequisite action, this happens before we check what we "find"
com = "ls /home/ram/allreport/*[Rr]eport*|awk -F. '{print $NF}'|sort -n|tail -n1" # we execute an ls on the target directory to get the highest existing report number
com | getline result # and store the number in the variable result
}
{ # here the processing of find's output starts
result # increase result by one for each line
printf "mv \"%s\" /home/ram/allreport/report.%s\n", $0, result # print the command we want to execute,
# a mv of the original file to the target directory with a new name and the counter "result" tacked on.
}
CodePudding user response:
Here's the script that will check any file contains the word 'report' (case insensitive) in it's name and will copy to a desired directory. If a file already exists in that name under the directory it will try to rename report1.txt
, report2.txt
etc. if it can't rename to that number it will assign a random suffix. Like report1354.txt
.
Mind that it will check if directory exists to. If directory does not exist the script will create one.
#!/bin/bash
# make sure the below variable does not contain tailing '/'
# it should be '/home/tfp/allreports' not '/home/tfp/allreports/'
ALL_REPORTS_DIRECTORY="/home/tfp/allreports"
FILE_NAMES_ARRAY=$(find . -type f -name "*[rR][eE][pP][oO][rR][tT]*" 2>>/dev/null)
RENAME_SUFFIX=1
if [[ -d ${ALL_REPORTS_DIRECTORY} ]]; then
echo "directory exists. not creating"
else
mkdir ${ALL_REPORTS_DIRECTORY}
echo "directory doesn't exists. creating"
fi
# to do give random suffix to files under subfolder
for file in ${FILE_NAMES_ARRAY}; do
file_name=$(basename $file)
if [[ -f "${ALL_REPORTS_DIRECTORY}/${file_name}" ]]; then
if [[ -f "${ALL_REPORTS_DIRECTORY}/report${RENAME_SUFFIX}.txt" ]]; then
random_suffix=$RANDOM
echo " couldn't rename '${file_name}'. suffix '${RENAME_SUFFIX}' already exists so giving random suffix"
cp $file ${ALL_REPORTS_DIRECTORY}/report${random_suffix}.txt
echo " '$file' -> new name: report${random_suffix}.txt"
else
echo "file '${file}' exists under '${ALL_REPORTS_DIRECTORY}'. renaming to report${RENAME_SUFFIX}.txt"
cp $file ${ALL_REPORTS_DIRECTORY}/report${RENAME_SUFFIX}.txt
echo " '$file' -> new name: report${RENAME_SUFFIX}.txt"
fi
RENAME_SUFFIX=$(echo $((${RENAME_SUFFIX} 1)))
else
cp $file ${ALL_REPORTS_DIRECTORY}
echo "copied '${file}'"
fi
done
Here's some example output.
$ls
myreport.txt report1.txt report3.txt report55.txt report6.txt Report.txt script.sh sundayReport.txt testfolder
$bash script.sh
directory exists. not creating
copied './Report.txt'
copied './testfolder/rePorT.txt'
copied './testfolder/RReport.txt'
copied './myreport.txt'
copied './sundayReport.txt'
copied './report1.txt'
copied './report6.txt'
copied './report55.txt'
copied './report3.txt'
$bash script.sh
directory exists. not creating
couldn't rename 'Report.txt'. suffix '1' already exists so giving random suffix
'./Report.txt' -> new name: report3262.txt
file './testfolder/rePorT.txt' exists under '/home/tfp/allreports'. renaming to report2.txt
'./testfolder/rePorT.txt' -> new name: report2.txt
couldn't rename 'RReport.txt'. suffix '3' already exists so giving random suffix
'./testfolder/RReport.txt' -> new name: report6693.txt
file './myreport.txt' exists under '/home/tfp/allreports'. renaming to report4.txt
'./myreport.txt' -> new name: report4.txt
file './sundayReport.txt' exists under '/home/tfp/allreports'. renaming to report5.txt
'./sundayReport.txt' -> new name: report5.txt
couldn't rename 'report1.txt'. suffix '6' already exists so giving random suffix
'./report1.txt' -> new name: report31626.txt
file './report6.txt' exists under '/home/tfp/allreports'. renaming to report7.txt
'./report6.txt' -> new name: report7.txt
file './report55.txt' exists under '/home/tfp/allreports'. renaming to report8.txt
'./report55.txt' -> new name: report8.txt
file './report3.txt' exists under '/home/tfp/allreports'. renaming to report9.txt
'./report3.txt' -> new name: report9.txt
$bash script.sh
directory exists. not creating
couldn't rename 'Report.txt'. suffix '1' already exists so giving random suffix
'./Report.txt' -> new name: report32658.txt
couldn't rename 'rePorT.txt'. suffix '2' already exists so giving random suffix
'./testfolder/rePorT.txt' -> new name: report1976.txt
couldn't rename 'RReport.txt'. suffix '3' already exists so giving random suffix
'./testfolder/RReport.txt' -> new name: report26897.txt
couldn't rename 'myreport.txt'. suffix '4' already exists so giving random suffix
'./myreport.txt' -> new name: report31297.txt
couldn't rename 'sundayReport.txt'. suffix '5' already exists so giving random suffix
'./sundayReport.txt' -> new name: report29608.txt
couldn't rename 'report1.txt'. suffix '6' already exists so giving random suffix
'./report1.txt' -> new name: report7259.txt
couldn't rename 'report6.txt'. suffix '7' already exists so giving random suffix
'./report6.txt' -> new name: report16691.txt
couldn't rename 'report55.txt'. suffix '8' already exists so giving random suffix
'./report55.txt' -> new name: report12176.txt
couldn't rename 'report3.txt'. suffix '9' already exists so giving random suffix
'./report3.txt' -> new name: report11087.txt
final ls
from destination directory
$ls -lah
total 8,0K
drwxrwxr-x 2 tfp tfp 4,0K Sep 15 18:37 .
drwxr-xr-x 46 tfp tfp 4,0K Sep 15 18:32 ..
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 myreport.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report11087.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report12176.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report16691.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report1976.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report1.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report26897.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report29608.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report2.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report31297.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report31626.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report3262.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report32658.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report3.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report4.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report55.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report5.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report6693.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report6.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:37 report7259.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report7.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report8.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 report9.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 rePorT.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 Report.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 RReport.txt
-rw-rw-r-- 1 tfp tfp 0 Sep 15 18:36 sundayReport.txt