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
Related
New to bash scripting, and I'm stuck. Within a static directory I'm trying to create a folder '001_scanned name' and within that directory create 6 more subfolders. I'm able to do it all brutishly with this code:
cd ~/deej/Test/Capture
mkdir "$1"
cd "$1"
mkdir "$1_1"
mkdir "$1_2"
mkdir "$1_3"
mkdir "$1_4"
mkdir "$1_5"
mkdir "$1_6"
Ugly, but works for now.
$1 is the scanned name and I was manually appending the prefix of the file names with "001, 002, etc.". Is there an easy way to do this within an Automator prompt since I'll be unable to keep the last variable stored in the code?
If you use the -p option with mkdir it will create intermediate directories as needed so you can do both commands at once. The seq function can create zero-padded integers:
for n in $(seq -f "%03g" 1 10);
do mkdir -p ${i}/${i}_${n};
done;
You can test using echo instead of mkdir -p. Below I had i set to 015...
015/015_001
015/015_002
015/015_003
015/015_004
015/015_005
015/015_006
015/015_007
015/015_008
015/015_009
015/015_010
This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 3 years ago.
I'm fairly new to bash scripting.
I'm trying to create a directory with a timestamp and then cd into the directory.
I'm able to create a directory and can cd into it the directory with one command.
mkdir "build" && cd "build"
I can create the directory with data then cd into it.
mkdir date '+%m%d%y' && cd date '+%m%d%y' I am able to create this dir and cd into it with one command.
Here is my bash script:
#!/bin/bash
# mkdir $(date +%F) && cd $(date +%F)
mkdir build_`date '+%m%d%y'` && cd build_`date '+%m%d%y'`
I need to create the directory with build_date '+%m%d%y' in the title then cd into that folder.
I've looked online but am unable to come up with a solution.
Thank you.
Try this:
#!/bin/bash
build_dir="build_$(date '+%m%d%y')"
mkdir $build_dir && cd $build_dir
Best is to name your Folders YYYY.MM.DD like build_2019.11.21
#!/bin/bash
DIRECTORY="build_$(date '+%Y.%m.%d')"
if [ ! -d "$DIRECTORY" ]; then
# checking if $DIRECTORY doesn't exist.
mkdir $DIRECTORY
fi
cd $DIRECTORY
touch testfile
Here's a script that uses /usr/bin/env bash and the builtin command printf instead of /bin/date.
#!/usr/bin/env bash
date="$(printf '%(%m%d%y)T' -1)"
[[ -d build_"${date}" ]] || mkdir build_"${date}"
cd build_"${date}"
# do something useful here
# ...
However, changing directory in a script (subshell) is not much use on its own, as your current working directory ($PWD) will be the same after the script exits.
If you want to have the current shell session change into the new directory, use a shell function.
mkbuild() {
date="$(printf '%(%m%d%y)T' -1)"
[[ -d build_"${date}" ]] || mkdir build_"${date}"
cd build_"${date}"
}
Then run it:
$ echo $PWD
/tmp
$ mkbuild
$ echo $PWD
/tmp/build_112119
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.
What is a better way to create sub folders in a shell script? Instead of using the following method?
mkdir /var/log
mkdir /var/log/celery
mkdir /var/log/celery/stdout
mkdir /var/log/celery/stderr
touch /var/log/celery/stdout/stdout.log <<< I'm hoping the use this path create folder if doesn't exists....
touch /var/log/celery/stderr/stderr.log
mkdir has a -p flag that will create parent directories but touch will not create directories that do not exist.
That still cuts the above down to:
mkdir -p /var/log/celery/stdout /var/log/celery/stderr
touch /var/log/celery/stdout/stdout.log /var/log/celery/stderr/stderr.log
Which in a shell that supports brace expansion could even be:
mkdir -p /var/log/celery/{stdout,stderr}
touch /var/log/celery/{stdout/stdout.log,stderr/stderr.log}
And actually, if you have brace expansion but not mkdir -p you could do:
mkdir /var/log{,/celery{,/{stdout,stderr}}}
touch /var/log/celery/{stdout/stdout.log,stderr/stderr.log}
But there isn't any way to combine the mkdir and touch steps with standard tools that I'm aware of.
The -p option of mkdir will create the intermediate folders of the path if they don't exists (and of course, if you have the appropriate privileges):
mkdir -p /var/log/celery/stderr
To create the file, you can append the touch after the operator &&, so the touch operation only occurs if the directory either was created successfully or already exists:
mkdir -p /var/log/celery/stderr && touch "$_/stderr.log"
(Basically, the $_ will pass the dir path to the touch command)
UNTESTED:
$ needir () { mkdir -p $1; echo $1; }
$ touch $(needir /var/log/celery/stderr)/stderr.log
and put "needir" in your .profile, or better yet, in a function library on your path that you source when you login. you'd be surprised how often you'll be using it.
This question already has answers here:
How to mkdir only if a directory does not already exist?
(17 answers)
Closed 8 years ago.
In my bash script I do:
mkdir product;
When I run the script more than once I get:
mkdir: product: File exists
In the console.
So I am looking to only run mkdir if the dir doesn't exist. Is this possible?
Do a test
[[ -d dir ]] || mkdir dir
Or use -p option:
mkdir -p dir
if [ ! -d directory ]; then
mkdir directory
fi
or
mkdir -p directory
-p ensures creation if directory does not exist
Use mkdir's -p option, but note that it has another effect as well.
-p Create intermediate directories as required. If this option is not specified, the full path prefix of each oper-
and must already exist. On the other hand, with this option specified, no error will be reported if a directory
given as an operand already exists. Intermediate directories are created with permission bits of rwxrwxrwx
(0777) as modified by the current umask, plus write and search permission for the owner.
mkdir -p
-p, --parents
no error if existing, make parent directories as needed
Try using this:-
mkdir -p dir;
NOTE:- This will also create any intermediate directories that don't exist; for instance,
Check out mkdir -p
or try this:-
if [[ ! -e $dir ]]; then
mkdir $dir
elif [[ ! -d $dir ]]; then
echo "$Message" 1>&2
fi