Moving millions of files to a different directory with specfic name patternsNeed to copy files to existing directory and remove files already there with the same name but different extensionfind file, copy but with different nameIs there a better way than cp to copy millions of files while preserving hard links?Comparing files with different extensions but possibly similar contentsCopy files from a directory if name present in a text fileHow to copy multiple files with a same name from children directory to another directory without losing the parent directory?Copy directory structure with random number of filesMoving specific files to two different locationsMoving millions of small files results in “out of space” errorCopying files based on partial names in a file

Nested-Loop-Join: How many comparisons and how many pages-accesses?

Variation in the spelling of word-final M

Why does the Earth have a z-component at the start of the J2000 epoch?

Is this a plot hole in the Lost Mine of Phandelver adventure?

Is it rude to tell recruiters I would only change jobs for a better salary?

Relationship between GCD, LCM and the Riemann Zeta function

Absconding a company after 1st day of joining

How did John Lennon tune his guitar

How would someone destroy a black hole that’s at the centre of a planet?

How to fit a linear model in the Bayesian way in Mathematica?

Can I activate an iPhone without an Apple ID?

I quit, and boss offered me 3 month "grace period" where I could still come back

3D-Plot with an inequality condition for parameter values

Can a continent naturally split into two distant parts within a week?

Alternatives to using writing paper for writing practice

Did the Shuttle's rudder or elevons operate when flown on its carrier 747?

Possible isometry groups of open manifolds

Is `curl something | sudo bash -` a reasonably safe installation method?

Is a public company able to check out who owns its shares in very detailed format?

Why linear regression uses "vertical" distance to the best-fit-line, instead of actual distance?

What exactly is the Tension force?

What are some symbols representing peasants/oppressed persons fighting back?

how to generate correct single and double quotes in tex

How can I legally visit the United States Minor Outlying Islands in the Pacific?



Moving millions of files to a different directory with specfic name patterns


Need to copy files to existing directory and remove files already there with the same name but different extensionfind file, copy but with different nameIs there a better way than cp to copy millions of files while preserving hard links?Comparing files with different extensions but possibly similar contentsCopy files from a directory if name present in a text fileHow to copy multiple files with a same name from children directory to another directory without losing the parent directory?Copy directory structure with random number of filesMoving specific files to two different locationsMoving millions of small files results in “out of space” errorCopying files based on partial names in a file






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








10















I have millions of files with the following nomenclature on a Linux machine:



1559704165_a1ac6f55fef555ee.jpg


The first 10 digits are timestamp and the ones followed by a _ are specific ids. I want to move all the files matching specific filename ids to a different folder.



I tried this on the directory with files



find . -maxdepth 1 -type f | ??????????_a1ac*.jpg |xargs mv -t "/home/ubuntu/ntest"


However I am getting an error indicating:



bash 1559704165_a1ac6f55fef555ee.jpg: command not found


When I tried, mv ??????????_a1ac*.jpg I am getting argument list too long error. I have atleast 15 different filename patterns. How do I move them.










