move files matching a specific pattern

How can I move files in a folder with a specific pattern to the destination folder?

I want to move files with names in the range [00-09] in the 4th column when separated by delimiter "-".

ls | cut -d "-" -f 4 | grep ^[00-09]

Shows these files:

UNCID_376947.TCGA-DD-A118-01A-11R-A131-07.110525_UNC12-SN629_0087_BC03HWABXX.7.trimmed.txt
UNCID_377115.TCGA-CC-5263-01A-01R-A131-07.110525_UNC12-SN629_0087_BC03HWABXX.5.trimmed.txt
UNCID_377252.TCGA-DD-A114-11A-12R-A131-07.110525_UNC12-SN629_0087_BC03HWABXX.6.trimmed.txt

Solution 1:

Use a glob expression:

mv your_dir/*?-*?-*?-0[0-9]* new_dir/

To test it, you can firstly perform a ls your_dir/*?-*?-*?-0[0-9]* to make sure it matches the files you want.

The expression *?-*?-*?-0[0-9]* means: characters + - three times and then 0 and any character.

Test

$ ls
another_dir/  aa-03A-b  aa-b-03A-b  aa-b-c-03A-b  a-b-c-03A-b  a-b-c-23A-b
$ mv *?-*?-*?-0[0-9]* another_dir/
$ ls another_dir/
aa-b-c-03A-b  a-b-c-03A-b

Solution 2:

You can use a 'for loop':

for FILE in 'file-list'
do
  echo "$FILE"
  mv "$FILE" /your/destination/
done

Explanation:

'file-list' should be replaced with a method to get a list of the files you want to use. For example $(cat files.txt) if you already have a list in a file, pattern* if the files in your directory start with the same pattern or $(find -iname "*pattern*") if you want to use find to get the list.
The output will be stored in the variable FILE one element after the other. The the commands between do and done are executed for each element. You can use echo "$FILE" to check if your command matches the right files before adding the mv command to the loop.