使用bash在其他文件夹中创建一个文件夹 [英] Create a folder inside other folders using bash

查看:49
本文介绍了使用bash在其他文件夹中创建一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一长串彼此为兄弟的文件夹,它们都以"0"开头,并用数字命名(001、002、003 ...),但名称不仅数值且不相关(例如,我有0010_foo,0032_bar,0150_baz等).我需要在列表中的每个文件夹内创建一个新文件夹(js).我想使用命令行递归地进行操作.

I have a long list of folders that are siblings to each other, they all start with "0" and are numerically named (001, 002, 003...) but names are not only numerical and are not correlative (for example I have 0010_foo, 0032_bar, 0150_baz, etc). I need to create a new folder (js) inside each of the folders on my list. I'd like to do it recursively using the command line.

我尝试过:

$ cd path/to/my/root/folder
$ find 0* -type d -exec mkdir js {} \;

但是每次尝试都会出现错误:"mkdir:js:文件存在".无需说我的文件夹中没有名为js的目录,但它们是扩展名为.js的文件.

But I get an error for each attempt: "mkdir: js: file exists". No need to say there's no directory named js inside my folders but they are files with .js extension.

我的命令中的错误在哪里,我该如何解决?谢谢!

Where is the error in my command and how can I fix it? Thanks!

推荐答案

在编辑后,我不确定您的目录结构是100%可靠,但是请稍稍试一下:

I'm not 100% sure of your directory structure after your edit, but give this a whirl:

cd /path/to/my/root/folder
find . -maxdepth 1 ! -path . -type d -exec mkdir -p {}/js \;

似乎可以正常工作:

$ cd /path/to/my/root/folder
$ tree
.
├── 001
│   └── js
└── 002
$ find . -maxdepth 1 ! -path . -type d -exec mkdir -p {}/js \;
.
├── 001
│   └── js
└── 002
    └── js

查找的功能:在当前目录(.)中,它找到子目录( -d )–除了当前目录本身(!-path.)和任何子子目录( -maxdepth 1 ).在找到的那些目录中,它将创建所需的子目录( -exec ... ). mkdir -p 部分创建目录,并使有关不存在的父项的任何错误保持沉默.find用找到的实际目录替换 {} 部分.

What this find does: In the current directory (.), it finds sub-directories (-type d) -- except the current directory itself (! -path .) and any sub-sub-directories (-maxdepth 1). In those found directories, it creates the desired sub-directory (-exec ...). The mkdir -p part creates the directory and silences any errors about parents not existing. find replaces the {} part with the actual directory it found.

这篇关于使用bash在其他文件夹中创建一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