Bash make and open directory [duplicate] - bash

This question already has answers here:
One command to create and change directory
(9 answers)
Closed 7 years ago.
Let's say I have the following command:
mkdir directory && cd directory
I normally do this a lot during the day so I'm wondering if there is a simpler shorter way of doing this.
Does anybody know?

you can call last argument by &_
mkdir directory && cd $_
this is result
system:/tmp # mkdir directory && cd $_
system:/tmp/directory #

Put the following code in your ~/.bashrc or ~/.zshrc :
mkcd () {
mkdir "$1"
cd "$1"
}
Then in your shell, enter the following command mkcd foo. As you can see, this function need one argument which are the name of the directory.

Related

How to use mkdir for nested directories [duplicate]

This question already has answers here:
How to manually expand a special variable (ex: ~ tilde) in bash
(19 answers)
Closed last month.
I have the following variable that shows where I should write my report to
REPORT="ZFS(~/Q4Y22/report/q4y22.md)"
I have used sedto extract the path and dirname command to get the
~/Q4Y22/report/dir name which I assign to reportdir variable.
However when I run the following test, instead of the Q4Y22 dir being created in my home dir, it literally creates the following directory hierachy ~/Q4Y22/report inside my home for example farai/~/Q4Y22/report instead of farai/Q4Y22/report
if [ -d $reportdir ]
then
echo "dir exists, keep moving"
else
mkdir -p $reportdir
fi
How do I get around this, I tried using sed to remove the tilde from the path but i was wondering if there is a more clever way
My try on this:
#!/bin/bash
REPORT="~/Q4Y22/report/q4y22.md"
reportdir=$(dirname "${REPORT/'~'/$HOME}")
if [ -d "$reportdir" ]
then
echo "dir exists, keep moving"
else
mkdir -p "$reportdir"
fi
This is a bit fragile since you could have ~ in the filename, though, but you could improve this script to replace only the first char if it's a tilde.

Change directory in script bash [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Find file then cd to that directory in Linux
(12 answers)
Make a Bash alias that takes a parameter?
(24 answers)
Closed 2 years ago.
I want to find a file and goto his directory. I made a bash script :
#!/bin/bash
FILE=$1
FILEPATH=`find . -name "$FILE"`
if [ -f "$FILEPATH" ]
then
cd $(dirname "$FILEPATH")
fi
But this script does not work. I saw on this post that I have to add exec bash or $SHELL but it create a new bash prompt ans display my welcome message.
How can I do ? I just want a script, alias or something to find a file and go to the directory containing that file.
Source your script instead of running it like you do. When you run it like you do, you spawn a new shell that executes the cd, completes succesfully, closes the shell and returns to your current shell, leaving you in your pwd.
Use source myscript.sh or . myscript.sh instead of bash myscript.sh or myscript.sh.

Bash script to change directory based on a string in the directory name [duplicate]

This question already has answers here:
How to go to each directory and execute a command?
(12 answers)
Closed 5 years ago.
I'm currently trying to write a script which will cd to a directory and execute a python script if it matches a predefined string to the directory name. I need the code to change to a directory with the string 'batch' in it and execute the script. The most useful tool would probably be the case statement, but I can't get it to work for some reason. I'm fairly new to bash scripting, so i'm not really sure why this isn't working.
Here's my code:
#!/bin/bash
dir_string="batch"
workingdir=$PWD
for dir in *;do
echo "$dir"
if [[-d "$dir"]]; then
case "$dir_string" in
$dir)
cd $workingdir/$dir
for i in *;do
execute python script
done
;;
*)
echo "Can't find appropriate directories."
;;
esac
fi
done
Any help is appreciated!
What you are trying to achieve with your script, can be achieve with find:
#!/bin/bash
dir_string="batch"
workingdir=$PWD
for dir in $(find "$workingdir" -type d|grep $dir_string)
do
(cd "$dir" && execute python script)
done

Get parent directory of shell script's directory [duplicate]

This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 6 years ago.
I have a shell script here:
/node_modules/.bin/exec.sh
in the exec.sh script, I want to obtain the path of the directory's parent directory that the script is contained in (not pwd/cwd!). I can obtain the containing directory like so:
`dirname $0`
which will yield:
/node_modules/.bin
but I am looking to get at one directory higher, I just want to get
/node_modules
I am having trouble searching for the answer, my guess is:
`dirname $1`
but just a guess, not sure if that's right at all. Can anyone give an explanation of how to do this and how it works?
Run dirname twice (nested).
~$ dirname $PWD
/home
~$ dirname `dirname $PWD`
/
~$
I believe the answer is
$(dirname $(dirname "$0"))
don't forget about the double-quotes around "$0"

Unix command to make file and folders recursively [duplicate]

This question already has answers here:
Unix - create path of folders and file
(11 answers)
Closed 12 months ago.
I know mkdir -p will make directories recursively.
I know touch will create a file recursively.
I know mkdir -p foo/bar; touch foo/bar/baz.txt will work, but is there a flag or something for touch so I can one-step this?
I'm sure this question has been asked before a million times but for some reason I'm coming up empty.
what about an alias or a function:
function my_touch {
mkdir -p $(dirname $1) && touch $1
}
my_touch /tmp/a/b/aaa ; ls -l /tmp/a/b/aaa
function mytouch { for x in "$#"; do mkdir -p -- `dirname -- "$item"` && touch -- "$x"; done }
usage:
mytouch aa/bb/cc/dd.txt --a/b/c/d.txt -a/b/c/d.txt
$ ls -- aa/bb/cc/dd.txt --a/b/c/d.txt -a/b/c/d.txt
--a/b/c/d.txt -a/b/c/d.txt aa/bb/cc/dd.txt
The GNU implementation of install can do this:
install -D /dev/null foo/bar/baz.txt
# will create an empty baz.txt file in foo/bar
If you're using OS X without coreutils you have to use functions, like already suggested

Resources