Linux 用于文件名路径列表的 mkdir 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9265607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mkdir command for a list of filenames paths
提问by Lukap
I have txt file with content like this
我有这样的内容的txt文件
/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
我想为每个不存在的文件夹调用 mkdir 命令
How can I do this on easy way ?
我怎样才能以简单的方式做到这一点?
Thanks
谢谢
采纳答案by SiegeX
This can be done in native bash
syntax without any calls to external binaries:
这可以使用本机bash
语法完成,无需调用外部二进制文件:
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
或者也许只需一个电话,mkdir
如果你有bash 4.x
mapfile -t arr < infile; mkdir -p "${arr[@]%/*}"
回答by cha0site
How about...
怎么样...
for p in $(xargs < somefile.txt);
do
mkdir -p $(dirname ${p})
done
回答by anubhava
It can be done without loop also (provided input file not huge):
它也可以在没有循环的情况下完成(提供的输入文件不是很大):
mkdir -p $(perl -pe 's#/(?!.*/).*$##' file.txt)
回答by tripleee
xargs -n 1 dirname <somefile.txt | xargs mkdir -p
回答by 2r2w
If you have file "file1" with filenames you could try this oneliner:
如果您有带有文件名的文件“file1”,您可以试试这个oneliner:
cat file1 |xargs -I {} dirname "{}"| sort -u | xargs -I{} mkdir -p "{}"
Use of:
用于:
xargs -I{} mkdir -p "{}"
ensures that even path names with spaces will be created
确保甚至会创建带空格的路径名
回答by Miller
Using a perl one-liner and File::Path qw(make_path)
:
使用 perl one-liner 和File::Path qw(make_path)
:
perl -MFile::Path=make_path -lne 'make_path $_' dirlist.txt