How to move directory into a directory with the same name?

Stefan

I have a directory foo with several files:

.
└── foo
    ├── a.txt
    └── b.txt

and I want to move it into a directory with the same name:

.
└── foo
    └── foo
        ├── a.txt
        └── b.txt

I'm currently creating a temporary directory bar, move foo into bar and rename bar to foo afterwards:

mkdir bar
mv foo bar
mv bar foo

But this feels a little cumbersome and I have to pick a name for bar that's not already taken.

Is there a more elegant or straight-forward way to achieve this? I'm on macOS if that matters.

Kusalananda

To safely create a temporary directory in the current directory, with a name that is not already taken, you can use mktemp -d like so:

tmpdir=$(mktemp -d "$PWD"/tmp.XXXXXXXX)   # using ./tmp.XXXXXXXX would work too

The mktemp -d command will create a directory at the given path, with the X-es at the end of the pathname replaced by random alphanumeric characters. It will return the pathname of the directory that was created, and we store this value in tmpdir.1

This tmpdir variable could then be used when following the same procedure that you are already doing, with bar replaced by "$tmpdir":

mv foo "$tmpdir"
mv "$tmpdir" foo
unset tmpdir

The unset tmpdir at the end just removes the variable.


1 Usually, one should be able to set the TMPDIR environment variable to a directory path where one wants to create temporary files or directories with mktemp, but the utility on macOS seems to work subtly differently with regards to this than the same utility on other BSD systems, and will create the directory in a totally different location. The above would however work on macOS. Using the slightly more convenient tmpdir=$(TMPDIR=$PWD mktemp -d) or even tmpdir=$(TMPDIR=. mktemp -d) would only be an issue on macOS if the default temporary directory was on another partition and the foo directory contained a lot of data (i.e. it would be slow).

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Active Directory OSSA实施

How to get script directory in POSIX sh?

How to run Maven from another directory (without cd to project dir)?

在Linux中〜directory_name和/ directory_name有什么区别

.NET Directory.Move()与File.Move()

How to set the current working directory in the Python sh module?

Directory.Move无法正常工作于UNC路径

React Native Changed Directory Name Packager不再运行

使用`cd path / && zip -r name.zip directory /`是线程安全的

zip a directory that name start with `-`

为什么chmod 0000 directory-name不能清除目录中的特殊位?

<Directory />是做什么的?

Active Directory登录

当Directory.Move子文件夹和父文件夹时,IOException访问被拒绝

Directory.GetFilesNameWithoutExtension()数组

Is it possible to delete directory when in the directory in shell

How to open the same directory in another panel in Midnight Commander?

How to move and recreate a folder at the same time?

How to find a directory on linux?

How can I show the size of all files in my ~/.cache directory?

Active Directory VBScript问题

Jenkins 2.60.2 在运行 Gradle 任务时返回“CreateProcess error=267, The directory name is invalid”

无法在 open() 中写入名称为 directory/file_name 形式的文件

更改 Active Directory 域

One Drive 文件夹的 .NET Directory.Move() 失败

使用 Heat 工具采集 Wix 后无法修改 Directoryref/Directory 元素的 Name 属性值

Qt: How to create a directory in project file?

use tree command output to create the same directory tree structure in different place

How to create a file from directory structure