LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating folders and moving files in shell script (https://www.linuxquestions.org/questions/linux-newbie-8/creating-folders-and-moving-files-in-shell-script-4175606571/)

Asoo 05-24-2017 05:27 AM

Creating folders and moving files in shell script
 
Hi,

I want to create folders and move the files into folder using shell script. For example, I have 50,000 files (choose only the specific extnsion), I want to create 50 folders, each having 1000 files. The files are named like 1.txt, 2.txt ... , 50000.txt. So 1.txt-1000.txt should be in folder 1, 1001.txt-2000.txt should be in folder 2 and so on...

My test code that runs on only 10 sample txt files is below:

Code:

end=2
begin=1
for i in {1..5}
do
        mkdir "$i"
        for file in *.txt:
        do
                mv "{$begin..$end}.txt" "$i"
        done
        end=$((end+2))
        begin=$((begin+2))

done

It gives error,'mv: cannot stat '{1..10}.txt': No such file or directory'. I am able to create directories but cannot move the files into specified folders.

Thanks in advance.

!!! 05-24-2017 06:38 AM

Debug advice: use echo in place of your mv; use shell -vx switches.

p.s. Please mark your prior thread resolved, via ThreadTools, at top.
Thanks and best wishes for success. Welcome to LQ;)

Asoo 05-24-2017 07:48 AM

Thank you for the reply.

I used "eval" and it worked.


All times are GMT -5. The time now is 08:55 PM.