share|improve this question



















  • 1





    The bash says it all: it tries to execute that filename as it is the first on the line in the 2nd stage of the pipe (your 2nd stage pipe is: | ??????????_a1ac*.jpg: bash expands it to several filename, the first being 1559704165_a1ac6f55fef555ee.jpg, si you end up, in that 2nd pipe stage, trying to execute: 1559704165_a1ac6f55fef555ee.jpg next_matching_filename 3rd_matching_filename ... nth_matching_filename. I guess you tried instead to filter to that filename (see answers below for that)

    – Olivier Dulac
    Jul 8 at 13:08


















10















I have millions of files with the following nomenclature on a Linux machine:



1559704165_a1ac6f55fef555ee.jpg


The first 10 digits are timestamp and the ones followed by a _ are specific ids. I want to move all the files matching specific filename ids to a different folder.



I tried this on the directory with files



find . -maxdepth 1 -type f | ??????????_a1ac*.jpg |xargs mv -t "/home/ubuntu/ntest"


However I am getting an error indicating:



bash 1559704165_a1ac6f55fef555ee.jpg: command not found


When I tried, mv ??????????_a1ac*.jpg I am getting argument list too long error. I have atleast 15 different filename patterns. How do I move them.










share|improve this question



















  • 1





    The bash says it all: it tries to execute that filename as it is the first on the line in the 2nd stage of the pipe (your 2nd stage pipe is: | ??????????_a1ac*.jpg: bash expands it to several filename, the first being 1559704165_a1ac6f55fef555ee.jpg, si you end up, in that 2nd pipe stage, trying to execute: 1559704165_a1ac6f55fef555ee.jpg next_matching_filename 3rd_matching_filename ... nth_matching_filename. I guess you tried instead to filter to that filename (see answers below for that)

    – Olivier Dulac
    Jul 8 at 13:08














10












10








10


3






I have millions of files with the following nomenclature on a Linux machine:



1559704165_a1ac6f55fef555ee.jpg


The first 10 digits are timestamp and the ones followed by a _ are specific ids. I want to move all the files matching specific filename ids to a different folder.



I tried this on the directory with files



find . -maxdepth 1 -type f | ??????????_a1ac*.jpg |xargs mv -t "/home/ubuntu/ntest"


However I am getting an error indicating:



bash 1559704165_a1ac6f55fef555ee.jpg: command not found


When I tried, mv ??????????_a1ac*.jpg I am getting argument list too long error. I have atleast 15 different filename patterns. How do I move them.










share|improve this question
















I have millions of files with the following nomenclature on a Linux machine:



1559704165_a1ac6f55fef555ee.jpg


The first 10 digits are timestamp and the ones followed by a _ are specific ids. I want to move all the files matching specific filename ids to a different folder.



I tried this on the directory with files



find . -maxdepth 1 -type f | ??????????_a1ac*.jpg |xargs mv -t "/home/ubuntu/ntest"


However I am getting an error indicating:



bash 1559704165_a1ac6f55fef555ee.jpg: command not found


When I tried, mv ??????????_a1ac*.jpg I am getting argument list too long error. I have atleast 15 different filename patterns. How do I move them.







file-copy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 6 at 13:56









terdon

138k33 gold badges285 silver badges462 bronze badges




138k33 gold badges285 silver badges462 bronze badges










asked Jul 6 at 13:07









ApricotApricot

2873 silver badges13 bronze badges




2873 silver badges13 bronze badges







  • 1





    The bash says it all: it tries to execute that filename as it is the first on the line in the 2nd stage of the pipe (your 2nd stage pipe is: | ??????????_a1ac*.jpg: bash expands it to several filename, the first being 1559704165_a1ac6f55fef555ee.jpg, si you end up, in that 2nd pipe stage, trying to execute: 1559704165_a1ac6f55fef555ee.jpg next_matching_filename 3rd_matching_filename ... nth_matching_filename. I guess you tried instead to filter to that filename (see answers below for that)

    – Olivier Dulac
    Jul 8 at 13:08













  • 1





    The bash says it all: it tries to execute that filename as it is the first on the line in the 2nd stage of the pipe (your 2nd stage pipe is: | ??????????_a1ac*.jpg: bash expands it to several filename, the first being 1559704165_a1ac6f55fef555ee.jpg, si you end up, in that 2nd pipe stage, trying to execute: 1559704165_a1ac6f55fef555ee.jpg next_matching_filename 3rd_matching_filename ... nth_matching_filename. I guess you tried instead to filter to that filename (see answers below for that)

    – Olivier Dulac
    Jul 8 at 13:08








1




1





The bash says it all: it tries to execute that filename as it is the first on the line in the 2nd stage of the pipe (your 2nd stage pipe is: | ??????????_a1ac*.jpg: bash expands it to several filename, the first being 1559704165_a1ac6f55fef555ee.jpg, si you end up, in that 2nd pipe stage, trying to execute: 1559704165_a1ac6f55fef555ee.jpg next_matching_filename 3rd_matching_filename ... nth_matching_filename. I guess you tried instead to filter to that filename (see answers below for that)

– Olivier Dulac
Jul 8 at 13:08






The bash says it all: it tries to execute that filename as it is the first on the line in the 2nd stage of the pipe (your 2nd stage pipe is: | ??????????_a1ac*.jpg: bash expands it to several filename, the first being 1559704165_a1ac6f55fef555ee.jpg, si you end up, in that 2nd pipe stage, trying to execute: 1559704165_a1ac6f55fef555ee.jpg next_matching_filename 3rd_matching_filename ... nth_matching_filename. I guess you tried instead to filter to that filename (see answers below for that)

– Olivier Dulac
Jul 8 at 13:08











5 Answers
5






active

oldest

votes


















15














You should use:



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' 
-exec mv -t destination "" +


So maxdepth 1 means that you want to search in current directory no subdirectories.



type f means find only files.



name '??????????_a1ac*.jpg' is a pattern that matches with file you are searching.



mv -t destination "" + means move matched files to destination. Here + adds new matched files to previous one like:



mv -t dest a b c d


Here a b c d are different files.






share|improve this answer

























  • Thanks for concisely answering this persons question. Rather than simply dumping a solution, maybe you could explain how/what/why. Instead of being useful to one person, one time, it can be useful to everyone, all the time. Same question has been asked & answered countless times over the last 40-50 years. Problem is, it's never explained well. Teach a man to fish.. In the meantime: gnu.org/software/findutils/manual/html_node/find_html/… and as is often the case Wikipedia's more useful than the official docs: en.wikipedia.org/wiki/Find_(Unix)

    – tjt263
    Jul 8 at 17:22











  • See updated answer.

    – Prvt_Yadv
    Jul 8 at 17:48











  • Note that -t is a GNU extension and so may not be available on other types of UNIX derivatives.

    – Kevin
    Jul 8 at 18:22











  • When you say "Double quotes prevents word splitting." I presume you are referring to "", in which case I want to point out that is not expanded by the shell and does not need to be quoted. The shell passes to find, and find sees and replaces it with pathnames. Find exec does not use the shell parser and does not do any word-splitting of its own. Quoting it does not do any harm, it is just that the justification given is a bit inaccurate.

    – jw013
    Jul 9 at 18:59











  • @jw013 thanks .

    – Prvt_Yadv
    Jul 10 at 6:49


















11














Your command,



find . -maxdepth 1 -type f | ??????????_a1ac*.jpg |xargs mv -t "/home/ubuntu/ntest"


Pipes the list of all the files TO all the files!



find . -maxdepth 1 -type f -name `*_a1ac*.jpg` -print0 |
xargs -0 -r mv -t "/home/ubuntu/ntest"


will do the trick.






share|improve this answer


















  • 1





    many thanks...your solution worked too...thank you for letting me know where I went wrong

    – Apricot
    Jul 6 at 14:13


















8














You're very close. You should use the -name option to find. And remember to quote the pattern.



So



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' |xargs mv -t "/home/ubuntu/ntest"





share|improve this answer























  • Many thanks...your solution worked too....additional thanks for letting me know i was close to the solution....its a motivator for a novice like me

    – Apricot
    Jul 6 at 14:12






  • 1





    you should add a -print0 as the last argument to the find (instead of the default: -print), and add a -0 as the first option to xargs (ie: xargs -0 mv -t "/home/ubuntu/ntest" ). that way, all kind of weird filenames (with spaces in it, with "newline" in it, etc) can be handled. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' -print0 |xargs -0 mv -t "/home/ubuntu/ntest" (works only with GNU-like find, though)

    – Olivier Dulac
    Jul 8 at 13:10



















2














Not as "good" as the find solutions, but another valid solution is to make the mv commands more granular.



This does 4096 moves, with a fewer number of files moved per mv operation.



FILEPAT=a1ac
for i in $(seq $((0x000)) $((0xfff)));
do
H=$(printf '%xn' $i)
mv 1559704165_$FILEPAT$H*.jpg /home/ubuntu/ntest
done





share|improve this answer























  • This is a clever hack for those without find (for whatever reason).

    – forest
    Jul 8 at 7:44



















-1














If you want to move files on the same host system, which I guess you are doing with your mv, rsync could be a faster option:



rsync -av --inplace -W /source/??????????_a1ac*.jpg /home/ubuntu/ntest/


--inplaceand -W are set to speed up the process.



Should this yield another argument list too long error then you could feed lists to rsync



Make the list with find, for example



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt


and give it to rsync



rsync -av --inplace -W --files-from=/tmp/my_image_list.txt /path/to/files /home/ubuntu/ntest/


The source here is /path/to/files, because rsync will treat the list you give it as relative to your source.




The point being: rsync is faster than mv, if the files are not on the same filesystem.






share|improve this answer

























  • This is likely to hit the same "argument list too long" error the OP mentioned

    – Grump
    Jul 8 at 13:24











  • @Grump, to avoid this, OP could write the list of files to copy to a file, i.e. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt and then pass it to rsync with --files-from=/tmp/my_image_list.txt. The point being that rsync is faster. Unless the files reside on the same filesystem, which OP has not indicated.

    – Robert Riedl
    Jul 8 at 13:46











  • @RobertRiedl: you should edit your answer and add this information. Comments can be impermanent.

    – NickD
    Jul 10 at 4:13











  • @NickD, I've updated my answer.

    – Robert Riedl
    Jul 10 at 6:31













Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f528679%2fmoving-millions-of-files-to-a-different-directory-with-specfic-name-patterns%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























5 Answers
5






active

oldest

votes








5 Answers
5






active

oldest

votes









active

oldest

votes






active

oldest

votes









15














You should use:



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' 
-exec mv -t destination "" +


So maxdepth 1 means that you want to search in current directory no subdirectories.



type f means find only files.



name '??????????_a1ac*.jpg' is a pattern that matches with file you are searching.



mv -t destination "" + means move matched files to destination. Here + adds new matched files to previous one like:



mv -t dest a b c d


Here a b c d are different files.






share|improve this answer

























  • Thanks for concisely answering this persons question. Rather than simply dumping a solution, maybe you could explain how/what/why. Instead of being useful to one person, one time, it can be useful to everyone, all the time. Same question has been asked & answered countless times over the last 40-50 years. Problem is, it's never explained well. Teach a man to fish.. In the meantime: gnu.org/software/findutils/manual/html_node/find_html/… and as is often the case Wikipedia's more useful than the official docs: en.wikipedia.org/wiki/Find_(Unix)

    – tjt263
    Jul 8 at 17:22











  • See updated answer.

    – Prvt_Yadv
    Jul 8 at 17:48











  • Note that -t is a GNU extension and so may not be available on other types of UNIX derivatives.

    – Kevin
    Jul 8 at 18:22











  • When you say "Double quotes prevents word splitting." I presume you are referring to "", in which case I want to point out that is not expanded by the shell and does not need to be quoted. The shell passes to find, and find sees and replaces it with pathnames. Find exec does not use the shell parser and does not do any word-splitting of its own. Quoting it does not do any harm, it is just that the justification given is a bit inaccurate.

    – jw013
    Jul 9 at 18:59











  • @jw013 thanks .

    – Prvt_Yadv
    Jul 10 at 6:49















15














You should use:



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' 
-exec mv -t destination "" +


So maxdepth 1 means that you want to search in current directory no subdirectories.



type f means find only files.



name '??????????_a1ac*.jpg' is a pattern that matches with file you are searching.



mv -t destination "" + means move matched files to destination. Here + adds new matched files to previous one like:



mv -t dest a b c d


Here a b c d are different files.






share|improve this answer

























  • Thanks for concisely answering this persons question. Rather than simply dumping a solution, maybe you could explain how/what/why. Instead of being useful to one person, one time, it can be useful to everyone, all the time. Same question has been asked & answered countless times over the last 40-50 years. Problem is, it's never explained well. Teach a man to fish.. In the meantime: gnu.org/software/findutils/manual/html_node/find_html/… and as is often the case Wikipedia's more useful than the official docs: en.wikipedia.org/wiki/Find_(Unix)

    – tjt263
    Jul 8 at 17:22











  • See updated answer.

    – Prvt_Yadv
    Jul 8 at 17:48











  • Note that -t is a GNU extension and so may not be available on other types of UNIX derivatives.

    – Kevin
    Jul 8 at 18:22











  • When you say "Double quotes prevents word splitting." I presume you are referring to "", in which case I want to point out that is not expanded by the shell and does not need to be quoted. The shell passes to find, and find sees and replaces it with pathnames. Find exec does not use the shell parser and does not do any word-splitting of its own. Quoting it does not do any harm, it is just that the justification given is a bit inaccurate.

    – jw013
    Jul 9 at 18:59











  • @jw013 thanks .

    – Prvt_Yadv
    Jul 10 at 6:49













15












15








15







You should use:



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' 
-exec mv -t destination "" +


So maxdepth 1 means that you want to search in current directory no subdirectories.



type f means find only files.



name '??????????_a1ac*.jpg' is a pattern that matches with file you are searching.



mv -t destination "" + means move matched files to destination. Here + adds new matched files to previous one like:



mv -t dest a b c d


Here a b c d are different files.






share|improve this answer















You should use:



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' 
-exec mv -t destination "" +


So maxdepth 1 means that you want to search in current directory no subdirectories.



type f means find only files.



name '??????????_a1ac*.jpg' is a pattern that matches with file you are searching.



mv -t destination "" + means move matched files to destination. Here + adds new matched files to previous one like:



mv -t dest a b c d


Here a b c d are different files.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 10 at 4:00

























answered Jul 6 at 13:42









Prvt_YadvPrvt_Yadv

3,7413 gold badges17 silver badges34 bronze badges




3,7413 gold badges17 silver badges34 bronze badges












  • Thanks for concisely answering this persons question. Rather than simply dumping a solution, maybe you could explain how/what/why. Instead of being useful to one person, one time, it can be useful to everyone, all the time. Same question has been asked & answered countless times over the last 40-50 years. Problem is, it's never explained well. Teach a man to fish.. In the meantime: gnu.org/software/findutils/manual/html_node/find_html/… and as is often the case Wikipedia's more useful than the official docs: en.wikipedia.org/wiki/Find_(Unix)

    – tjt263
    Jul 8 at 17:22











  • See updated answer.

    – Prvt_Yadv
    Jul 8 at 17:48











  • Note that -t is a GNU extension and so may not be available on other types of UNIX derivatives.

    – Kevin
    Jul 8 at 18:22











  • When you say "Double quotes prevents word splitting." I presume you are referring to "", in which case I want to point out that is not expanded by the shell and does not need to be quoted. The shell passes to find, and find sees and replaces it with pathnames. Find exec does not use the shell parser and does not do any word-splitting of its own. Quoting it does not do any harm, it is just that the justification given is a bit inaccurate.

    – jw013
    Jul 9 at 18:59











  • @jw013 thanks .

    – Prvt_Yadv
    Jul 10 at 6:49

















  • Thanks for concisely answering this persons question. Rather than simply dumping a solution, maybe you could explain how/what/why. Instead of being useful to one person, one time, it can be useful to everyone, all the time. Same question has been asked & answered countless times over the last 40-50 years. Problem is, it's never explained well. Teach a man to fish.. In the meantime: gnu.org/software/findutils/manual/html_node/find_html/… and as is often the case Wikipedia's more useful than the official docs: en.wikipedia.org/wiki/Find_(Unix)

    – tjt263
    Jul 8 at 17:22











  • See updated answer.

    – Prvt_Yadv
    Jul 8 at 17:48











  • Note that -t is a GNU extension and so may not be available on other types of UNIX derivatives.

    – Kevin
    Jul 8 at 18:22











  • When you say "Double quotes prevents word splitting." I presume you are referring to "", in which case I want to point out that is not expanded by the shell and does not need to be quoted. The shell passes to find, and find sees and replaces it with pathnames. Find exec does not use the shell parser and does not do any word-splitting of its own. Quoting it does not do any harm, it is just that the justification given is a bit inaccurate.

    – jw013
    Jul 9 at 18:59











  • @jw013 thanks .

    – Prvt_Yadv
    Jul 10 at 6:49
















Thanks for concisely answering this persons question. Rather than simply dumping a solution, maybe you could explain how/what/why. Instead of being useful to one person, one time, it can be useful to everyone, all the time. Same question has been asked & answered countless times over the last 40-50 years. Problem is, it's never explained well. Teach a man to fish.. In the meantime: gnu.org/software/findutils/manual/html_node/find_html/… and as is often the case Wikipedia's more useful than the official docs: en.wikipedia.org/wiki/Find_(Unix)

– tjt263
Jul 8 at 17:22





Thanks for concisely answering this persons question. Rather than simply dumping a solution, maybe you could explain how/what/why. Instead of being useful to one person, one time, it can be useful to everyone, all the time. Same question has been asked & answered countless times over the last 40-50 years. Problem is, it's never explained well. Teach a man to fish.. In the meantime: gnu.org/software/findutils/manual/html_node/find_html/… and as is often the case Wikipedia's more useful than the official docs: en.wikipedia.org/wiki/Find_(Unix)

– tjt263
Jul 8 at 17:22













See updated answer.

– Prvt_Yadv
Jul 8 at 17:48





See updated answer.

– Prvt_Yadv
Jul 8 at 17:48













Note that -t is a GNU extension and so may not be available on other types of UNIX derivatives.

– Kevin
Jul 8 at 18:22





Note that -t is a GNU extension and so may not be available on other types of UNIX derivatives.

– Kevin
Jul 8 at 18:22













When you say "Double quotes prevents word splitting." I presume you are referring to "", in which case I want to point out that is not expanded by the shell and does not need to be quoted. The shell passes to find, and find sees and replaces it with pathnames. Find exec does not use the shell parser and does not do any word-splitting of its own. Quoting it does not do any harm, it is just that the justification given is a bit inaccurate.

– jw013
Jul 9 at 18:59





When you say "Double quotes prevents word splitting." I presume you are referring to "", in which case I want to point out that is not expanded by the shell and does not need to be quoted. The shell passes to find, and find sees and replaces it with pathnames. Find exec does not use the shell parser and does not do any word-splitting of its own. Quoting it does not do any harm, it is just that the justification given is a bit inaccurate.

– jw013
Jul 9 at 18:59













@jw013 thanks .

– Prvt_Yadv
Jul 10 at 6:49





@jw013 thanks .

– Prvt_Yadv
Jul 10 at 6:49













11














Your command,



find . -maxdepth 1 -type f | ??????????_a1ac*.jpg |xargs mv -t "/home/ubuntu/ntest"


Pipes the list of all the files TO all the files!



find . -maxdepth 1 -type f -name `*_a1ac*.jpg` -print0 |
xargs -0 -r mv -t "/home/ubuntu/ntest"


will do the trick.






share|improve this answer


















  • 1





    many thanks...your solution worked too...thank you for letting me know where I went wrong

    – Apricot
    Jul 6 at 14:13















11














Your command,



find . -maxdepth 1 -type f | ??????????_a1ac*.jpg |xargs mv -t "/home/ubuntu/ntest"


Pipes the list of all the files TO all the files!



find . -maxdepth 1 -type f -name `*_a1ac*.jpg` -print0 |
xargs -0 -r mv -t "/home/ubuntu/ntest"


will do the trick.






share|improve this answer


















  • 1





    many thanks...your solution worked too...thank you for letting me know where I went wrong

    – Apricot
    Jul 6 at 14:13













11












11








11







Your command,



find . -maxdepth 1 -type f | ??????????_a1ac*.jpg |xargs mv -t "/home/ubuntu/ntest"


Pipes the list of all the files TO all the files!



find . -maxdepth 1 -type f -name `*_a1ac*.jpg` -print0 |
xargs -0 -r mv -t "/home/ubuntu/ntest"


will do the trick.






share|improve this answer













Your command,



find . -maxdepth 1 -type f | ??????????_a1ac*.jpg |xargs mv -t "/home/ubuntu/ntest"


Pipes the list of all the files TO all the files!



find . -maxdepth 1 -type f -name `*_a1ac*.jpg` -print0 |
xargs -0 -r mv -t "/home/ubuntu/ntest"


will do the trick.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 6 at 13:49









waltinatorwaltinator

9085 silver badges10 bronze badges




9085 silver badges10 bronze badges







  • 1





    many thanks...your solution worked too...thank you for letting me know where I went wrong

    – Apricot
    Jul 6 at 14:13












  • 1





    many thanks...your solution worked too...thank you for letting me know where I went wrong

    – Apricot
    Jul 6 at 14:13







1




1





many thanks...your solution worked too...thank you for letting me know where I went wrong

– Apricot
Jul 6 at 14:13





many thanks...your solution worked too...thank you for letting me know where I went wrong

– Apricot
Jul 6 at 14:13











8














You're very close. You should use the -name option to find. And remember to quote the pattern.



So



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' |xargs mv -t "/home/ubuntu/ntest"





share|improve this answer























  • Many thanks...your solution worked too....additional thanks for letting me know i was close to the solution....its a motivator for a novice like me

    – Apricot
    Jul 6 at 14:12






  • 1





    you should add a -print0 as the last argument to the find (instead of the default: -print), and add a -0 as the first option to xargs (ie: xargs -0 mv -t "/home/ubuntu/ntest" ). that way, all kind of weird filenames (with spaces in it, with "newline" in it, etc) can be handled. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' -print0 |xargs -0 mv -t "/home/ubuntu/ntest" (works only with GNU-like find, though)

    – Olivier Dulac
    Jul 8 at 13:10
















8














You're very close. You should use the -name option to find. And remember to quote the pattern.



So



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' |xargs mv -t "/home/ubuntu/ntest"





share|improve this answer























  • Many thanks...your solution worked too....additional thanks for letting me know i was close to the solution....its a motivator for a novice like me

    – Apricot
    Jul 6 at 14:12






  • 1





    you should add a -print0 as the last argument to the find (instead of the default: -print), and add a -0 as the first option to xargs (ie: xargs -0 mv -t "/home/ubuntu/ntest" ). that way, all kind of weird filenames (with spaces in it, with "newline" in it, etc) can be handled. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' -print0 |xargs -0 mv -t "/home/ubuntu/ntest" (works only with GNU-like find, though)

    – Olivier Dulac
    Jul 8 at 13:10














8












8








8







You're very close. You should use the -name option to find. And remember to quote the pattern.



So



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' |xargs mv -t "/home/ubuntu/ntest"





share|improve this answer













You're very close. You should use the -name option to find. And remember to quote the pattern.



So



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' |xargs mv -t "/home/ubuntu/ntest"






share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 6 at 13:45









Stephen HarrisStephen Harris

28.7k3 gold badges57 silver badges85 bronze badges




28.7k3 gold badges57 silver badges85 bronze badges












  • Many thanks...your solution worked too....additional thanks for letting me know i was close to the solution....its a motivator for a novice like me

    – Apricot
    Jul 6 at 14:12






  • 1





    you should add a -print0 as the last argument to the find (instead of the default: -print), and add a -0 as the first option to xargs (ie: xargs -0 mv -t "/home/ubuntu/ntest" ). that way, all kind of weird filenames (with spaces in it, with "newline" in it, etc) can be handled. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' -print0 |xargs -0 mv -t "/home/ubuntu/ntest" (works only with GNU-like find, though)

    – Olivier Dulac
    Jul 8 at 13:10


















  • Many thanks...your solution worked too....additional thanks for letting me know i was close to the solution....its a motivator for a novice like me

    – Apricot
    Jul 6 at 14:12






  • 1





    you should add a -print0 as the last argument to the find (instead of the default: -print), and add a -0 as the first option to xargs (ie: xargs -0 mv -t "/home/ubuntu/ntest" ). that way, all kind of weird filenames (with spaces in it, with "newline" in it, etc) can be handled. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' -print0 |xargs -0 mv -t "/home/ubuntu/ntest" (works only with GNU-like find, though)

    – Olivier Dulac
    Jul 8 at 13:10

















Many thanks...your solution worked too....additional thanks for letting me know i was close to the solution....its a motivator for a novice like me

– Apricot
Jul 6 at 14:12





Many thanks...your solution worked too....additional thanks for letting me know i was close to the solution....its a motivator for a novice like me

– Apricot
Jul 6 at 14:12




1




1





you should add a -print0 as the last argument to the find (instead of the default: -print), and add a -0 as the first option to xargs (ie: xargs -0 mv -t "/home/ubuntu/ntest" ). that way, all kind of weird filenames (with spaces in it, with "newline" in it, etc) can be handled. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' -print0 |xargs -0 mv -t "/home/ubuntu/ntest" (works only with GNU-like find, though)

– Olivier Dulac
Jul 8 at 13:10






you should add a -print0 as the last argument to the find (instead of the default: -print), and add a -0 as the first option to xargs (ie: xargs -0 mv -t "/home/ubuntu/ntest" ). that way, all kind of weird filenames (with spaces in it, with "newline" in it, etc) can be handled. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' -print0 |xargs -0 mv -t "/home/ubuntu/ntest" (works only with GNU-like find, though)

– Olivier Dulac
Jul 8 at 13:10












2














Not as "good" as the find solutions, but another valid solution is to make the mv commands more granular.



This does 4096 moves, with a fewer number of files moved per mv operation.



FILEPAT=a1ac
for i in $(seq $((0x000)) $((0xfff)));
do
H=$(printf '%xn' $i)
mv 1559704165_$FILEPAT$H*.jpg /home/ubuntu/ntest
done





share|improve this answer























  • This is a clever hack for those without find (for whatever reason).

    – forest
    Jul 8 at 7:44
















2














Not as "good" as the find solutions, but another valid solution is to make the mv commands more granular.



This does 4096 moves, with a fewer number of files moved per mv operation.



FILEPAT=a1ac
for i in $(seq $((0x000)) $((0xfff)));
do
H=$(printf '%xn' $i)
mv 1559704165_$FILEPAT$H*.jpg /home/ubuntu/ntest
done





share|improve this answer























  • This is a clever hack for those without find (for whatever reason).

    – forest
    Jul 8 at 7:44














2












2








2







Not as "good" as the find solutions, but another valid solution is to make the mv commands more granular.



This does 4096 moves, with a fewer number of files moved per mv operation.



FILEPAT=a1ac
for i in $(seq $((0x000)) $((0xfff)));
do
H=$(printf '%xn' $i)
mv 1559704165_$FILEPAT$H*.jpg /home/ubuntu/ntest
done





share|improve this answer













Not as "good" as the find solutions, but another valid solution is to make the mv commands more granular.



This does 4096 moves, with a fewer number of files moved per mv operation.



FILEPAT=a1ac
for i in $(seq $((0x000)) $((0xfff)));
do
H=$(printf '%xn' $i)
mv 1559704165_$FILEPAT$H*.jpg /home/ubuntu/ntest
done






share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 8 at 0:28









RonJohnRonJohn

5834 silver badges16 bronze badges




5834 silver badges16 bronze badges












  • This is a clever hack for those without find (for whatever reason).

    – forest
    Jul 8 at 7:44


















  • This is a clever hack for those without find (for whatever reason).

    – forest
    Jul 8 at 7:44

















This is a clever hack for those without find (for whatever reason).

– forest
Jul 8 at 7:44






This is a clever hack for those without find (for whatever reason).

– forest
Jul 8 at 7:44












-1














If you want to move files on the same host system, which I guess you are doing with your mv, rsync could be a faster option:



rsync -av --inplace -W /source/??????????_a1ac*.jpg /home/ubuntu/ntest/


--inplaceand -W are set to speed up the process.



Should this yield another argument list too long error then you could feed lists to rsync



Make the list with find, for example



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt


and give it to rsync



rsync -av --inplace -W --files-from=/tmp/my_image_list.txt /path/to/files /home/ubuntu/ntest/


The source here is /path/to/files, because rsync will treat the list you give it as relative to your source.




The point being: rsync is faster than mv, if the files are not on the same filesystem.






share|improve this answer

























  • This is likely to hit the same "argument list too long" error the OP mentioned

    – Grump
    Jul 8 at 13:24











  • @Grump, to avoid this, OP could write the list of files to copy to a file, i.e. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt and then pass it to rsync with --files-from=/tmp/my_image_list.txt. The point being that rsync is faster. Unless the files reside on the same filesystem, which OP has not indicated.

    – Robert Riedl
    Jul 8 at 13:46











  • @RobertRiedl: you should edit your answer and add this information. Comments can be impermanent.

    – NickD
    Jul 10 at 4:13











  • @NickD, I've updated my answer.

    – Robert Riedl
    Jul 10 at 6:31















-1














If you want to move files on the same host system, which I guess you are doing with your mv, rsync could be a faster option:



rsync -av --inplace -W /source/??????????_a1ac*.jpg /home/ubuntu/ntest/


--inplaceand -W are set to speed up the process.



Should this yield another argument list too long error then you could feed lists to rsync



Make the list with find, for example



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt


and give it to rsync



rsync -av --inplace -W --files-from=/tmp/my_image_list.txt /path/to/files /home/ubuntu/ntest/


The source here is /path/to/files, because rsync will treat the list you give it as relative to your source.




The point being: rsync is faster than mv, if the files are not on the same filesystem.






share|improve this answer

























  • This is likely to hit the same "argument list too long" error the OP mentioned

    – Grump
    Jul 8 at 13:24











  • @Grump, to avoid this, OP could write the list of files to copy to a file, i.e. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt and then pass it to rsync with --files-from=/tmp/my_image_list.txt. The point being that rsync is faster. Unless the files reside on the same filesystem, which OP has not indicated.

    – Robert Riedl
    Jul 8 at 13:46











  • @RobertRiedl: you should edit your answer and add this information. Comments can be impermanent.

    – NickD
    Jul 10 at 4:13











  • @NickD, I've updated my answer.

    – Robert Riedl
    Jul 10 at 6:31













-1












-1








-1







If you want to move files on the same host system, which I guess you are doing with your mv, rsync could be a faster option:



rsync -av --inplace -W /source/??????????_a1ac*.jpg /home/ubuntu/ntest/


--inplaceand -W are set to speed up the process.



Should this yield another argument list too long error then you could feed lists to rsync



Make the list with find, for example



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt


and give it to rsync



rsync -av --inplace -W --files-from=/tmp/my_image_list.txt /path/to/files /home/ubuntu/ntest/


The source here is /path/to/files, because rsync will treat the list you give it as relative to your source.




The point being: rsync is faster than mv, if the files are not on the same filesystem.






share|improve this answer















If you want to move files on the same host system, which I guess you are doing with your mv, rsync could be a faster option:



rsync -av --inplace -W /source/??????????_a1ac*.jpg /home/ubuntu/ntest/


--inplaceand -W are set to speed up the process.



Should this yield another argument list too long error then you could feed lists to rsync



Make the list with find, for example



find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt


and give it to rsync



rsync -av --inplace -W --files-from=/tmp/my_image_list.txt /path/to/files /home/ubuntu/ntest/


The source here is /path/to/files, because rsync will treat the list you give it as relative to your source.




The point being: rsync is faster than mv, if the files are not on the same filesystem.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 10 at 6:30

























answered Jul 8 at 7:09









Robert RiedlRobert Riedl

1178 bronze badges




1178 bronze badges












  • This is likely to hit the same "argument list too long" error the OP mentioned

    – Grump
    Jul 8 at 13:24











  • @Grump, to avoid this, OP could write the list of files to copy to a file, i.e. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt and then pass it to rsync with --files-from=/tmp/my_image_list.txt. The point being that rsync is faster. Unless the files reside on the same filesystem, which OP has not indicated.

    – Robert Riedl
    Jul 8 at 13:46











  • @RobertRiedl: you should edit your answer and add this information. Comments can be impermanent.

    – NickD
    Jul 10 at 4:13











  • @NickD, I've updated my answer.

    – Robert Riedl
    Jul 10 at 6:31

















  • This is likely to hit the same "argument list too long" error the OP mentioned

    – Grump
    Jul 8 at 13:24











  • @Grump, to avoid this, OP could write the list of files to copy to a file, i.e. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt and then pass it to rsync with --files-from=/tmp/my_image_list.txt. The point being that rsync is faster. Unless the files reside on the same filesystem, which OP has not indicated.

    – Robert Riedl
    Jul 8 at 13:46











  • @RobertRiedl: you should edit your answer and add this information. Comments can be impermanent.

    – NickD
    Jul 10 at 4:13











  • @NickD, I've updated my answer.

    – Robert Riedl
    Jul 10 at 6:31
















This is likely to hit the same "argument list too long" error the OP mentioned

– Grump
Jul 8 at 13:24





This is likely to hit the same "argument list too long" error the OP mentioned

– Grump
Jul 8 at 13:24













@Grump, to avoid this, OP could write the list of files to copy to a file, i.e. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt and then pass it to rsync with --files-from=/tmp/my_image_list.txt. The point being that rsync is faster. Unless the files reside on the same filesystem, which OP has not indicated.

– Robert Riedl
Jul 8 at 13:46





@Grump, to avoid this, OP could write the list of files to copy to a file, i.e. find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' > /tmp/my_image_list.txt and then pass it to rsync with --files-from=/tmp/my_image_list.txt. The point being that rsync is faster. Unless the files reside on the same filesystem, which OP has not indicated.

– Robert Riedl
Jul 8 at 13:46













@RobertRiedl: you should edit your answer and add this information. Comments can be impermanent.

– NickD
Jul 10 at 4:13





@RobertRiedl: you should edit your answer and add this information. Comments can be impermanent.

– NickD
Jul 10 at 4:13













@NickD, I've updated my answer.

– Robert Riedl
Jul 10 at 6:31





@NickD, I've updated my answer.

– Robert Riedl
Jul 10 at 6:31

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f528679%2fmoving-millions-of-files-to-a-different-directory-with-specfic-name-patterns%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form