How to create multiple files with the Terminal?
You can do this with these commands:
mkdir learning_c
cd learning_c
touch bspl{0001..0003}.c
Explanation:
mkdir learning_c
- This will create a folder called
learning_c
in the current folder - The current folder usually is your home folder also called
~
- You can change the current directory using the
cd
command (i.e.cd Desktop
)
- This will create a folder called
cd learning_c
- Yes, you can guess it, you're entering on the newly created folder
touch bspl{0001..0003}.c
touch
is a tool to create empty files and modify timestamps; we're creating empty files.touch myfile
will create an empty file calledmyfile
.The ugly code that follows (
bspl{0001..0003}.c
) is called a brace expansion. This is a great feature of thebash
shell that allows you to create long lists of arbitrary string combinations. You can learn more about this in the Bash Hackers Wiki. In this case you will be making a long list of parameters that will be passed totouch
. You can also use its long equivalent:touch bspl0001.c bspl0002.c bspl0003.c
You can change the number of files: if you want 12 files, you can run
bspl{0001..0012}.c
.- The leading zeros (
0012
instead of12
) make sure that the output uses zero-padded 4 digits.
You can use the following python code, you can modify it to fit your needs.
Save the following code with filename filecreator.py
#!/usr/bin/env python
import os
import subprocess
work_path = os.path.abspath(os.path.dirname(__file__))
if not os.path.exists("learning_c"):
os.mkdir("learning_c")
os.chdir(os.path.expanduser(work_path+"/learning_c"))
n = 10 #put the number as you wish
for i in range(n):
subprocess.call(['touch', "bsdl"+str(i).zfill(4)+".c"])
And then execute it with this command:
python filecreator.py
Create correctly numbered (next) file with a shortcut key combination
Why create all files at once? The disadvantage is that you will have a lot of empty and unused files. What I am actually using: press a key combination to:
- have a script see in my code directory what should be the "next" file,
- create the correctly named file (including shebang) and
- open the new file in my editor (in my case Idle).
All in one keypress. That way you prevent a lot of (still) unused files; The files are only created if you need them.
A simplified version below (not running step 3). On every keypress, it will create a correctly numbered file like:
bspl0001.c, bspl0002.c, bspl0003.c etc
#!/usr/bin/env python3
import os
#--- set your code directory below
dr = "/path/to/your/coding_files"
#--- set the desired (base) name extension and shebang below (leave it ""if you don't want an automatically set shebang)
name_initial = "bspl"
extension = ".c"
shebang = ""
#---
existing = os.listdir(dr)
n = 1
while True:
file = dr+"/"+name_initial+str(n).zfill(4)+extension
if os.path.exists(file):
n = n+1
else:
with open(file, "wt") as out:
out.write(shebang)
break
How to use
- Copy the script into an empty file
- In the head section, set the path to your directory (and optional: change the base name and/or extension, shebang).
- Save the script as create_empty.py
Run the script from a shortcut: System Settings > Keyboard > Custom Shortcuts. Add the command:
python3 /path/to/create_empty.py