Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir command for a list of filenames paths

I have txt file with content like this

/home/username/Desktop/folder/folder3333/IMAGw488.jpg
/home/username/Desktop/folder/folder3333/IMAG04f88.jpg
/home/username/Desktop/folder/folder3333/IMAGe0488.jpg
/home/username/Desktop/folder/folder3333/IMAG0r88.jpg
/home/username/Desktop/folder/folder3333/
/home/username/Desktop/folder/
/home/username/Desktop/folder/IMAG0488.jpg
/home/username/Desktop/folder/fff/fff/feqw/123.jpg
/home/username/Desktop/folder/fffa/asd.png
....

these are filenames paths but also paths of folders. The problem I want to solve is to create all folders that doesn't exist.

I want to call mkdir command for every folder that does not exist

How can I do this on easy way ?

Thanks

like image 606
Lukap Avatar asked Feb 13 '12 18:02

Lukap


People also ask

What is mkdir path?

The mkdir() function creates a new, empty directory whose name is defined by path. The file permission bits in mode are modified by the file creation mask of the job and then used to set the file permission bits of the directory being created.

How do I make multiple folders in one mkdir command?

You can create directories one by one with mkdir, but this can be time-consuming. To avoid that, you can run a single mkdir command to create multiple directories at once. To do so, use the curly brackets {} with mkdir and state the directory names, separated by a comma.

Does mkdir create file?

mkdir creates a file instead of directory.

Which of the following command is used to create the mkdir mkdir dir all?

The command that allows you to create directories (also known as folders) is mkdir .


1 Answers

This can be done in native bash syntax without any calls to external binaries:

while read line; do mkdir -p "${line%/*}"; done < infile

Or perhaps with a just a single call to mkdir if you have bash 4.x

mapfile -t arr < infile; mkdir -p "${arr[@]%/*}"
like image 66
SiegeX Avatar answered Oct 05 '22 11:10

SiegeX