如何在一个文件夹中的文件夹内移动文件

时间:2018-12-10 21:04:05

标签: bash

我有1000个文件夹,在其中几个文件夹中我需要特定的文件。 他们的命名很疯狂,例如文件夹名称为0af1b217-6de0-48c6-ba62-6ffacdc07dce,其中的文件为d5a54e1d-02d7-4cee-b6b1-e0a616cc322d.txt

因此,我有一个文本文件,其中包含所需文件夹的名称和所需文件的名称。我想将它们移动到新文件夹中。

我只是想知道是否有人可以帮助我做到这一点。我基本上是想使用for循环来完成此操作,但我不知道如何遍历每个文件夹并取出文件

for i in list.txt; do
    mv -i -- "$i" "${i%.ttxt}"
done

列表看起来像这样

0af1b217-6de0-48c6-ba62-6ffacdc07dce 
0af1b218-6di0-48c6-ba62-6ffacdc07dme
Naf1b274-6de0-47c6-ba62-6ffacdc07die 
.
.
.

每个都是文件夹的名称

在每个文件夹中,有很多文件,我只想从其中取出.txt并将其放在文件夹中。如果更容易,我还可以在list.txt中添加要提取的文件的名称吗?

enter image description here

在这些文件夹中的每个文件夹中,有一个我想取出的.txt格式文件并将其移动到文件夹中。

错误是这样

