An attempt at copying specific files to specific folders
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This script copies all the files to all the folders. But I am struggling to construct a condition statement that will specify to which folder each file is copied to. I've tried the if statement but no copy is made at all.
The reason I use parameter expansion is because the file names are like, Long_John_Silver, Miss_Havisham and Master_Pip and the folder names are like Long, Miss and Master. So essentially I'm trying to copy the files to their respective folders e.g. Master_Pip.fna.gz into the folder named Master. And so what I've tried to do is to capture the first word of the file name and somehow use that as a reference.
for fldr in /home/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
This is my script with the if statement, but this script copies nothing at all.
for fldr in /home/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
if [ "$f_name" == /home/scripts/playground/port/"$f_name" ] ; then
cp -r $f /home/scripts/playground/port/"$f_name"/ ;
else
continue
fi
done
done
linux bash
add a comment |Â
up vote
0
down vote
favorite
This script copies all the files to all the folders. But I am struggling to construct a condition statement that will specify to which folder each file is copied to. I've tried the if statement but no copy is made at all.
The reason I use parameter expansion is because the file names are like, Long_John_Silver, Miss_Havisham and Master_Pip and the folder names are like Long, Miss and Master. So essentially I'm trying to copy the files to their respective folders e.g. Master_Pip.fna.gz into the folder named Master. And so what I've tried to do is to capture the first word of the file name and somehow use that as a reference.
for fldr in /home/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
This is my script with the if statement, but this script copies nothing at all.
for fldr in /home/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
if [ "$f_name" == /home/scripts/playground/port/"$f_name" ] ; then
cp -r $f /home/scripts/playground/port/"$f_name"/ ;
else
continue
fi
done
done
linux bash
how can"$f_name"
could possibly equal to itself + another string ??? You might wanna test if the file already exists it the specific folder, fi so, useif -f $myPathAndFile
â Carpette
Mar 6 at 12:53
"f_name"
is reference point. It extracts from the file name like so - "James_Bond" into "James". I am doing this because all my files have multiple underscores but the beginning of the file is the name of a folder in a distant location i.e there is a folder called "James" somewhere. And there are 100s of these which is why I'm trying to automate this.
â Mr Keystrokes
Mar 6 at 13:32
I understand this but ...if [ "$f_name" == /home/scripts/playground/port/"$f_name" ]
... i don't get how this can possibly work. It's like sayingx = 10 + x
â Carpette
Mar 6 at 13:42
Yeah I agree it was a crucial mistake.
â Mr Keystrokes
Mar 6 at 15:41
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This script copies all the files to all the folders. But I am struggling to construct a condition statement that will specify to which folder each file is copied to. I've tried the if statement but no copy is made at all.
The reason I use parameter expansion is because the file names are like, Long_John_Silver, Miss_Havisham and Master_Pip and the folder names are like Long, Miss and Master. So essentially I'm trying to copy the files to their respective folders e.g. Master_Pip.fna.gz into the folder named Master. And so what I've tried to do is to capture the first word of the file name and somehow use that as a reference.
for fldr in /home/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
This is my script with the if statement, but this script copies nothing at all.
for fldr in /home/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
if [ "$f_name" == /home/scripts/playground/port/"$f_name" ] ; then
cp -r $f /home/scripts/playground/port/"$f_name"/ ;
else
continue
fi
done
done
linux bash
This script copies all the files to all the folders. But I am struggling to construct a condition statement that will specify to which folder each file is copied to. I've tried the if statement but no copy is made at all.
The reason I use parameter expansion is because the file names are like, Long_John_Silver, Miss_Havisham and Master_Pip and the folder names are like Long, Miss and Master. So essentially I'm trying to copy the files to their respective folders e.g. Master_Pip.fna.gz into the folder named Master. And so what I've tried to do is to capture the first word of the file name and somehow use that as a reference.
for fldr in /home/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
This is my script with the if statement, but this script copies nothing at all.
for fldr in /home/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
if [ "$f_name" == /home/scripts/playground/port/"$f_name" ] ; then
cp -r $f /home/scripts/playground/port/"$f_name"/ ;
else
continue
fi
done
done
linux bash
edited Mar 15 at 21:44
sourcejedi
18.9k32477
18.9k32477
asked Mar 6 at 12:08
Mr Keystrokes
11
11
how can"$f_name"
could possibly equal to itself + another string ??? You might wanna test if the file already exists it the specific folder, fi so, useif -f $myPathAndFile
â Carpette
Mar 6 at 12:53
"f_name"
is reference point. It extracts from the file name like so - "James_Bond" into "James". I am doing this because all my files have multiple underscores but the beginning of the file is the name of a folder in a distant location i.e there is a folder called "James" somewhere. And there are 100s of these which is why I'm trying to automate this.
â Mr Keystrokes
Mar 6 at 13:32
I understand this but ...if [ "$f_name" == /home/scripts/playground/port/"$f_name" ]
... i don't get how this can possibly work. It's like sayingx = 10 + x
â Carpette
Mar 6 at 13:42
Yeah I agree it was a crucial mistake.
â Mr Keystrokes
Mar 6 at 15:41
add a comment |Â
how can"$f_name"
could possibly equal to itself + another string ??? You might wanna test if the file already exists it the specific folder, fi so, useif -f $myPathAndFile
â Carpette
Mar 6 at 12:53
"f_name"
is reference point. It extracts from the file name like so - "James_Bond" into "James". I am doing this because all my files have multiple underscores but the beginning of the file is the name of a folder in a distant location i.e there is a folder called "James" somewhere. And there are 100s of these which is why I'm trying to automate this.
â Mr Keystrokes
Mar 6 at 13:32
I understand this but ...if [ "$f_name" == /home/scripts/playground/port/"$f_name" ]
... i don't get how this can possibly work. It's like sayingx = 10 + x
â Carpette
Mar 6 at 13:42
Yeah I agree it was a crucial mistake.
â Mr Keystrokes
Mar 6 at 15:41
how can
"$f_name"
could possibly equal to itself + another string ??? You might wanna test if the file already exists it the specific folder, fi so, use if -f $myPathAndFile
â Carpette
Mar 6 at 12:53
how can
"$f_name"
could possibly equal to itself + another string ??? You might wanna test if the file already exists it the specific folder, fi so, use if -f $myPathAndFile
â Carpette
Mar 6 at 12:53
"f_name"
is reference point. It extracts from the file name like so - "James_Bond" into "James". I am doing this because all my files have multiple underscores but the beginning of the file is the name of a folder in a distant location i.e there is a folder called "James" somewhere. And there are 100s of these which is why I'm trying to automate this.â Mr Keystrokes
Mar 6 at 13:32
"f_name"
is reference point. It extracts from the file name like so - "James_Bond" into "James". I am doing this because all my files have multiple underscores but the beginning of the file is the name of a folder in a distant location i.e there is a folder called "James" somewhere. And there are 100s of these which is why I'm trying to automate this.â Mr Keystrokes
Mar 6 at 13:32
I understand this but ...
if [ "$f_name" == /home/scripts/playground/port/"$f_name" ]
... i don't get how this can possibly work. It's like saying x = 10 + x
â Carpette
Mar 6 at 13:42
I understand this but ...
if [ "$f_name" == /home/scripts/playground/port/"$f_name" ]
... i don't get how this can possibly work. It's like saying x = 10 + x
â Carpette
Mar 6 at 13:42
Yeah I agree it was a crucial mistake.
â Mr Keystrokes
Mar 6 at 15:41
Yeah I agree it was a crucial mistake.
â Mr Keystrokes
Mar 6 at 15:41
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
1.
Change this code
basenm=$fldr##*/;
to this one
basenm=$f##*/ ;
and remove -r switch from copy command
cp $f /home/scripts/playground/port/"$f_name"
Maybe you have some jumbled source paths like this /home/scripts/playground/genomes_2/Long_John_Silver/Master_Pip.fna.gz. So, when you're extracting f_name from folder name fldr
you get 'Long'. Then you copy Master_Pip.fna.gz to the ..../Long folder.
2.
"$f_name" == /home/scripts/playground/port/"$f_name"
they are not equal, and they won't.
Basically, your first script is good. Condition is redundant because you do it implicitly by parameter expansion.
I would only add directory creation command before copying. So you'll always have a directory to copy to in case new person appears in source directory, Shorty_Pete for example.
mkdir -p /home/scripts/playground/port/"$f_name"
cp ...
I have already created folders which don't exist in a previous step of the same script, I've only posted this part of the script. Even still, if I remove the if statement, which I have tested, it copies all files into all folders and so the implicit specificity by parameter expansion actually doesn't seem to work.
â Mr Keystrokes
Mar 6 at 13:22
OK. What are the names of subdirectories in the /home/scripts/playground/genomes_2 ? The only discrepancy I see that you extract f_name not from filename$f
but from$fldr
name.
â Alexey Dolgopolov
Mar 6 at 14:05
Yeah that's a significant point actually, that I took into account in my solution.
â Mr Keystrokes
Mar 6 at 15:42
add a comment |Â
up vote
0
down vote
accepted
I realised that I was copying all the files into all the folders based on the type of file i.e. .fna.gz, so I specified what type of fna.gz file I want to be read and then copied. There is no need for an if statement since the specificity is implied through the parameter expansion. It now works perfectly.
for fldr in /home/scripts/playground/genomes_2/* ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
find . -name $f_name*fna.gz | while read f ; do
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
1.
Change this code
basenm=$fldr##*/;
to this one
basenm=$f##*/ ;
and remove -r switch from copy command
cp $f /home/scripts/playground/port/"$f_name"
Maybe you have some jumbled source paths like this /home/scripts/playground/genomes_2/Long_John_Silver/Master_Pip.fna.gz. So, when you're extracting f_name from folder name fldr
you get 'Long'. Then you copy Master_Pip.fna.gz to the ..../Long folder.
2.
"$f_name" == /home/scripts/playground/port/"$f_name"
they are not equal, and they won't.
Basically, your first script is good. Condition is redundant because you do it implicitly by parameter expansion.
I would only add directory creation command before copying. So you'll always have a directory to copy to in case new person appears in source directory, Shorty_Pete for example.
mkdir -p /home/scripts/playground/port/"$f_name"
cp ...
I have already created folders which don't exist in a previous step of the same script, I've only posted this part of the script. Even still, if I remove the if statement, which I have tested, it copies all files into all folders and so the implicit specificity by parameter expansion actually doesn't seem to work.
â Mr Keystrokes
Mar 6 at 13:22
OK. What are the names of subdirectories in the /home/scripts/playground/genomes_2 ? The only discrepancy I see that you extract f_name not from filename$f
but from$fldr
name.
â Alexey Dolgopolov
Mar 6 at 14:05
Yeah that's a significant point actually, that I took into account in my solution.
â Mr Keystrokes
Mar 6 at 15:42
add a comment |Â
up vote
1
down vote
1.
Change this code
basenm=$fldr##*/;
to this one
basenm=$f##*/ ;
and remove -r switch from copy command
cp $f /home/scripts/playground/port/"$f_name"
Maybe you have some jumbled source paths like this /home/scripts/playground/genomes_2/Long_John_Silver/Master_Pip.fna.gz. So, when you're extracting f_name from folder name fldr
you get 'Long'. Then you copy Master_Pip.fna.gz to the ..../Long folder.
2.
"$f_name" == /home/scripts/playground/port/"$f_name"
they are not equal, and they won't.
Basically, your first script is good. Condition is redundant because you do it implicitly by parameter expansion.
I would only add directory creation command before copying. So you'll always have a directory to copy to in case new person appears in source directory, Shorty_Pete for example.
mkdir -p /home/scripts/playground/port/"$f_name"
cp ...
I have already created folders which don't exist in a previous step of the same script, I've only posted this part of the script. Even still, if I remove the if statement, which I have tested, it copies all files into all folders and so the implicit specificity by parameter expansion actually doesn't seem to work.
â Mr Keystrokes
Mar 6 at 13:22
OK. What are the names of subdirectories in the /home/scripts/playground/genomes_2 ? The only discrepancy I see that you extract f_name not from filename$f
but from$fldr
name.
â Alexey Dolgopolov
Mar 6 at 14:05
Yeah that's a significant point actually, that I took into account in my solution.
â Mr Keystrokes
Mar 6 at 15:42
add a comment |Â
up vote
1
down vote
up vote
1
down vote
1.
Change this code
basenm=$fldr##*/;
to this one
basenm=$f##*/ ;
and remove -r switch from copy command
cp $f /home/scripts/playground/port/"$f_name"
Maybe you have some jumbled source paths like this /home/scripts/playground/genomes_2/Long_John_Silver/Master_Pip.fna.gz. So, when you're extracting f_name from folder name fldr
you get 'Long'. Then you copy Master_Pip.fna.gz to the ..../Long folder.
2.
"$f_name" == /home/scripts/playground/port/"$f_name"
they are not equal, and they won't.
Basically, your first script is good. Condition is redundant because you do it implicitly by parameter expansion.
I would only add directory creation command before copying. So you'll always have a directory to copy to in case new person appears in source directory, Shorty_Pete for example.
mkdir -p /home/scripts/playground/port/"$f_name"
cp ...
1.
Change this code
basenm=$fldr##*/;
to this one
basenm=$f##*/ ;
and remove -r switch from copy command
cp $f /home/scripts/playground/port/"$f_name"
Maybe you have some jumbled source paths like this /home/scripts/playground/genomes_2/Long_John_Silver/Master_Pip.fna.gz. So, when you're extracting f_name from folder name fldr
you get 'Long'. Then you copy Master_Pip.fna.gz to the ..../Long folder.
2.
"$f_name" == /home/scripts/playground/port/"$f_name"
they are not equal, and they won't.
Basically, your first script is good. Condition is redundant because you do it implicitly by parameter expansion.
I would only add directory creation command before copying. So you'll always have a directory to copy to in case new person appears in source directory, Shorty_Pete for example.
mkdir -p /home/scripts/playground/port/"$f_name"
cp ...
edited Mar 6 at 14:29
answered Mar 6 at 12:23
Alexey Dolgopolov
1113
1113
I have already created folders which don't exist in a previous step of the same script, I've only posted this part of the script. Even still, if I remove the if statement, which I have tested, it copies all files into all folders and so the implicit specificity by parameter expansion actually doesn't seem to work.
â Mr Keystrokes
Mar 6 at 13:22
OK. What are the names of subdirectories in the /home/scripts/playground/genomes_2 ? The only discrepancy I see that you extract f_name not from filename$f
but from$fldr
name.
â Alexey Dolgopolov
Mar 6 at 14:05
Yeah that's a significant point actually, that I took into account in my solution.
â Mr Keystrokes
Mar 6 at 15:42
add a comment |Â
I have already created folders which don't exist in a previous step of the same script, I've only posted this part of the script. Even still, if I remove the if statement, which I have tested, it copies all files into all folders and so the implicit specificity by parameter expansion actually doesn't seem to work.
â Mr Keystrokes
Mar 6 at 13:22
OK. What are the names of subdirectories in the /home/scripts/playground/genomes_2 ? The only discrepancy I see that you extract f_name not from filename$f
but from$fldr
name.
â Alexey Dolgopolov
Mar 6 at 14:05
Yeah that's a significant point actually, that I took into account in my solution.
â Mr Keystrokes
Mar 6 at 15:42
I have already created folders which don't exist in a previous step of the same script, I've only posted this part of the script. Even still, if I remove the if statement, which I have tested, it copies all files into all folders and so the implicit specificity by parameter expansion actually doesn't seem to work.
â Mr Keystrokes
Mar 6 at 13:22
I have already created folders which don't exist in a previous step of the same script, I've only posted this part of the script. Even still, if I remove the if statement, which I have tested, it copies all files into all folders and so the implicit specificity by parameter expansion actually doesn't seem to work.
â Mr Keystrokes
Mar 6 at 13:22
OK. What are the names of subdirectories in the /home/scripts/playground/genomes_2 ? The only discrepancy I see that you extract f_name not from filename
$f
but from $fldr
name.â Alexey Dolgopolov
Mar 6 at 14:05
OK. What are the names of subdirectories in the /home/scripts/playground/genomes_2 ? The only discrepancy I see that you extract f_name not from filename
$f
but from $fldr
name.â Alexey Dolgopolov
Mar 6 at 14:05
Yeah that's a significant point actually, that I took into account in my solution.
â Mr Keystrokes
Mar 6 at 15:42
Yeah that's a significant point actually, that I took into account in my solution.
â Mr Keystrokes
Mar 6 at 15:42
add a comment |Â
up vote
0
down vote
accepted
I realised that I was copying all the files into all the folders based on the type of file i.e. .fna.gz, so I specified what type of fna.gz file I want to be read and then copied. There is no need for an if statement since the specificity is implied through the parameter expansion. It now works perfectly.
for fldr in /home/scripts/playground/genomes_2/* ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
find . -name $f_name*fna.gz | while read f ; do
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
add a comment |Â
up vote
0
down vote
accepted
I realised that I was copying all the files into all the folders based on the type of file i.e. .fna.gz, so I specified what type of fna.gz file I want to be read and then copied. There is no need for an if statement since the specificity is implied through the parameter expansion. It now works perfectly.
for fldr in /home/scripts/playground/genomes_2/* ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
find . -name $f_name*fna.gz | while read f ; do
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I realised that I was copying all the files into all the folders based on the type of file i.e. .fna.gz, so I specified what type of fna.gz file I want to be read and then copied. There is no need for an if statement since the specificity is implied through the parameter expansion. It now works perfectly.
for fldr in /home/scripts/playground/genomes_2/* ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
find . -name $f_name*fna.gz | while read f ; do
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
I realised that I was copying all the files into all the folders based on the type of file i.e. .fna.gz, so I specified what type of fna.gz file I want to be read and then copied. There is no need for an if statement since the specificity is implied through the parameter expansion. It now works perfectly.
for fldr in /home/scripts/playground/genomes_2/* ; do
basenm=$fldr##*/ ; f_name=$basenm%%_* ;
find . -name $f_name*fna.gz | while read f ; do
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
answered Mar 6 at 15:35
Mr Keystrokes
11
11
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428492%2fan-attempt-at-copying-specific-files-to-specific-folders%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
how can
"$f_name"
could possibly equal to itself + another string ??? You might wanna test if the file already exists it the specific folder, fi so, useif -f $myPathAndFile
â Carpette
Mar 6 at 12:53
"f_name"
is reference point. It extracts from the file name like so - "James_Bond" into "James". I am doing this because all my files have multiple underscores but the beginning of the file is the name of a folder in a distant location i.e there is a folder called "James" somewhere. And there are 100s of these which is why I'm trying to automate this.â Mr Keystrokes
Mar 6 at 13:32
I understand this but ...
if [ "$f_name" == /home/scripts/playground/port/"$f_name" ]
... i don't get how this can possibly work. It's like sayingx = 10 + x
â Carpette
Mar 6 at 13:42
Yeah I agree it was a crucial mistake.
â Mr Keystrokes
Mar 6 at 15:41