1

I have directories which are named as 0.1, 0.15,0.2, 0.25, 0.3,0.35 and so on. Now I want to copy directories at particular increments say of 0.1 together to a separate directory. Say directories 0.1,0.2,0.3 should go to folder one; directories 0.15,0.25,0.35 should go to folder two, etc. How can I do it with ease?

2

For your specific problem you can use the length of the file names to distinguish them:

mv ??? first
mv ???? second

If you want to solve this in a more general case you really want to use a language like Python or Ruby, where looping over files and basic arithmetic are sane.

  • In case we get to 10.1, your patterns should probably be *.? and *.??, or, bash-specific, shopt -s extglob; mv +([0-9]).[0-9] first – glenn jackman Jul 17 '15 at 18:19
0

If your shell support brace expansion:

for the first increment:

mv /path/to/0.{0..99} /path/to/newdirectory/

the second increment:

mv /path/to/0.{0..99}5 /path/to/newdir/

note: change 0 and 99 from {0..99} with first and last digit of your increment.

-1

This is the directory structure I am using

/
--0.1 - file
--0.2 - file
--0.15 - file
--0.25 - file
--first - dir
--second - dir

Now, first I will move the 0.1 and 0.2 files into dir 'first'

for f in `ls|grep '^0\.[0-9]$'`;do;mv $f first;done;

Next I'll move the 0.15 and 0.25 into dir 'second'

for f in `ls|grep '^0\.[0-9][0-9]$'`;do;mv $f second;done

In the code, grep '<regex>' lists all the files matching the pattern and then mv moves them to the respective directories.

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.