Search code examples
linuxbashgitshellclone

Git clone repository in a specific folder but keep default folder name


So I'm using the git clone command but when I try to clone the repository into a specific folder it doesn't create a folder like it does normally. For example when I use it like this

git clone https://github.com/username/repositoryName.git

it creates a folder named repositoryName and stores the repository in there. When I'm using it like this

git clone https://github.com/username/repositoryName.git myFolderName

it doesn't create that default folder it would create. It just saves it in myFolderName. The thing is that I want it stored in that default folder but I want that default folder in myFolderName. I can't use mv command because I don't know the defaults folder name and I clone a lot of repositories at the same time. Any ideas?

Thanks in advance.


Solution

  • What about something like this:

    ## Your links in an array
    declare -a arr=("https://github.com/username/repositoryName" "https://github.com/username/repositoryName2")
    
    ## Folder to store each of these git repos
    folder=myFolderName
    
    ## Go through each link in array
    for i in "${arr[@]}"
    do
        ## Use basename to extract folder name from link
        git clone $i $folder/$(basename $i)
    done
    

    Which will produce your following git repositories:

    myFolderName/repositoryName
    myFolderName/repositoryName2
    

    Note: You don't need to add .git at the end of each HTTPS link for git clone.

    If you need to include .git for some reason, you can strip it with:

    git clone $i $folder/$(basename ${i%.*})