Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create folder in same directory as shell script [duplicate]

I'm new to Shell Scripting:

I'm trying to create a script that asks for user input as to the folder name, finds the directory the script is being run from, and creates a folder within that same directory with a bunch of files (I can do that part)

My issue is I can't seem to figure out how to make the script find its current directory without explicitly spelling it out.

I want to be able to run it from anywhere and have it create the folder right next to it without the user having to path it from the home folder every time.

Could someone help with this?


1 Answers

You could try this one liner:

DIR="$( cd "$( dirname "$0" )" && pwd )

Will leave you with a $DIR variable that contains the full path to the current directory. See this answer for more information!

like image 87
binaryatrocity Avatar answered Nov 21 '25 08:11

binaryatrocity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!