What does cd myfolder/* do in Linux?

0

I am new to Linux and have been using the cd command for a while. I just saw someone cd'ing to somefolder/* like cd foo/*. What does that do?

Also, depending on how that works, what will be the outcome of cp -R foo/* bar ?

Edit- If cd foo/* cd's into the first directory inside foo, shouldn't cp -R foo/* bar only copy the first directory present inside foo to bar?

aandis

Posted 2014-06-12T10:29:40.677

Reputation: 103

Answers

1

* will be expanded to all items in foo and then you will cd into the first one of those items (which is hopefully a directory).

Der Hochstapler

Posted 2014-06-12T10:29:40.677

Reputation: 77 228

so shouldn't cp -R foo/* bar only copy the first directory inside foo to bar ? – aandis – 2014-06-12T10:34:10.013

No, that will copy all of them. When you use * on the shell, press Ctrl+X and then * instead of pressing Enter, this will expand the * and show you what will actually be executed. – Der Hochstapler – 2014-06-12T10:36:12.650

1The difference is that cp takes multiple arguments (filenames) and cd only takes a single argument. When the shell expands multiple filenames, cd will take the first. – mtak – 2014-06-12T10:53:43.803