The script below will identify the oldest folder in the directory and and stored as $filename and the parsed value is $folder.... user_S5-0271-96-v5.6 is an example.
The awk extracts the matching pdf based on the $filename variable. That pdf is parsed and the user_S5-0271-96-v5.6 is stored in $pdf. The set -xv shows that nothing populates in $pdf. I am not sure why though?
I will then perform the match on the $folder = $pdfand cp. to the matching $folder in $dir. Is there another better way or am I getting closer?
I also removed the gsub(/^0+/,"", FNUM) as that is not needed to match the pdf.
Code:
set -xv # add error checking
dir=/home/cmccabe/folder # define directory as dir
# Find oldest directory
find "$dir" -maxdepth 1 -mindepth 1 -type d -printf '%T+\t%P\0' | sort -z | #s earch dir for only folders by time and sort
while read -r -d $'\t' time && read -r -d '' filename # read each folder into $filename and grab oldest
do # start loop
printf "The oldest folder is $filename, created on $time\n" # print message with oldest folder
folder="$(echo $filename|cut -d'_' -f8-)" # split on _ and print # create $folder variable with parse output
echo "The folder is" "$folder" # confirm parse message --- user_S5-0271-96-v5.6 --- is an example
# Find matching pdf
pdf=$(awk -v FL="$filename" ' # store oldest folder and $FL
FNR == 1 {filenum++} # start loop
filenum==1 && index($0, FL) { # look at only 1 folder and index
match($0, "_0*([0-9]+)/") # match substring _user in each folder name
FNUM=substr($0,RSTART+1,RLENGTH-2) # extract contents and store as $FNUM --- user_S5-0271-96-v5.6 --- is an example
}filenum==2 && $0 ~ FNUM".pdf$"') # print $FNUM for pdf
break # end loop
done # end processing
echo "The matching pdf is" $pdf # confirm pdf match
# copy pdf to folder
if [[ "$pdf" = "$folder" ]] # only execute file--->folder value match
then # perform action on match
cp $dir/*$pdf.pdf $dir/*$folder # copy pdf to matching folder
fi # end
done # processing complete
set -xv output
Code:
cmccabe@Satellite-M645:~$ set -xv
cmccabe@Satellite-M645:~$ dir=/home/cmccabe/folder
dir=/home/cmccabe/folder
+ dir=/home/cmccabe/folder
cmccabe@Satellite-M645:~$ # Find oldest directory
# Find oldest directory
cmccabe@Satellite-M645:~$ find "$dir" -maxdepth 1 -mindepth 1 -type d -printf '%T+\t%P\0' | sort -z |
find "$dir" -maxdepth 1 -mindepth 1 -type d -printf '%T+\t%P\0' | sort -z |
> while read -r -d $'\t' time && read -r -d '' filename
while read -r -d $'\t' time && read -r -d '' filename
> do
do
> printf "The oldest folder is $filename, created on $time\n"
printf "The oldest folder is $filename, created on $time\n"
> folder="$(echo $filename|cut -d'_' -f8-)" # split on _ and print
folder="$(echo $filename|cut -d'_' -f8-)" # split on _ and print
> echo "The folder is" "$folder"
echo "The folder is" "$folder"
> # Find matching pdf
# Find matching pdf
> pdf=$(awk -v FL="$filename" '
pdf=$(awk -v FL="$filename" '
> FNR == 1 {filenum++}
FNR == 1 {filenum++}
> filenum==1 && index($0, FL) {
filenum==1 && index($0, FL) {
> match($0, "_0*([0-9]+)/")
match($0, "_0*([0-9]+)/")
> FNUM=substr($0,RSTART+1,RLENGTH-2)
FNUM=substr($0,RSTART+1,RLENGTH-2)
> gsub(/^0+/,"", FNUM)
gsub(/^0+/,"", FNUM)
> }filenum==2 && $0 ~ FNUM".pdf$"')
}filenum==2 && $0 ~ FNUM".pdf$"')
> break
break
> done
done
+ sort -z
+ find /home/cmccabe/folder -maxdepth 1 -mindepth 1 -type d -printf '%T+\t%P\0'
+ read -r -d ' ' time
+ read -r -d '' filename
+ printf 'The oldest folder is R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions, created on 2019-03-01+11:32:47.3690364740\n'
The oldest folder is R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions, created on 2019-03-01+11:32:47.3690364740
echo $filename|cut -d'_' -f8-
++ echo R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
++ cut -d_ -f8-
+ folder=user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
+ echo 'The folder is' user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
The folder is user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
awk -v FL="$filename" '
FNR == 1 {filenum++}
filenum==1 && index($0, FL) {
match($0, "_0*([0-9]+)/")
FNUM=substr($0,RSTART+1,RLENGTH-2)
gsub(/^0+/,"", FNUM)
}filenum==2 && $0 ~ FNUM".pdf$"'
++ awk -v FL=R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions '
FNR == 1 {filenum++}
filenum==1 && index($0, FL) {
match($0, "_0*([0-9]+)/")
FNUM=substr($0,RSTART+1,RLENGTH-2)
gsub(/^0+/,"", FNUM)
}filenum==2 && $0 ~ FNUM".pdf$"'
+ pdf=
+ break
cmccabe@Satellite-M645:~$ echo "The matching pdf is" $pdf
echo "The matching pdf is" $pdf
+ echo 'The matching pdf is'
The matching pdf is
Last edited by cmccabe; 03-03-2019 at 08:42 PM..
Reason: updated script with comments
Hi,
I have a requirement to move a file from one folder(a) to another folder(b) only when folder (b) have a write permission. Folder permission is 755
If the permission is otherthan 755 we need to come out of the loop
I will appreciate your help
Thanks
Soll (1 Reply)
This is the file structure:
DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg
DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg
DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg
DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg
DESKTOP/Root of Photo... (4 Replies)
Hello guys
I am sure that you will help me on this issue as you did earlier::)
Scenario :
I have a folder named "XYZ". It consist many sub-folders and subfolder contain severals files. there may be abc.dat in each subfolder. Now i want to seperate subfolders on follwing conditions-
if abc.dat... (12 Replies)
I Need help for one requirement,
I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix.
Example:
Source Directory : \a
destination Directory : \a\b
File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt)
File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Dear Experts,
I need to connect to a FTPS Server and move the files from FTPS folder "/SAP/Out" to Unix directory "/SAP/In". I need to run this script on Unix directory...Script should get the files from FTPS folder and place that in specified Unix Directory.
Thanks In Advance. (1 Reply)
Hi all,
let me explain my requirments
i am having 5 folder with different name for eg) abc , cdf , efd, rtg, ead
each 5 folders contain 15 files
i want to move 10 files to some other folder, remain 5 files should be there in the same folder.
give me some suggestion on this. (6 Replies)
Hi All,
I have directory and it has multiple sub directories and all these sub directories contains many files. i want to move all these files to one different directory. But after moving files i should be able to recognize which file belongs to which directory.
Is there any way to achieve... (6 Replies)
I have a script, which is checking if file exists and move it to another directory
if
then
mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly
mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly
... (9 Replies)
Hi Team,
I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder.
Eg:
parent folder --> /Interface/data/test/IN
Sub folder -->/Interface/data/test/IN/Invoice20180607233338
Subfolder will be always with timestamp... (6 Replies)