user:GDC admin$ ./run4.sh list.txt
mv: rename b7a8cbc0-8ff5-4216-8a00-d7a238f22fcd\r/*.txt to ./*.txt: No such file or directory
mv: rename 7595d58e-f4ee-4fbb-a8d3-ebdc6c00ffa1\r/*.txt to ./*.txt: No such file or directory
mv: rename dfe14a8f-8c2c-41f5-9591-29a9dbf00db9\r/*.txt to ./*.txt: No such file or directory
mv: rename cc850b19-1d54-4794-a7c6-c6eaacd1dfc2\r/*.txt to ./*.txt: No such file or directory
mv: rename e8adda1d-86f5-45d1-bd7e-e9d1f1ce3075\r/*.txt to ./*.txt: No such file or directory
mv: rename 5af584ef-5dc4-42dd-b136-eff03f9e6a8c\r/*.txt to ./*.txt: No such file or directory
mv: rename f904505e-11d2-4ef0-8f14-7154a2f5d853\r/*.txt to ./*.txt: No such file or directory
mv: rename 29e99565-f4c0-4a55-acc7-4bf6cd0a5fd2\r/*.txt to ./*.txt: No such file or directory
mv: rename b35a74b2-1d85-4609-8b38-5f55a7322a85\r/*.txt to ./*.txt: No such file or directory
mv: rename c072f502-1ae8-42a7-ac80-3982f01586c6\r/*.txt to ./*.txt: No such file or directory
mv: rename 15270fb4-8f4e-4a26-a391-7d42576c46bb\r/*.txt to ./*.txt: No such file or directory
mv: rename 3b1b9bbd-c177-406a-b81c-d8baf1ba9257\r/*.txt to ./*.txt: No such file or directory
mv: rename a0a40207-1f02-4f1c-aa30-b39f97c91d55\r/*.txt to ./*.txt: No such file or directory
mv: rename 22280281-1e0d-459e-9690-0194836e7ebd\r/*.txt to ./*.txt: No such file or directory
mv: rename b97c966f-472e-4551-9fcd-19b76c1829d6\r/*.txt to ./*.txt: No such file or directory
mv: rename 28be129b-f449-4b46-81ba-7fef28d0d4b3\r/*.txt to ./*.txt: No such file or directory
mv: rename fad24504-b3d5-4ea1-b9d4-5a480c827188\r/*.txt to ./*.txt: No such file or directory
mv: rename ac05f0e1-be8f-4c7f-827c-e4a443f4d856\r/*.txt to ./*.txt: No such file or directory
mv: rename 068741d6-9334-4fc6-8310-7aefdc1e7b04\r/*.txt to ./*.txt: No such file or directory
mv: rename 8f3ffcf3-3257-4078-a362-cd5e8f4ed2d2\r/*.txt to ./*.txt: No such file or directory
mv: rename 2c5c6668-9ff0-4a1e-a89b-013f5f515dcf\r/*.txt to ./*.txt: No such file or directory
mv: rename 0738e26d-942f-4ce2-b0ed-f0bc7428d05e\r/*.txt to ./*.txt: No such file or directory
mv: rename fff2056e-1e1c-4d91-88b0-37e04679aeb5\r/*.txt to ./*.txt: No such file or directory
mv: rename ed425d32-b56f-4884-b54b-d49c5a73853c\r/*.txt to ./*.txt: No such file or directory
mv: rename 4962a8b1-717e-419d-9539-3c3e485f235d\r/*.txt to ./*.txt: No such file or directory
mv: rename 1b1975cc-db84-4c82-9f54-24e0876bf202\r/*.txt to ./*.txt: No such file or directory
mv: rename bcbb93da-dddf-4819-ae96-0ffbd9857744\r/*.txt to ./*.txt: No such file or directory
mv: rename 0bce4a69-81ee-43b6-8fe7-126c3c1a61fd\r/*.txt to ./*.txt: No such file or directory
mv: rename e7e1e726-bbd0-4d5d-adb2-3a3cbc0ce36e\r/*.txt to ./*.txt: No such file or directory
mv: rename 0dd39e9e-aa3e-4dc7-aa4d-e1abca583679\r/*.txt to ./*.txt: No such file or directory
mv: rename 4af4e64a-78f7-430a-a62e-08728ad76ffa\r/*.txt to ./*.txt: No such file or directory
mv: rename 1888e5bd-fd6b-48cb-a59e-46a6e4518d11\r/*.txt to ./*.txt: No such file or directory
mv: rename c8769309-dfac-4fa0-ac97-4b44edc9db7d\r/*.txt to ./*.txt: No such file or directory
mv: rename 7fd79bc2-bd6a-4845-9abd-18d77db964b0\r/*.txt to ./*.txt: No such file or directory
mv: rename 09ab195a-d931-4596-975b-3551100900f2\r/*.txt to ./*.txt: No such file or directory
mv: rename 41af5af9-2a4a-49c0-81a1-39ae9fd5417a\r/*.txt to ./*.txt: No such file or directory
mv: rename 34a7817d-afd1-4073-8173-ff86a284f6c9\r/*.txt to ./*.txt: No such file or directory
mv: rename 29942956-f9f9-42c1-a575-2e93a6f1ac03\r/*.txt to ./*.txt: No such file or directory
mv: rename a0f85380-dc8e-4424-985f-36c5d6fb861c\r/*.txt to ./*.txt: No such file or directory
mv: rename d3947906-9987-4ab7-bf67-fd96d5755b04\r/*.txt to ./*.txt: No such file or directory
mv: rename f36367cf-412a-4e76-8219-11937f3143a3\r/*.txt to ./*.txt: No such file or directory
mv: rename d513d991-4b52-4f84-af7c-dc2ad7fcd39f\r/*.txt to ./*.txt: No such file or directory
mv: rename fbc0b227-750b-4028-b00f-7da13fc09215\r/*.txt to ./*.txt: No such file or directory
mv: rename 87cb2067-679b-4cff-a264-d5e108ccb1b0\r/*.txt to ./*.txt: No such file or directory
mv: rename 532fd713-e9b0-4074-8fe9-3a652d5c71cb\r/*.txt to ./*.txt: No such file or directory
mv: rename 0d0638dc-3c4a-4218-83e2-34347e69e71a\r/*.txt to ./*.txt: No such file or directory
mv: rename ac069baa-07e2-4493-9d4c-ade21e3a25be\r/*.txt to ./*.txt: No such file or directory
mv: rename 03ae150d-02e7-4246-8d59-c558d28fc2c6\r/*.txt to ./*.txt: No such file or directory
mv: rename 247b1359-ccba-4636-aad3-f1788b033d67\r/*.txt to ./*.txt: No such file or directory
mv: rename 27e5c514-9c17-4279-9980-3da544a662a6\r/*.txt to ./*.txt: No such file or directory
mv: rename 18e284a1-0fb0-4e90-bdf5-a6d2464e26af\r/*.txt to ./*.txt: No such file or directory
mv: rename 0b611938-6433-4ca3-b4c9-774414c70b25\r/*.txt to ./*.txt: No such file or directory
mv: rename df298b13-5ac4-4089-9a1a-0b6471ab93a5\r/*.txt to ./*.txt: No such file or directory
mv: rename 6bc1eb63-0ae0-49f2-9777-9ca7bb261fe5\r/*.txt to ./*.txt: No such file or directory
mv: rename 776db67a-ddc4-4488-9406-9e961048f8bf\r/*.txt to ./*.txt: No such file or directory
mv: rename 9caae20a-f218-4e52-af7e-70f57ff710e3\r/*.txt to ./*.txt: No such file or directory
mv: rename 438e8278-e836-44bd-8225-e90053838f6b\r/*.txt to ./*.txt: No such file or directory
mv: rename 59e4c575-324d-4697-9609-2619e8927972\r/*.txt to ./*.txt: No such file or directory
mv: rename ec2d9440-bcf7-446a-a563-d2d4bb37e8a6\r/*.txt to ./*.txt: No such file or directory
mv: rename bfebe117-1fa5-40e9-9b4b-288300898a3b\r/*.txt to ./*.txt: No such file or directory
mv: rename 219c79e7-be38-4f9a-a392-8f3697f65877\r/*.txt to ./*.txt: No such file or directory
mv: rename 82a86f37-1b62-4485-9f86-1fa3a5e51f0b\r/*.txt to ./*.txt: No such file or directory
mv: rename ea83302f-0446-4a7f-bf65-dc86265f6e74\r/*.txt to ./*.txt: No such file or directory
mv: rename 2e0e3d3b-4ebd-4ebd-9f12-41626fc263de\r/*.txt to ./*.txt: No such file or directory
mv: rename d6ebfd02-7d77-47db-86ab-ee26cc43018e\r/*.txt to ./*.txt: No such file or directory
mv: rename a229c849-c790-4b41-a4fb-7ec86c309411\r/*.txt to ./*.txt: No such file or directory
mv: rename 89ccea22-f0f9-4601-b2c9-303bb83f6ae7\r/*.txt to ./*.txt: No such file or directory
mv: rename b3470ead-f401-4c8c-a8cd-6fa0252931cb\r/*.txt to ./*.txt: No such file or directory
mv: rename 88465767-638d-436f-b706-919646852f0a\r/*.txt to ./*.txt: No such file or directory
mv: rename f07c4b11-c455-458a-a390-f14be4f805a4\r/*.txt to ./*.txt: No such file or directory
mv: rename 465cda40-5705-4059-89b7-be7b3c736b3f\r/*.txt to ./*.txt: No such file or directory
mv: rename da9a0171-aad6-418e-91fe-c8af7babca53\r/*.txt to ./*.txt: No such file or directory
mv: rename 285cd8bc-2ed3-43d0-a2ec-d880dda6a306\r/*.txt to ./*.txt: No such file or directory
mv: rename d04be5e5-9c8e-46b2-9b7c-0de0b88bc2ea\r/*.txt to ./*.txt: No such file or directory
mv: rename 22583de0-d275-48db-926f-fff6f8f7ceb5\r/*.txt to ./*.txt: No such file or directory
mv: rename 444f69bd-417c-4b36-b1e7-8ee33ce9ae42\r/*.txt to ./*.txt: No such file or directory
mv: rename 24597e3e-3a92-4302-93e5-57b3821fbee8\r/*.txt to ./*.txt: No such file or directory
mv: rename cfa24f91-5cea-4a45-8a00-afbebaea462b\r/*.txt to ./*.txt: No such file or directory
mv: rename b7e1d1e4-44ea-4835-a79c-3403ee2497de\r/*.txt to ./*.txt: No such file or directory
mv: rename 43ec9f6b-fc9d-4d37-a6be-4bee07ff75b8\r/*.txt to ./*.txt: No such file or directory
mv: rename 85665189-ad34-4fb2-9739-6afaa1c7292f\r/*.txt to ./*.txt: No such file or directory
mv: rename 4bdfc4e9-2a42-49af-975d-855601d25bd6\r/*.txt to ./*.txt: No such file or directory
mv: rename 07488bb4-14b1-4433-9cca-4cfc37b16030\r/*.txt to ./*.txt: No such file or directory
mv: rename 52dd0c75-8148-4684-86e2-9e13ff3702e8\r/*.txt to ./*.txt: No such file or directory
mv: rename eb2d1d01-590d-452c-b421-5df874762121\r/*.txt to ./*.txt: No such file or directory
mv: rename b1cc1842-e490-4706-a79e-62cf429923cd\r/*.txt to ./*.txt: No such file or directory
mv: rename eb02c79a-5561-45c2-a962-f89bf5951efd\r/*.txt to ./*.txt: No such file or directory
mv: rename 6c7aeb93-94e0-433d-b108-e3b7e3897613\r/*.txt to ./*.txt: No such file or directory
mv: rename d1aa6e66-70dd-4f7c-aacc-e2e7d4709693\r/*.txt to ./*.txt: No such file or directory
mv: rename 3eb73d0f-3b8f-474d-9fa7-42d18e5ce6cf\r/*.txt to ./*.txt: No such file or directory
mv: rename 2c05f88c-4c27-48aa-b03b-58a50b081c8c\r/*.txt to ./*.txt: No such file or directory
mv: rename 1b76d0b5-21ba-47fb-a9f5-6f5179e3d2af\r/*.txt to ./*.txt: No such file or directory
mv: rename 4c63a4aa-d246-44ce-afa8-27d5e20866e1\r/*.txt to ./*.txt: No such file or directory
mv: rename 18ab45e5-0746-4e69-b21c-32cddb1c27b5\r/*.txt to ./*.txt: No such file or directory
mv: rename b2fc14b6-a281-4eb6-8186-82113f60c720\r/*.txt to ./*.txt: No such file or directory
mv: rename 799d8654-63f1-4b14-aab0-45276e23e42c\r/*.txt to ./*.txt: No such file or directory
mv: rename c53ea4b1-9b4c-4389-a396-62cb3de213c8\r/*.txt to ./*.txt: No such file or directory
mv: rename 1a225f23-384a-4197-9e02-b35ebc187f14\r/*.txt to ./*.txt: No such file or directory
mv: rename e0ae7fad-cf09-4fbc-be8c-6f66ded89d31\r/*.txt to ./*.txt: No such file or directory
mv: rename aef553f5-0c79-434d-a0ea-f3a3f903471f\r/*.txt to ./*.txt: No such file or directory
mv: rename 297aa29f-b08a-4e40-978b-2c2e7c378b01\r/*.txt to ./*.txt: No such file or directory
mv: rename 0881241f-af6f-4fe3-919c-0c8809e9c467\r/*.txt to ./*.txt: No such file or directory
mv: rename 354e436f-3ded-4b2e-abc3-d85d7801d45e\r/*.txt to ./*.txt: No such file or directory
mv: rename 297f42e7-e602-4104-b28a-bd2cd44787ca\r/*.txt to ./*.txt: No such file or directory
mv: rename 8572fe7d-e4a5-4fe3-8632-c5c5846aa804\r/*.txt to ./*.txt: No such file or directory
mv: rename 89cf8fe3-fc5c-43d3-b956-856aa62d5a38\r/*.txt to ./*.txt: No such file or directory
mv: rename 14a23ade-233d-4d17-9511-ce86c412144d\r/*.txt to ./*.txt: No such file or directory
mv: rename 56d0bcdf-03a7-4927-ab0b-e6557bdaefe7\r/*.txt to ./*.txt: No such file or directory
mv: rename 320e8c00-5bdb-44f6-a6a0-aedaf7923f46\r/*.txt to ./*.txt: No such file or directory
mv: rename 934f75b2-2202-4c91-b871-32bd1200881c\r/*.txt to ./*.txt: No such file or directory
mv: rename 0a21b137-a1dc-4f9f-a4fb-55e0800d7298\r/*.txt to ./*.txt: No such file or directory
mv: rename 2f9ba82e-7d19-49ed-a52c-266628aa834f\r/*.txt to ./*.txt: No such file or directory
mv: rename ed12aeb6-8db1-4623-83ea-754d18bce6cd\r/*.txt to ./*.txt: No such file or directory
mv: rename c826b814-031e-416c-a7c6-5aefba1c0170\r/*.txt to ./*.txt: No such file or directory
mv: rename 5a046111-c6fe-4d09-a6d3-66d4d99410d3\r/*.txt to ./*.txt: No such file or directory
mv: rename a9599472-8f67-4234-84df-9279b2c2e4d3\r/*.txt to ./*.txt: No such file or directory
mv: rename 81b291b9-94c1-44ae-a8fb-af016fda16a0\r/*.txt to ./*.txt: No such file or directory
mv: rename f8b875ad-91f9-4e9f-a358-7ef08bc92c09\r/*.txt to ./*.txt: No such file or directory
mv: rename bd56f5f4-f319-4c06-9b84-1073aae81e5d\r/*.txt to ./*.txt: No such file or directory
mv: rename 1ef3c255-e94a-4ed4-86b0-a55661a9a884\r/*.txt to ./*.txt: No such file or directory
mv: rename ae19ed2b-5f5f-4656-877d-915209fb3edf\r/*.txt to ./*.txt: No such file or directory
mv: rename 9b7a55c1-c8ca-46cd-a221-63b951aa01d8\r/*.txt to ./*.txt: No such file or directory
mv: rename 53fa7d62-cc67-4952-8b88-6263556bdcbc\r/*.txt to ./*.txt: No such file or directory
mv: rename 853c6091-72c5-43bd-a105-843ed703f21d\r/*.txt to ./*.txt: No such file or directory
mv: rename be2e81fb-8b9d-4bda-b4de-15b9df9ffbef\r/*.txt to ./*.txt: No such file or directory
mv: rename 42e75ea9-0d88-4fb5-ab0d-909a8a512a47\r/*.txt to ./*.txt: No such file or directory
mv: rename 28e5f49a-24a8-422c-a905-907f764c6e42\r/*.txt to ./*.txt: No such file or directory
mv: rename 3868b38a-cca1-46d4-b880-9285c67a40a4\r/*.txt to ./*.txt: No such file or directory
mv: rename 0fc032d0-0bd8-4220-b258-b53f818fb09d\r/*.txt to ./*.txt: No such file or directory
mv: rename 36411c9d-30c2-443c-a802-0dcd35782882\r/*.txt to ./*.txt: No such file or directory
mv: rename 9a7c710d-b0ec-4785-befa-7397a9626c73\r/*.txt to ./*.txt: No such file or directory
mv: rename 7aa6b6fb-a28b-4d16-ac90-84c2c6c0dff1\r/*.txt to ./*.txt: No such file or directory
mv: rename 9b11d3a2-a193-4fdb-befa-f0cfd712338e\r/*.txt to ./*.txt: No such file or directory
mv: rename c24ae1da-789e-4fd0-b004-ed85d8c39d65\r/*.txt to ./*.txt: No such file or directory
mv: rename 7c631538-8e1e-4319-af4e-cca68b235216\r/*.txt to ./*.txt: No such file or directory
mv: rename ea4faf2f-070c-4346-9681-b23e31ea7c34\r/*.txt to ./*.txt: No such file or directory
mv: rename 536cd03b-0bc8-4b86-8535-5b1edde385aa\r/*.txt to ./*.txt: No such file or directory
mv: rename 8425d840-4962-41cb-9351-cefbd0566de7\r/*.txt to ./*.txt: No such file or directory
mv: rename d5c0f504-8bbe-4e56-87f7-606f94340353\r/*.txt to ./*.txt: No such file or directory
mv: rename 83772960-4d13-4142-8b3d-263a10fc1fd3\r/*.txt to ./*.txt: No such file or directory
mv: rename 441dbe16-af52-4b0a-8d1c-cdcc9eda080d\r/*.txt to ./*.txt: No such file or directory
mv: rename 42f845a0-e2ce-4724-b405-c8978215c2e8/*.txt to ./*.txt: No such file or directory

@Simonare我尝试了这种方法

enter image description here

我有5个文件夹,每个文件夹中都有一个.txt文件,其名称与list.txt中的名称不同。

例如,在照片7595d58e-f4ee-4fbb-a8d3-ebdc6c00ffa1中看到的第一个文件夹中,文件名为

2961321f-a06c-4ead-9bfd-b27d99e4ef08.txt

然后我运行了您给我的算法,如下所示: 我将它们放在与list.txt和所有其他文件夹相同的文件夹中。我创建了测试文件夹并像这样

运行

$ ./run.sh list.txt

这是错误

mv: rename list/b7a8cbc0-8ff5-4216-8a00-d7a238f22fcd\r.txt to test/b7a8cbc0-8ff5-4216-8a00-d7a238f22fcd\r.txt: No such file or directory
b7a8cbc0-8ff5-4216-8a00-d7a238f22fcd
mv: rename list/7595d58e-f4ee-4fbb-a8d3-ebdc6c00ffa1\r.txt to test/7595d58e-f4ee-4fbb-a8d3-ebdc6c00ffa1\r.txt: No such file or directory
7595d58e-f4ee-4fbb-a8d3-ebdc6c00ffa1
mv: rename list/dfe14a8f-8c2c-41f5-9591-29a9dbf00db9\r.txt to test/dfe14a8f-8c2c-41f5-9591-29a9dbf00db9\r.txt: No such file or directory
dfe14a8f-8c2c-41f5-9591-29a9dbf00db9
mv: rename list/cc850b19-1d54-4794-a7c6-c6eaacd1dfc2\r.txt to test/cc850b19-1d54-4794-a7c6-c6eaacd1dfc2\r.txt: No such file or directory
cc850b19-1d54-4794-a7c6-c6eaacd1dfc2
mv: rename list/e8adda1d-86f5-45d1-bd7e-e9d1f1ce3075.txt to test/e8adda1d-86f5-45d1-bd7e-e9d1f1ce3075.txt: No such file or directory
e8adda1d-86f5-45d1-bd7e-e9d1f1ce3075

3 个答案:

答案 0 :(得分:0)

您需要这样的东西

编辑:我遗漏了一点。现在它必须工作。

#!/bin/bash
for file in *.txt; do
    while IFS='' read -r line || [[ -n "$line" ]];
    do
        FolderName=`echo ${file%.*}`
        mv $FolderName/$line.txt test/
        echo "$line"
    done <"$file"
done

答案 1 :(得分:0)

不确定我是否完全理解此问题,但是如果您有一个文件夹列表,并且在所有这些文件夹中,您希望有一个或多个.txt文件,则可以执行以下操作。

for dir in $(cat list.txt); do
   mv /some_path/$dir/*.txt /tmp/found_text_files
done

这假设列表只是目录名,而上面没有显示其他内容。它逐行抓取所有以.txt结尾的文件,并将它们移到/ tmp / found_text_files /目录中。如果文本文件数量过多,这将失败,因为bash扩展具有上限,但是只要每个目录<10,000个文件就可以了。

答案 2 :(得分:0)

如果我理解您的问题,并且您拥有list.txt,其中包含目录列表(每行一个),并且您有兴趣移动该格式的单个(或多个)文本文件somename.txt从命名目录到当前目录目录,那么您通常会在read循环中while从文件的每一行开始,通过将文本文件重定向为输入来提供循环循环,例如

## read each line into name, move name/*.txt to present dir
while read -r name || [ -n "$name" ]; do
    mv -i "$name"/*.txt .
done <"$1"

(注意"$1"list.txt文件的名称-您可以传递第二个 positional参数,以提供的名称替换当前目录'.'在第二个参数中)

用简短的脚本整理内容并添加验证以确保需要的输入并验证提供的list.txt的名称是有效的文件名,并且您有权读取文件,可以执行以下操作:

#!/bin/bash

[ -z "$1" ] && {    ## validate at least one input parameter
    printf "error: insufficient input\nusage: %s list.txt\n" "${0##*/}" >&2
    exit 1
}

[ -f "$1" -a -r "$1" ] || { ## validate input is file and is readable
    printf "error: file not found or not readable '%s'\n" "$1" >&2
    exit 1
}

## read each line into name, move name/*.txt to present dir
while read -r name || [ -n "$name" ]; do
    mv -i "$name"/*.txt .
done <"$1"

注意:,这假定脚本在包含list.txt中指定的所有目录的目录中执行。否则,您需要添加/path/组件"$name"/*.txt之前

仔细研究一下,让我知道这是否是您想要的,还有其他问题。