简体   繁体   中英

how to move files inside folders in one folder

I have 1000 folders and in few of them I need specific files. Their naming is crazy , for example the folder name is 0af1b217-6de0-48c6-ba62-6ffacdc07dce and the file in there is d5a54e1d-02d7-4cee-b6b1-e0a616cc322d.txt

So I have a text file that has the name of the folder that I want and the name of the file that I want. I want to move those in a new folder.

I am just wondering if someone could help me do that. I basically was thinking to do it using a for loop but I could not figure out how to go through each folder and take out the file

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

List looks like this

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

each of them is the name of a folder

inside each folder, there are many files, I only want to get the .txt out of it and put it in a folder. If it is easier I can also add the name of the files that I want to extract in the list.txt?

在此处输入图片说明

Inside each of these folders, there is one files with .txt format that I want to get out and move it to a folder.

The error is like this

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 I tried your approach like this

在此处输入图片说明

I have 5 folders and in each folder there is a .txt file with different name that in the list.txt.

for example inside the first folder you see in the photo 7595d58e-f4ee-4fbb-a8d3-ebdc6c00ffa1, the name of the file is

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

Then I ran the algorithm you gave me as follows: I put them in the same folder as the list.txt is there and all other folders. I created the test folder and ran it like this

$ ./run.sh list.txt

and this is the error

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

you need something like this

Edit: I Missed a point. Now it must work.

#!/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

Not sure if I understand this problem entirely, but if you have a list of folders and in all those folders you expect there to be one or many .txt files you could do something like this.

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

This assumes that the list is only the dir names with nothing else on the line, like you have shown above. It goes line by line grabbing all the files ending with .txt and moves them into the /tmp/found_text_files/ dir. This will fail if there are an absurd number of text files as bash expansion has an upper limit but as long as there are < 10,000 files per dir you should be fine.

If I understand your problem and you have list.txt that contains a list of directories (one on each line) and for which you are interested in moving the single (or more) text file(s) of the format somename.txt from the named directory to the present directory directory, then you would normally read each line from the file within a while loop, feeding the loop by redirecting the text file as input to the loop, eg

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

(note "$1" being the name of list.txt file - and you could pass a second positional parameter to replace the present directory '.' with the name provided in the second parameter)

Tidying things up in a short script and adding validations to insure needed input and validating the name for list.txt provided is a valid filename and that you have permission to read from the file, you could do something like the following:

#!/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"

( note: this presumes the script is executed in the directory containing all of the directories specified in your list.txt . Otherwise, you would need to add a /path/ component before "$name"/*.txt )

Look things over and let me know if this is what you intended and if you have further questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM