Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bunch of files in one folder named numeric as 001001 .. 001002 .. 002001 .. 002002 .. and so on until 114001 .. 114002 , i want to move every group of files that start with the same number to one folder , all files with 001 to folder 1 , all files with 110 to folder 110 and so on , no problem in creating the folders i have a problem in moving files in the sub folders in one step , hope some one help me to do that

share|improve this question
up vote 0 down vote accepted

Seems like the main trick here is translating the file name into the target folder name. There are two steps: truncate all but the first three digits, and remove zero padding. For this I have selected the sed and $((10# commands respectively:

for X in *; do mv $X $((10#$(echo $X | sed -e 's/^\(...\).*/\1/') )); done

This gives me minor errors such as mv: cannot move ‘114’ to a subdirectory of itself, ‘114/114’, but presumably you will not care about that if your files are getting moved where you want them to go.

share|improve this answer
thanx for ur fast reply but it's not do the trick cause it's moved the files started with 001 and 018 or 019 to the same dir 1 , i want to move every sequence to one folder 001 to 1 and 018 to 18 and 019 to 19 and so on – Mohammed Aly Jun 1 '13 at 2:55
Right you are! I did not realize that printf would interpret such a value in octal. Apparently there is a better method for discarding leading zeroes, and it handles the 018 and 019 case properly in my testing, so I have updated my answer accordingly. Please try again. – Paul Jun 1 '13 at 3:02
Perfect, thank you so much , just i am some new to shell scripting and i want to learn more, can u recommend me some sources to learn – Mohammed Aly Jun 1 '13 at 3:26
There are some decent answers to that at this older question:… I would say a good strategy is to start with what you know already, pick a short term goal, and add one piece at a time. So if you aren't familiar with for loops, read about those and experiment to figure out how you would want to use them. Similarly with various other pieces of syntax like $() and $[], and utility programs like sed. – Paul Jun 1 '13 at 4:35

Following one liner will create the directories and move the grouped files:

for f in *; do mkdir ${f:0:3}; mv $f ${f:0:3}/; done

If you already have directory structure use following instead:

for f in *; do mv $f ${f:0:3}/; done
share|improve this answer
thank u man , but it does not work good , the command moves all files started with 100 and 101 and 102 and so on but the rest of files are deleted and some files renamed to just 001 and 002 and 003 and so on , but thank u anyway for ur help – Mohammed Aly Jun 1 '13 at 3:28
@MohammedAly Some files got renamed/overwritten because there was no corresponding directory for them, like 001 for files starting with 001, so instead of moving them to 001 directory mv renamed them. That's why I provided the first command witch creates the directory first and then moves the files. – Basharat Sialvi Jun 1 '13 at 3:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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