在其中创建目录和文件的一个命令 linux 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17986615/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:25:43  来源:igfitidea点击:

One command to create a directory and file inside it linux command

linuxcommand-line

提问by chanchal118

Suppose my current directory is A. I want to create a directory Band a file "myfile.txt" inside B.

假设我当前的目录是A。我想创建一个目录和文件“myfile.txt的”内部

How to do that in one command from Terminal?

如何在终端的一个命令中做到这一点?

Edit:

编辑:

Directory can be nested multiple times. Like I may want to create B/C/Dand then "myfile.txt" inside that. I do not also want to repeat the directory part.

目录可以嵌套多次。就像我可能想创建B/C/D然后在里面创建“myfile.txt”。我也不想重复目录部分。

Following command will create directory at any level.

以下命令将在任何级别创建目录。

mkdir -p B/C/D 

and

mkdir -p B/C/D && touch B/C/D/myfile.txt

will create the directory and the file. But I do not want to repeat the directory part after the touchcommand. Is that possible?

将创建目录和文件。但我不想在touch命令后重复目录部分。那可能吗?

采纳答案by devnull

mkdir B && touch B/myfile.txt

Alternatively, create a function:

或者,创建一个函数:

mkfile() { mkdir -p -- "" && touch -- ""/"" }

Execute it with 2 arguments: path to create and filename. Saying:

使用 2 个参数执行它:要创建的路径和文件名。说:

mkfile B/C/D myfile.txt

would create the file myfile.txtin the directory B/C/D.

myfile.txt在目录中创建文件B/C/D

回答by chandan

This might work:

这可能有效:

mkdir {{FOLDER NAME}}
cd {{FOLDER NAME}}
touch {{FOLDER NAME}}/file.txt

回答by Purkhalo Alex

For this purpose, you can create your own function. For example:

为此,您可以创建自己的函数。例如:

$ echo 'mkfile() { mkdir -p "$(dirname "")" && touch "" ;  }' >> ~/.bashrc
$ source ~/.bashrc
$ mkfile ./fldr1/fldr2/file.txt

Explanation:

解释:

  • Insert the function to the end of ~/.bashrcfile using the echocommand
  • The -pflag is for creating the nested folders, such as fldr2
  • Update the ~/.bashrcfile with the sourcecommand
  • Use the mkfilefunction to create the file
  • ~/.bashrc使用echo命令将函数插入到文件末尾
  • -p标志用于创建嵌套文件夹,例如fldr2
  • ~/.bashrc使用source命令更新文件
  • 使用mkfile函数创建文件

回答by Ilya Zarembsky

devnull's answerprovides a function:

devnull 的回答提供了一个函数:

mkfile() { mkdir -p -- "" && touch -- ""/"" }

This function did not work for me as is (I'm running bash 4.3.48 on WSL Ubuntu), but did work once I removed the double dashes. So, this worked for me:

这个功能对我不起作用(我在 WSL Ubuntu 上运行 bash 4.3.48),但是一旦我删除了双破折号就起作用了。所以,这对我有用:

echo 'mkfile() { mkdir -p "" && touch ""/"" }' >> ~/.bash_profile
source ~/.bash_profile
mkfile sample/dir test.file

回答by Rodrigo Andrade

Just a simple command below is enough.

下面一个简单的命令就足够了。

mkdir a && touch !$/file.txt

Thx

谢谢

回答by user2073100

You could create a function that parses argument with sed;

您可以创建一个解析参数的函数sed

atouch() {
  mkdir -p $(sed 's/\(.*\)\/.*//' <<< ) && touch 
}

and then, execute it with one argument:

然后,用一个参数执行它:

atouch B/C/D/myfile.txt

回答by Damon

you can install the script ;

你可以安装脚本;

pip3 install --user advance-touch

After installed, you can use ad command

安装后,您可以使用 ad 命令

ad airport/plane/captain.txt
airport/
├── plane/
│   ├── captain.txt