Copy all files from certain file type extension in script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I am trying to execute the following command in a shell script where adir="mydir" and extType=*.txt



cp "$adir/$extType" "$destination"


However I get:



cp: cannot stat ‘mydir/*.txt’: No such file or directory


but there is such directory and if I execute it by hand in command line it works.



Any ideas what's wrong?










share|improve this question























  • Quoting prevents globbing.
    – Cyrus
    46 mins ago














up vote
3
down vote

favorite












I am trying to execute the following command in a shell script where adir="mydir" and extType=*.txt



cp "$adir/$extType" "$destination"


However I get:



cp: cannot stat ‘mydir/*.txt’: No such file or directory


but there is such directory and if I execute it by hand in command line it works.



Any ideas what's wrong?










share|improve this question























  • Quoting prevents globbing.
    – Cyrus
    46 mins ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I am trying to execute the following command in a shell script where adir="mydir" and extType=*.txt



cp "$adir/$extType" "$destination"


However I get:



cp: cannot stat ‘mydir/*.txt’: No such file or directory


but there is such directory and if I execute it by hand in command line it works.



Any ideas what's wrong?










share|improve this question















I am trying to execute the following command in a shell script where adir="mydir" and extType=*.txt



cp "$adir/$extType" "$destination"


However I get:



cp: cannot stat ‘mydir/*.txt’: No such file or directory


but there is such directory and if I execute it by hand in command line it works.



Any ideas what's wrong?







command-line bash scripts copy file-format






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 39 mins ago









Ravexina

29.1k147199




29.1k147199










asked 59 mins ago









Jocky Doe

1455




1455











  • Quoting prevents globbing.
    – Cyrus
    46 mins ago
















  • Quoting prevents globbing.
    – Cyrus
    46 mins ago















Quoting prevents globbing.
– Cyrus
46 mins ago




Quoting prevents globbing.
– Cyrus
46 mins ago










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










You should change "$adir/$extType" to "$adir/"$extType otherwise glob expansion won't take place and it looks for a file exactly with the name of *.txt which can not be found and thus it complains about it with:



cp: cannot stat ‘mydir/*.txt’: No such file or directory





share|improve this answer






















  • Thank you very much
    – Jocky Doe
    23 mins ago

















up vote
2
down vote













Ravexina already pointed out well that globbing doesn't work in double or single quotes, and btw that cp syntax is wrong for multiple files / single destination and needs -t flag.



What you could do as alternative is to use bash arrays:



extType=( "$adir"/*.txt )
cp -t "$destination" "$extType[@]"


Alternatively, loop



for i in "$adir"/*.txt ; do cp "$i" "$destination"; done





share|improve this answer




















  • Thank you I didn't know that
    – Jocky Doe
    21 mins ago










  • The cp syntax isn't that wrong: cp a b c d copies a, b, and c to an existing directory d.
    – PerlDuck
    16 mins ago











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
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',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1081652%2fcopy-all-files-from-certain-file-type-extension-in-script%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










You should change "$adir/$extType" to "$adir/"$extType otherwise glob expansion won't take place and it looks for a file exactly with the name of *.txt which can not be found and thus it complains about it with:



cp: cannot stat ‘mydir/*.txt’: No such file or directory





share|improve this answer






















  • Thank you very much
    – Jocky Doe
    23 mins ago














up vote
4
down vote



accepted










You should change "$adir/$extType" to "$adir/"$extType otherwise glob expansion won't take place and it looks for a file exactly with the name of *.txt which can not be found and thus it complains about it with:



cp: cannot stat ‘mydir/*.txt’: No such file or directory





share|improve this answer






















  • Thank you very much
    – Jocky Doe
    23 mins ago












up vote
4
down vote



accepted







up vote
4
down vote



accepted






You should change "$adir/$extType" to "$adir/"$extType otherwise glob expansion won't take place and it looks for a file exactly with the name of *.txt which can not be found and thus it complains about it with:



cp: cannot stat ‘mydir/*.txt’: No such file or directory





share|improve this answer














You should change "$adir/$extType" to "$adir/"$extType otherwise glob expansion won't take place and it looks for a file exactly with the name of *.txt which can not be found and thus it complains about it with:



cp: cannot stat ‘mydir/*.txt’: No such file or directory






share|improve this answer














share|improve this answer



share|improve this answer








edited 48 mins ago

























answered 55 mins ago









Ravexina

29.1k147199




29.1k147199











  • Thank you very much
    – Jocky Doe
    23 mins ago
















  • Thank you very much
    – Jocky Doe
    23 mins ago















Thank you very much
– Jocky Doe
23 mins ago




Thank you very much
– Jocky Doe
23 mins ago












up vote
2
down vote













Ravexina already pointed out well that globbing doesn't work in double or single quotes, and btw that cp syntax is wrong for multiple files / single destination and needs -t flag.



What you could do as alternative is to use bash arrays:



extType=( "$adir"/*.txt )
cp -t "$destination" "$extType[@]"


Alternatively, loop



for i in "$adir"/*.txt ; do cp "$i" "$destination"; done





share|improve this answer




















  • Thank you I didn't know that
    – Jocky Doe
    21 mins ago










  • The cp syntax isn't that wrong: cp a b c d copies a, b, and c to an existing directory d.
    – PerlDuck
    16 mins ago















up vote
2
down vote













Ravexina already pointed out well that globbing doesn't work in double or single quotes, and btw that cp syntax is wrong for multiple files / single destination and needs -t flag.



What you could do as alternative is to use bash arrays:



extType=( "$adir"/*.txt )
cp -t "$destination" "$extType[@]"


Alternatively, loop



for i in "$adir"/*.txt ; do cp "$i" "$destination"; done





share|improve this answer




















  • Thank you I didn't know that
    – Jocky Doe
    21 mins ago










  • The cp syntax isn't that wrong: cp a b c d copies a, b, and c to an existing directory d.
    – PerlDuck
    16 mins ago













up vote
2
down vote










up vote
2
down vote









Ravexina already pointed out well that globbing doesn't work in double or single quotes, and btw that cp syntax is wrong for multiple files / single destination and needs -t flag.



What you could do as alternative is to use bash arrays:



extType=( "$adir"/*.txt )
cp -t "$destination" "$extType[@]"


Alternatively, loop



for i in "$adir"/*.txt ; do cp "$i" "$destination"; done





share|improve this answer












Ravexina already pointed out well that globbing doesn't work in double or single quotes, and btw that cp syntax is wrong for multiple files / single destination and needs -t flag.



What you could do as alternative is to use bash arrays:



extType=( "$adir"/*.txt )
cp -t "$destination" "$extType[@]"


Alternatively, loop



for i in "$adir"/*.txt ; do cp "$i" "$destination"; done






share|improve this answer












share|improve this answer



share|improve this answer










answered 48 mins ago









Sergiy Kolodyazhnyy

66k9134289




66k9134289











  • Thank you I didn't know that
    – Jocky Doe
    21 mins ago










  • The cp syntax isn't that wrong: cp a b c d copies a, b, and c to an existing directory d.
    – PerlDuck
    16 mins ago

















  • Thank you I didn't know that
    – Jocky Doe
    21 mins ago










  • The cp syntax isn't that wrong: cp a b c d copies a, b, and c to an existing directory d.
    – PerlDuck
    16 mins ago
















Thank you I didn't know that
– Jocky Doe
21 mins ago




Thank you I didn't know that
– Jocky Doe
21 mins ago












The cp syntax isn't that wrong: cp a b c d copies a, b, and c to an existing directory d.
– PerlDuck
16 mins ago





The cp syntax isn't that wrong: cp a b c d copies a, b, and c to an existing directory d.
– PerlDuck
16 mins ago


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1081652%2fcopy-all-files-from-certain-file-type-extension-in-script%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery