I need to move different files from different folders to a new folder using a command line. The files share the same format .asc: for example, there are files 20file1.asc, 20file2.asc and 20file3.asc existing in folder1, folder2 and folder3 respectively. I simply need to move the .asc from all folders to new folder which is folder4. Each folder is named following a yyyymmdd scheme.



I would use simple script for that, like this one:

for x in {folder1,folder2,folder3}; do mv $x/*.asc folder4/; done

This script assumes you're in path that has folder1,folder2,folder3 in it. You can tune it up to your needs.

For all folders in specific place you can do:

ls -1 >> file
for x in `cat file`; do mv $x/*.asc folder4/; done

But be aware, that ls -1 will also contain files and if there are also files in that folder, you will get errors for these files, saying that path does not exist.

  • the number of folders I have is around 4000. the names are not as I provided, the names actually are dates in this format yyyymmdd. example: 20100415 – Omar Ali Feb 29 '16 at 10:43
  • you can do ls -1 >> file then for x in `cat file`; do mv... – tymik Feb 29 '16 at 11:03

Assuming that all source folders are in the same directory path, you can use the following bash script:

ls -1 >> sources
for x in `cat sources`; do mv $x/*.asc target/; done
rm sources

Save it in your main folder with some name.sh, then run

$ sh name.sh

to get the job done automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.