BashScript move files based on matching part of filenames from a list
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have millions of xml files. The name of the xml file follows this pattern:
ABC_20180912_12345.xml
ABC_20180412_98765.xml
ABC_20180412_45678.xml
From this I want to copy files to a different folder based on the name it has after the underscore. To identify the files, I have a list which I have saved in a csv file which provides me with the required names. An example:
vcfile="/home/mycomp/Documents/wd/vehicles.csv"
vcpvr=`cat $vcfile`
echo $vcpvr provides me with this list:
2894 4249 5464
I am able to loop through the xmlfiles in the folder, open each file and grep to see if the file contains the string and if it is, the move the files to a new location. This is working.
The complete code:
#filesToExtract is the interim folder
fold="/home/mycomp/filesToExtract";
query=$fold/*.xml
vcfile="/home/mycomp/Documents/wd/vehicles.csv"
vcpvr=`cat $vcfile`
#xmlfiles - keep all tar.gz files here
cd ~/xmlfiles/
COUNTER=1
for f in *.tar.gz
do
echo " $COUNTER "
tar zxf "$f" -C ~/filesToExtract
for k in $query
do
file $k | if grep -q "$vcpvr"
then
mv $k ~/xmlToWork/
fi
done
#xmltowork is the final folder
#rm -r ~/filesToExtract/*.xml
COUNTER=$((COUNTER + 1))
done
But since this looks for the string inside the file, instead of filename, it takes longer to process millions of files. Instead, I want to look for the string in the filename and if it is there, move the files. This is what I have tried:
target="/home/mycomp/xmlToWork"
for k in $query
do
if [[ $k =~ "$vcpvr" ]]; then
cp -v $k $target
fi
done
But this gives me an error tarextract.sh: 12: tarextract.sh: [[: not found
bash filenames
add a comment |Â
up vote
0
down vote
favorite
I have millions of xml files. The name of the xml file follows this pattern:
ABC_20180912_12345.xml
ABC_20180412_98765.xml
ABC_20180412_45678.xml
From this I want to copy files to a different folder based on the name it has after the underscore. To identify the files, I have a list which I have saved in a csv file which provides me with the required names. An example:
vcfile="/home/mycomp/Documents/wd/vehicles.csv"
vcpvr=`cat $vcfile`
echo $vcpvr provides me with this list:
2894 4249 5464
I am able to loop through the xmlfiles in the folder, open each file and grep to see if the file contains the string and if it is, the move the files to a new location. This is working.
The complete code:
#filesToExtract is the interim folder
fold="/home/mycomp/filesToExtract";
query=$fold/*.xml
vcfile="/home/mycomp/Documents/wd/vehicles.csv"
vcpvr=`cat $vcfile`
#xmlfiles - keep all tar.gz files here
cd ~/xmlfiles/
COUNTER=1
for f in *.tar.gz
do
echo " $COUNTER "
tar zxf "$f" -C ~/filesToExtract
for k in $query
do
file $k | if grep -q "$vcpvr"
then
mv $k ~/xmlToWork/
fi
done
#xmltowork is the final folder
#rm -r ~/filesToExtract/*.xml
COUNTER=$((COUNTER + 1))
done
But since this looks for the string inside the file, instead of filename, it takes longer to process millions of files. Instead, I want to look for the string in the filename and if it is there, move the files. This is what I have tried:
target="/home/mycomp/xmlToWork"
for k in $query
do
if [[ $k =~ "$vcpvr" ]]; then
cp -v $k $target
fi
done
But this gives me an error tarextract.sh: 12: tarextract.sh: [[: not found
bash filenames
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have millions of xml files. The name of the xml file follows this pattern:
ABC_20180912_12345.xml
ABC_20180412_98765.xml
ABC_20180412_45678.xml
From this I want to copy files to a different folder based on the name it has after the underscore. To identify the files, I have a list which I have saved in a csv file which provides me with the required names. An example:
vcfile="/home/mycomp/Documents/wd/vehicles.csv"
vcpvr=`cat $vcfile`
echo $vcpvr provides me with this list:
2894 4249 5464
I am able to loop through the xmlfiles in the folder, open each file and grep to see if the file contains the string and if it is, the move the files to a new location. This is working.
The complete code:
#filesToExtract is the interim folder
fold="/home/mycomp/filesToExtract";
query=$fold/*.xml
vcfile="/home/mycomp/Documents/wd/vehicles.csv"
vcpvr=`cat $vcfile`
#xmlfiles - keep all tar.gz files here
cd ~/xmlfiles/
COUNTER=1
for f in *.tar.gz
do
echo " $COUNTER "
tar zxf "$f" -C ~/filesToExtract
for k in $query
do
file $k | if grep -q "$vcpvr"
then
mv $k ~/xmlToWork/
fi
done
#xmltowork is the final folder
#rm -r ~/filesToExtract/*.xml
COUNTER=$((COUNTER + 1))
done
But since this looks for the string inside the file, instead of filename, it takes longer to process millions of files. Instead, I want to look for the string in the filename and if it is there, move the files. This is what I have tried:
target="/home/mycomp/xmlToWork"
for k in $query
do
if [[ $k =~ "$vcpvr" ]]; then
cp -v $k $target
fi
done
But this gives me an error tarextract.sh: 12: tarextract.sh: [[: not found
bash filenames
I have millions of xml files. The name of the xml file follows this pattern:
ABC_20180912_12345.xml
ABC_20180412_98765.xml
ABC_20180412_45678.xml
From this I want to copy files to a different folder based on the name it has after the underscore. To identify the files, I have a list which I have saved in a csv file which provides me with the required names. An example:
vcfile="/home/mycomp/Documents/wd/vehicles.csv"
vcpvr=`cat $vcfile`
echo $vcpvr provides me with this list:
2894 4249 5464
I am able to loop through the xmlfiles in the folder, open each file and grep to see if the file contains the string and if it is, the move the files to a new location. This is working.
The complete code:
#filesToExtract is the interim folder
fold="/home/mycomp/filesToExtract";
query=$fold/*.xml
vcfile="/home/mycomp/Documents/wd/vehicles.csv"
vcpvr=`cat $vcfile`
#xmlfiles - keep all tar.gz files here
cd ~/xmlfiles/
COUNTER=1
for f in *.tar.gz
do
echo " $COUNTER "
tar zxf "$f" -C ~/filesToExtract
for k in $query
do
file $k | if grep -q "$vcpvr"
then
mv $k ~/xmlToWork/
fi
done
#xmltowork is the final folder
#rm -r ~/filesToExtract/*.xml
COUNTER=$((COUNTER + 1))
done
But since this looks for the string inside the file, instead of filename, it takes longer to process millions of files. Instead, I want to look for the string in the filename and if it is there, move the files. This is what I have tried:
target="/home/mycomp/xmlToWork"
for k in $query
do
if [[ $k =~ "$vcpvr" ]]; then
cp -v $k $target
fi
done
But this gives me an error tarextract.sh: 12: tarextract.sh: [[: not found
bash filenames
bash filenames
asked Aug 21 at 7:35
Apricot
22818
22818
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
$cvfile is also a list, isn't it? So I would do:
for k in "$query"
do
for l in "$cvfile"
do
if [[ "$k" =~ "$l" ]]
then
cp -v "$k" "$target"
fi
done
done
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
$cvfile is also a list, isn't it? So I would do:
for k in "$query"
do
for l in "$cvfile"
do
if [[ "$k" =~ "$l" ]]
then
cp -v "$k" "$target"
fi
done
done
add a comment |Â
up vote
0
down vote
$cvfile is also a list, isn't it? So I would do:
for k in "$query"
do
for l in "$cvfile"
do
if [[ "$k" =~ "$l" ]]
then
cp -v "$k" "$target"
fi
done
done
add a comment |Â
up vote
0
down vote
up vote
0
down vote
$cvfile is also a list, isn't it? So I would do:
for k in "$query"
do
for l in "$cvfile"
do
if [[ "$k" =~ "$l" ]]
then
cp -v "$k" "$target"
fi
done
done
$cvfile is also a list, isn't it? So I would do:
for k in "$query"
do
for l in "$cvfile"
do
if [[ "$k" =~ "$l" ]]
then
cp -v "$k" "$target"
fi
done
done
answered Aug 21 at 9:29
spacelander
1485
1485
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%2f463793%2fbashscript-move-files-based-on-matching-part-of-filenames-from-a-list%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