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.]
Related Topics
Unix/Linux Ipc: Reading from a Pipe. How to Know Length of Data at Runtime
Vagrant - How to Mount The Virtualbox Shared Folder? ("Vboxsf" Not Available)
How to Delete 5 Lines Before and 6 Lines After Pattern Match Using Sed
Apache/Httpd /Var/Www/HTML/ .Cgi Scripts Throw 500 Internal Server Error
Linux Kernel Hardware Break Points
Tail Logback Log Files with Log Level Coloring in Linux Server
Why Does 'Ping' Not Timeout in Linux
How to Flush Stdout of a Running Process
Reading Entropy_Avail File Appears to Consume Entropy
Where Is G_Multi Configured in Beaglebone Black
How to Fetch The Logical Name of a Nic Card Given The Ip Address Associated with It
What Means "Atomic" System Call
Avoid Daemon Running in Dedicated CPU Cores
Touch Command Create Multiple Files (Different Names) Under One Directory
Error: You Must Install at Least One Postgresql-Client-<Version> Package