How do I move files based on a certain character length range?

320 views Asked by At

I have the following files in a directory:

f.txt
fi.txt
fil.txt
file.txt
filen.txt
filena.txt
filenam.txt
filename.txt
filenametoolong.txt

I want to move all except the last one. This following regular expression applies to my needs: ^.{1,13}.txt

However, using the regex with with "mv"

mv ^.{1,13}\.txt trashdir

Results in

mv: cannot stat `^.1.txt': No such file or directory
mv: cannot stat `^.24.txt': No such file or directory

I have checked and double checked the regex syntax, and it seems to be valid. What am I missing?

1

There are 1 answers

4
anubhava On BEST ANSWER

mv command doesn't support regex. You can use find with -regex option:

find . -maxdepth 1 -type f -regex '\./.\{1,13\}' -exec mv {} /trashdir +