Touch Command Create Multiple Files (Different Names) Under One Directory

touch command create multiple files (different names) under one directory

I suggest to add ,:

mkdir -p 1/2 && touch 1/2/{a,b,c}

How to touch multiple files, each with unique filenames, files to be created from outside the current directory. Using Git Bash CLI

Fiddled around and got it. If there is a shorter way without repeating the file path please advise. Thx

touch views/register.ejs & touch views/secret.ejs

Create multiple files with same names in linux

You can do this without the loop by using brace expansion in the file name:

touch text{1..8}.txt

See brace expansion in the bash man pages. Unlike wildcard expansion, the file names do not have to exist for brace expansion.

Ansible create multiple directories and files within each directory

Create the structure

  my_files: |
{% for dir in directories %}
- [{{ dir.directory}}, directory]
{% for file in dir.static_files.files %}
- [{{ dir.directory }}/{{ file }}, touch]
{% endfor %}
{% endfor %}

gives

  my_files: |-
- [dir1, directory]
- [dir1/file1, touch]
- [dir1/file2, touch]
- [dir2, directory]
- [dir2/file1, touch]
- [dir2/file2, touch]

The iteration of the list

    - file:
path: "/tmp/{{ item.0 }}"
state: "{{ item.1 }}"
loop: "{{ my_files|from_yaml }}"

creates the directories and the files

TASK [file] ***************************************************************
changed: [localhost] => (item=['dir1', 'directory'])
changed: [localhost] => (item=['dir1/file1', 'touch'])
changed: [localhost] => (item=['dir1/file2', 'touch'])
changed: [localhost] => (item=['dir2', 'directory'])
changed: [localhost] => (item=['dir2/file1', 'touch'])
changed: [localhost] => (item=['dir2/file2', 'touch'])
shell> tree /tmp/dir1
/tmp/dir1
├── file1
└── file2

0 directories, 2 files
shell> tree /tmp/dir2
/tmp/dir2
├── file1
└── file2

0 directories, 2 files

This task is not idempotent. When you run it again the files will be touched and both the access and modification time will be updated. Set the parameters access_time and modification_time to 'preserve' if you don't want to change the times

  my_files: |
{% for dir in directories %}
- [{{ dir.directory}}, directory]
{% for file in dir.static_files.files %}
- [{{ dir.directory }}/{{ file }}, touch, preserve, preserve]
{% endfor %}
{% endfor %}

gives

  my_files: |-
- [dir1, directory]
- [dir1/file1, touch, preserve, preserve]
- [dir1/file2, touch, preserve, preserve]
- [dir2, directory]
- [dir2/file1, touch, preserve, preserve]
- [dir2/file2, touch, preserve, preserve]

Make the parameters access_time and modification_time optional

    - file:
path: "/tmp/{{ item.0 }}"
state: "{{ item.1 }}"
access_time: "{{ item.2|d(omit) }}"
modification_time: "{{ item.3|d(omit) }}"
loop: "{{ my_files|from_yaml }}"

This makes the task idempotent

TASK [file] ***************************************************************
ok: [localhost] => (item=['dir1', 'directory'])
ok: [localhost] => (item=['dir1/file1', 'touch', 'preserve', 'preserve'])
ok: [localhost] => (item=['dir1/file2', 'touch', 'preserve', 'preserve'])
ok: [localhost] => (item=['dir2', 'directory'])
ok: [localhost] => (item=['dir2/file1', 'touch', 'preserve', 'preserve'])
ok: [localhost] => (item=['dir2/file2', 'touch', 'preserve', 'preserve'])

Example of a complete playbook for testing

- hosts: localhost

vars:

directories:
- directory: dir1
static_files:
files:
- file1
- file2
- directory: dir2
static_files:
files:
- file1
- file2

my_files: |
{% for dir in directories %}
- [{{ dir.directory}}, directory]
{% for file in dir.static_files.files %}
- [{{ dir.directory }}/{{ file }}, touch, preserve, preserve]
{% endfor %}
{% endfor %}

tasks:

- debug:
var: my_files

- file:
path: "/tmp/{{ item.0 }}"
state: "{{ item.1 }}"
access_time: "{{ item.2|d(omit) }}"
modification_time: "{{ item.3|d(omit) }}"
loop: "{{ my_files|from_yaml }}"

How to simultaneously create a new folder and multiple files in VSCode?

I don't think you can do it the way you showed, but it is pretty easy to do it with a task. In your tasks.json:

{
"version": "2.0.0",

"tasks": [
{
"label": "new react folder and files",

"command": "mkdir ${input:dirName} && touch '${input:dirName}/${input:dirName}.component.jsx' '${input:dirName}/${input:dirName}.styles.jsx'",

"type": "shell",
"problemMatcher": [],
"presentation": {
"echo": false,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": true
},
}
],

// ........................................................................................

"inputs": [

{
"type": "promptString",
"id": "dirName",
"description": "Complete my folder name",
"default": "jsx folder to create"
}
]
}

And some keybinding to trigger the task (in your keybindings.json):

[
{
"key": "alt+j",
"command": "workbench.action.tasks.runTask",
"args": "new react folder and files",
}
]

This will prompt for the directory name and then create the folder and two files within it.

[I used bash commands mkdir and touch to create the folder and files, if you are using a shell without those commands swap out the ones you have.]

create a react folder and files with a task



Related Topics



Leave a reply



Submit