Home > Back-end >  Create file with same name as directory without typing the filename possibly with brace expansion or
Create file with same name as directory without typing the filename possibly with brace expansion or

Time:10-10

Let's say I have a directory called Navigation and inside that I want to make a file called Navigation.jsx.

Instead of doing touch Navigation/Navigation.jsx I'm trying to figure out if there is a trick to not have to type Navigation twice, such as brace expansion.

I tried stuff like touch Navigation/{,.jsx} and touch Navigation/{/,.jsx} but rather than removing the slash it only produces a file called .jsx.

When doing this many times for multiple components it gets really monotonous and I'd love a streamlined way of doing it. Hey, maybe I'm thinking about this all wrong and there's a different flow I should use to create folders and files.

CodePudding user response:

Here's what I did. Questions and comments welcome.

#!/usr/bin/env bash

if [ $# -gt 0 ]; then
    for arg in "$@"; do

        rfc="const $arg = () => {
    return (
        <div>
            $arg 
        </div>
    )
}

export default $arg 
"

        mkdir "$arg"/
        touch "$arg"/"$arg".jsx
        echo "$rfc" >>"$arg"/"$arg".jsx
        echo "Component $arg created at $arg/$arg.jsx"
    done

else
    echo "Enter component names as arguments"

fi

CodePudding user response:

If you put the directory names in a file, you could use a read while loop.

#!/bin/sh -x

find . -type d | sed '1d' | sed s'@^..@@g' > stack
while read n
do
mkdir "${n}"
touch "${n}"/"${n}"
done < stack
  • Related