Why am I not able to use * with touch in path?

Random Person

This is the output of tree:

[xyz@localhost Semester1]$ tree
.
├── Eng
├── IT
├── IT_workshop
├── LA
├── OS
├── OS_lab
├── Psy
├── Python
└── Python_lab

9 directories, 0 files

I want to create 3 common files (named credits, links and notes) in each of these directories using touch.

I tried this command:

[xyz@localhost Semester1]$ touch */{credits,links,notes}

and this was the output:

touch: cannot touch ‘*/credits’: No such file or directory
touch: cannot touch ‘*/links’: No such file or directory
touch: cannot touch ‘*/notes’: No such file or directory

Why did the command not work as I expected it to?

BTW, I'm using CentOS Linux 7.

terdon

The problem is that globs (the */ is a glob) are expanded by the shell before launching the command. And brace expansions happen before globs. What this means is that */{credits,links,notes} becomes '*/credits' '*/links' '*/notes' and then these globs are expanded by the shell, and because the files haven't been created yet, the globs are expanded to themselves.

You can see the same behavior with any glob that doesn't match anything. For example:

$ echo a*j
a*j

While when it does match:

$ touch abj
$ echo a*j
abj

Coming back to your case, because the files don't actually exist, the command you are running becomes:

touch '*/credits' '*/links' '*/notes'

If you create one of them, you can see that things change:

$ touch Psy/credits
$ touch */{credits,links,notes}
touch: cannot touch '*/links': No such file or directory
touch: cannot touch '*/notes': No such file or directory

Since we now have one file that does match the */credits glob, the file Psy/credits, that one works but the other two give errors.

The right way to do what you are attempting is something like this:

for d in */; do touch "$d"/{credits,links,notes}; done

Which results in:

$ tree
.
├── abj
├── Eng
│   ├── credits
│   ├── links
│   └── notes
├── IT
│   ├── credits
│   ├── links
│   └── notes
├── IT_workshop
│   ├── credits
│   ├── links
│   └── notes
├── LA
│   ├── credits
│   ├── links
│   └── notes
├── OS
│   ├── credits
│   ├── links
│   └── notes
├── OS_lab
│   ├── credits
│   ├── links
│   └── notes
├── Psy
│   ├── credits
│   ├── links
│   └── notes
├── Python
│   ├── credits
│   ├── links
│   └── notes
└── Python_lab
    ├── credits
    ├── links
    └── notes

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why am I not able to use relative path to open a file in CLion?

Why am I not able use round in an extension?

Why am I not able to use $this is an opencart helper?

Why am I able to use the no void or return value on this function

Why am I able to use Application as default bindings in ViewModelComponent with Hilt?

Why I am not able to use scrollToIndex method of FlatList in react native?

Why am I not able to use default method in interface?

Why am I not able to use stopwatch.Restart()?

Why am I not able to use promise in my basic react app?

Why am I not able to use Python 3 in a Crontab?

Why am I able to use Kernel singleton methods like `puts`?

Why am I not able to use chmod 000 for a folder?

Why i am not able to use setState Under GestureDetector

Why am I able to use datetime in pandas without importing datetime?

The touch event in my canvas is not working as intended and I am not able to identify the reason why it is not working

I am not able to use ipfs

I am not able to use the variables

Why I'm unable to use sum() method here, while I am able to use '+' operator?

Why I am not able to see the JAVA_HOME path on my MAC OS X 10.11?

Why am I not able to add a JPanel to a JPanel?

Why am I able to open this file?

Why am I not able to remove outliers?

Why i am able to change angular constant?

Why am I not able to drop a column in Vertica

Why am I not able to install JSON gem?

Why am I not able to open a VS project

Why am i not able to login a user?

Why am I not able to clone an object?

Why i am not able to create the Unique pointer