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

2 Answers
2

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:

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 AlyJun 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.
–
PaulJun 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 AlyJun 1 '13 at 3:26

There are some decent answers to that at this older question: askubuntu.com/questions/288287/… 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.
–
PaulJun 1 '13 at 4:35

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 AlyJun 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 SialviJun 1 '13 at 3:49