linux - 将文件夹移动到 unix 中按字母顺序排列的子目录

标签 linux unix subdirectory mv

给定一个包含公司名称的文件夹:

Ace inc
Austerity
A to Z
Beeline Industries
Bing
Booze inc.
Crypto
....
Zeebra 

...我需要将所有 A* 文件夹移动到子目录 A:

A/Ace inc
A/Austerity
A/A to Z
...

我尝试了以下命令:mv A*/A/ 一直运行到某个点,然后失败,因为它试图将新创建的 A/ 移动到自身中.

此外 - 我觉得已经运行过一次这个命令,我可能把我们的文件系统搞得一团糟。

最佳答案

您想在开始移动之前创建一个文件名列表。你不应该写信给 /...除非您以 super 用户root 身份运行,所以你不应该乱搞任何东西(你很可能把包含原始文件的子目录搞得一团糟......)

要在当前目录中创建一个文件列表并逐一循环,您可以简单地使用for fname in *; do ... done。 .您将需要引用所有后续使用的 fname以防止分词。由于您还标记了您的问题 shell而不是 bash等,您将需要使用符合 POSIX 的方式来获取每个文件名的第一个字符 ( fc )。为此,您可以使用旧的 expr substr "$fname" 1 1在命令替换中,例如

    fc=$(expr substr "$fname" 1 1)  ## use 'expr substr ...' to get 1st char

将它完全放在一个 POSIX 兼容的 shell 脚本中,该脚本只对当前目录中的文件进行操作,根据文件名的第一个字符将当前目录中的所有文件移动到适当的子目录中,您可以这样做:

#!/bin/sh

for fname in *; do                  ## loop over each file
    [ -d "$fname" ] && continue     ## skip all directory names
    fc=$(expr substr "$fname" 1 1)  ## use 'expr substr ...' to get 1st char
    mkdir -p "$fc" || exit 1        ## create the directory or exit
    mv "$fname" "$fc" || exit 1     ## move the file or exit
done

生成的目录结构

使用您在示例列表中提供的文件名将产生以下目录结构:

$ tree .
.
├── A
│   ├── A to Z
│   ├── Ace inc
│   └── Austerity
├── B
│   ├── Beeline Industries
│   ├── Bing
│   └── Booze inc.
├── C
│   └── Crypto
└── Z
    └── Zeebra

如果您在修复文件系统方面需要帮助,您需要在其他 StackExchange 站点上提出该问题 Super UserUnix & Linux因为它与“编程”无关。

如果你有bash或其他一些支持字符串索引作为参数扩展的 shell,然后使用它来获取第一个字符会更快,并且避免使用 $(expr ...) 进行命令替换所需的子 shell

如果您对将文件移动到子目录有进一步的疑问,请告诉我。

根据不移动当前目录中的文件的说明进行编辑

好的,如果我们达成共识 (1) 您正在运行 bash作为您的 shell,(2) 您不想移动当前目录中子目录下的文件,并且 (3) 只想移动工作目录中包含的目录 (及其内容)在从目录名称的第一个字符创建的目录下,您可以在下面进行以下更改:

#!/bin/bash

for fname in *; do                  ## loop over each file
    [ -f "$fname" ] && continue     ## skip files in the current dir
    fc=${fname:0:1}                 ## use parameter expansion for 1st char
    mkdir -p "$fc" || exit 1        ## create the directory or exit
    [ "$fc" = "$fname" ] && continue    ## don't move dir into itself
    mv "$fname" "$fc" || exit 1     ## move the directory or exit
done

原始目录结构

您的脚本在当前目录和 Apples 的附加目录中的示例和 Cans包含需要移动到 'A' 下方的文件和 'C'目录分别为:

$ tree .
.
├── A
│   ├── A to Z
│   ├── Ace inc
│   └── Austerity
├── Apples
│   ├── file1
│   └── file2.3
├── B
│   ├── Beeline Industries
│   ├── Bing
│   └── Booze inc.
├── C
│   └── Crypto
├── Cans
│   ├── file1
│   ├── file2
│   └── file3
├── Z
│   └── Zeebra
└── script.sh

使用示例

$ bash script.sh

生成的目录结构

$ tree .
.
├── A
│   ├── A to Z
│   ├── Ace inc
│   ├── Apples
│   │   ├── file1
│   │   └── file2.3
│   └── Austerity
├── B
│   ├── Beeline Industries
│   ├── Bing
│   └── Booze inc.
├── C
│   ├── Cans
│   │   ├── file1
│   │   ├── file2
│   │   └── file3
│   └── Crypto
├── Z
│   └── Zeebra
└── script.sh

仔细检查一下,如果我们就您需要完成的工作达成一致意见,请告诉我。

关于linux - 将文件夹移动到 unix 中按字母顺序排列的子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55911860/

相关文章:

c# - 我如何访问 C :\Program Files\in c#

Python Setuptools 构建 RPM 错误

linux - 从aws ec2到ec2的无密码root登录

mysql - 如何使用 ssh 从命令行执行 init 脚本

java - 用于搜索/查找的目录路径中正则表达式中的通配符?

wordpress - htaccess : Redirect entire subfolder to root

linux - libusb 批量传输的最大长度

linux - Perl:杀死任何 child 的整棵树

c - 许多子进程与单亲通信

unix - 在执行 `npm install -g` 之后,我无法使用已安装软件包中的 CLI 命令