Copy all files from certain file type extension in script
Clash 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?
command-line bash scripts copy file-format
add a comment |Â
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?
command-line bash scripts copy file-format
Quoting prevents globbing.
– Cyrus
46 mins ago
add a comment |Â
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?
command-line bash scripts copy file-format
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
command-line bash scripts copy file-format
edited 39 mins ago


Ravexina
29.1k147199
29.1k147199
asked 59 mins ago


Jocky Doe
1455
1455
Quoting prevents globbing.
– Cyrus
46 mins ago
add a comment |Â
Quoting prevents globbing.
– Cyrus
46 mins ago
Quoting prevents globbing.
– Cyrus
46 mins ago
Quoting prevents globbing.
– Cyrus
46 mins ago
add a comment |Â
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
Thank you very much
– Jocky Doe
23 mins ago
add a comment |Â
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
Thank you I didn't know that
– Jocky Doe
21 mins ago
Thecp
syntax isn't that wrong:cp a b c d
copiesa
,b
, andc
to an existing directoryd
.
– PerlDuck
16 mins ago
add a comment |Â
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
Thank you very much
– Jocky Doe
23 mins ago
add a comment |Â
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
Thank you very much
– Jocky Doe
23 mins ago
add a comment |Â
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
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
edited 48 mins ago
answered 55 mins ago


Ravexina
29.1k147199
29.1k147199
Thank you very much
– Jocky Doe
23 mins ago
add a comment |Â
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
add a comment |Â
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
Thank you I didn't know that
– Jocky Doe
21 mins ago
Thecp
syntax isn't that wrong:cp a b c d
copiesa
,b
, andc
to an existing directoryd
.
– PerlDuck
16 mins ago
add a comment |Â
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
Thank you I didn't know that
– Jocky Doe
21 mins ago
Thecp
syntax isn't that wrong:cp a b c d
copiesa
,b
, andc
to an existing directoryd
.
– PerlDuck
16 mins ago
add a comment |Â
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
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
answered 48 mins ago


Sergiy Kolodyazhnyy
66k9134289
66k9134289
Thank you I didn't know that
– Jocky Doe
21 mins ago
Thecp
syntax isn't that wrong:cp a b c d
copiesa
,b
, andc
to an existing directoryd
.
– PerlDuck
16 mins ago
add a comment |Â
Thank you I didn't know that
– Jocky Doe
21 mins ago
Thecp
syntax isn't that wrong:cp a b c d
copiesa
,b
, andc
to an existing directoryd
.
– 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
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%2faskubuntu.com%2fquestions%2f1081652%2fcopy-all-files-from-certain-file-type-extension-in-script%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
Quoting prevents globbing.
– Cyrus
46 mins ago