I have 4 directories: dir1 dir2 dir3 dir4. I want to create a file in each directory. I know I can do this: touch file1.txt and then move it to dir1, then touch file2.txt then move it to dir2 but it takes ages. How can I create a function that creates a text file in each of the directories? Thanks
You don't need to create and then move files - you can
touch
them in place ex.It won't really save you any time (or typing) but you can of course create a shell function for this - a minimal implementation would be
which you can invoke as
although of course it should really include some checking / error handling (such as what to do if
$1
is not a directory, or doesn't exist).If your file and directory structure is always of the form
dirN/fileN.txt
then there are some better ways to automate the creation such asor even (using GNU
parallel
and brace expansion)Not sure what you regard as 'ages'. There's no function that would create 4 files in parallel for you. Assuming you're using bash or similar the 'easiest' way is probably a loop. e.g.:
That's pretty quick on any machine I use. If you want an atomic 4 file create, I'm afraid there's no real way to do this.
Note that if there's content in the 4 files, the 'quickest' way in terms of lag between the first file appearing and the last one would be create the files elsewhere and then
mv
them.