Bash mkdir and subfolders [duplicate] - bash

This question already has answers here:
How can I create nonexistent subdirectories recursively using Bash?
(4 answers)
Closed 6 years ago.
Why I can't do something like this? mkdir folder/subfolder/ in order to achive this I have to do:
mkdir folder
cd folder
mkdir subfolder
Is there a better way to do it?

You can:
mkdir -p folder/subfolder
The -p flag causes any parent directories to be created if necessary.

To create multiple sub-folders
mkdir -p parentfolder/{subfolder1,subfolder2,subfolder3}

FWIW,
Poor mans security folder (to protect a public shared folder from little prying eyes ;) )
mkdir -p {0..9}/{0..9}/{0..9}/{0..9}
Now you can put your files in a pin numbered folder. Not exactly waterproof, but it's a barrier for the youngest.

Related

What does ../../ located within a file path means? [duplicate]

This question already has answers here:
What is double dot(..) and single dot(.) in Linux?
(4 answers)
Closed 9 months ago.
I saw some shell scripts that cd into some file path that starts with an alias and have /../../ in between, for example:
$exampleroot/../../folder/subfolder/filename.zip
i understand that /../ means root path but what does this /../../ refer to? does it mean capturing all misc sub folders in between regardless of the folders name?
Thanks
.. means the parent directory, i.e. the one that contains $exampleroot. ../../ is therefore two levels up:
$ pwd
/tmp
$ mkdir -pv foo/bar
mkdir: created directory 'foo/bar'
$ cd foo/bar/
$ pwd
/tmp/foo/bar
$ cd ../..
$ pwd
/tmp
An example might help. Suppose $exampleroot is /one/two/three/four (and this is a directory). Then:
$exampleroot/.. is equivalent to /one/two/three
$exampleroot/../.. is /one/two
$exampleroot/../../folder is /one/two/folder
$exampleroot/../../folder/subfolder/filename.zip is /one/two/folder/subfolder/filename.zip
(Note: If /one/two/three/four is not a directory, then /one/two/three/four/.. is not a valid path. And there are some weird exceptions if there's something like a symbolic link in the path, because going up a level in the directory hierarchy is not the same as un-following a symbolic link.)

How to create folder/file that name ended with a dot in Windows? [duplicate]

This question already has answers here:
How to delete a folder that name ended with a dot (".")?
(10 answers)
Closed 5 years ago.
I wanna create a file that file name end with a dot in Windows.
use "mkdir a.", but i only create a folder named with "a"
And I just wonder how to write a function to create such directory, thanks
mkdir \\?\c:\test2.
this creates a folder ending with (.)
provide drive letter
Use an extra dot and a trailing backslash:
mkdir a..\
But I highly recommend not to do so. You'll mess the directories up instantly.

How do I copy a file in multiple folders in Ubuntu? [duplicate]

This question already has answers here:
How to copy a file to multiple directories using the gnu cp command
(22 answers)
Closed 6 years ago.
I have directory named "public" in every directory inside Documents.
I want to copy file named File.txt in every directory "public".
cp -r File.txt Documents/*/public/
doesn't seam to work. What should I do?
Well I'm very certain that using cp combined with xargs would do just what you want to do if you know how to use 'em... or you can just write a script to perform just that

Make multiple directories and put files into each one

I've been attempting to mkdir multiple directories and the touch a set of files into each one.
I'm basically trying to combine the following:
mkdir ./file_directory{1..10};
then in each directory
touch ./file_directory{1..2} file_name{1..10};
When I execute touch it creates a the file_name{1..10} under ./
I've been looking around. Please forgive me for I have sinned and am new to not only bash, but coding/scripting in general. I'm attempting to create these files to learn scripting. Although this is technically my first project, I'm going to
You need to put / between the directory and filename to create a file in the subdirectory:
mkdir file_directory{1..10}
touch file_directory{1..10}/file_name{1..10}

Create folder in same directory as shell script [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 8 years ago.
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?
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!
script_dir=$(dirname "$0")
$0 is the name of the running script, as entered on the command line (for example: ./bin/script)
If you want the full path:
script_dir=$(cd -P -- "$(dirname "$0")" && pwd -P)

Resources