LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   scripting question (https://www.linuxquestions.org/questions/linux-general-1/scripting-question-278158/)

lazyuser 01-15-2005 02:56 PM

scripting question
 
Hello,

I have a question, but do not know how to set it up, so it can be solved.

Lets say you have files, in a dir, named 0020.mpeg, 0030.mpeg, 0040.mpeg,
ex: /home/lazy/movies/1/0020.mpeg
/home/lazy/movies/1/0030.mpeg
However you have about 500 dir. like this.
/home/lazy/movies/2/0020.mpeg
/home/lazy/movies/2/0030.mpeg, How would you be able to get all them, in order (starting from dir 1 to 500), and move them in one dir, and rename them in order. So 0020.mpeg becomes 1.mpeg and so on. Any Insight is appreciated, not looking for a complete answer just any insight on how I would do this.

Thanks
Lazyuser

rgiggs 01-15-2005 03:07 PM

i see 2 for loops. here's a pseudocode i can think of:
Code:

NEWDIR="..."
counter = "1"
for each "subdir" under movies
    for each "file" under "subdir"
        mv "file" to NEWDIR/"counter".mpeg
    endfor
endfor

now, you can go to tldp.org and go through their bash tutorial.

edit:
actually, i think this won't work quite the way you want. i think you want the subdirs processed in order like this:
movies/1
movies/2
...
movies/9
movies/10
...

but i believe my pseudocode will do it like this:
movies/1
movies/10
movies/11
movies/12
...
movies/19
movies/100
movies/101
...
movies/2
movies/20
...

david_ross 01-15-2005 03:17 PM

You could probably just use a loop:
Code:

#!/bin/bash
IFS="
"
num=1
for file in `find /path/to/files -name "*.mpeg"`;do
 mv "$file" "`dirname $file`/$num.mpeg"
 num=$(($num+1))
done


homey 01-15-2005 03:22 PM

I just happen to have something laying around which may get you started. You could experiment with changing ( ls $dir ) to ( ls -R $dir ) ....
You chomd +x test then run the script like this: ./test /home/dir
Code:

#!/bin/sh

usage()
{
        echo "Usage: $0 [directory]"
        exit 1;
}

test -d "$1" || usage

dir="$1"

ls $dir | grep -e "[:alnum:]" | \
while read i ; do
n=$(( $n + 1 ))
j=`echo $i | awk -F. '{print $2}'`
mv -v "$dir/$i" "/tmp/file$n.$j"

if [ $n -lt '10' ]; then
mv "/tmp/file$n.$j" "/tmp/file00$n.$j"

elif [ $n -ge '10' -a $n -lt '100' ]; then
mv "/tmp/file$n.$j" "/tmp/file0$n.$j"

fi

done
find /tmp -type f -name 'file*' -exec mv -v {} $dir \;


lazyuser 01-15-2005 06:20 PM

Hello,

You guys rule.

I used david_ross example, and it worked. Then I issued the comand, find /home/lazyuser/movies/ -name "*.mpeg" -exec mv {} '/home/lazyuser/movies' \;
to move all the files into a dir.
One more question, if anybody can help. I want to combine all the files into one file. I know I can use cat, but everytime I try it it dosen't put the files in order. When I go ls *.* | sort -n, it puts them in order the way I want. How do I get that order into cat. Or is there some software in linux, where I can combine .mpeg files. Again any help would be appericate.

Thanks for the help already.
lazyuser

lazyuser 01-15-2005 07:38 PM

Hello,

Well I used TMPGEnc in windows, and it worked great. I was wondering if there is any util like this in linux.

lazyuser


All times are GMT -5. The time now is 10:45 PM.