LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script finding files, placing them and naming the (https://www.linuxquestions.org/questions/linux-newbie-8/script-finding-files-placing-them-and-naming-the-256531/)

TroelsSmit 11-18-2004 02:24 PM

script finding files, placing them and naming the
 
Hi,
I would like to create a script which will do the following:

1. Find all files following a regular expression finging following names: *.img *.IMG *.jpg *.JPG *.eps *.EPS

2. Move these files to a specified directory naming them:
1filename
2filename
3filename
etc.
etc.

If this can be easily done (tcsh,bash,perl) please help :-)

homey 11-18-2004 02:57 PM

Here's a little bash diddy which may help.

#!/bin/bash
for i in *.jpg *.gif *.img
do
n=$(( $n + 1 ))
echo mv "$i" "/home/$n$i"
done

TroelsSmit 11-18-2004 09:14 PM

Perfect, but I would like it to search my entire structure from / and so forth ... can that be done ?

btmiller 11-18-2004 09:45 PM

I would use:

find / \( -name "*.jpg" -o -name "*.img" -o - name "*.eps \) -print

to find the files, and then use xargs to pipe them to a shell script like homey gave you to do the moving. So the final result is:

find / \( -name "*.jpg" -o -name "*.img" -o - name "*.eps \) -print | xargs move_script

Then change the for loop in the script to "for i in $@" ($@ is all the arguments in bash).

homey 11-18-2004 09:50 PM

Quote:

Perfect, but I would like it to search my entire structure from / and so forth ... can that be done ?
You can do this from the / if you want but normally something like that would be for renaming a bunch of pics in your images directory. If you want to run this from the root directory, I would not try to move ( mv ) all of those files but use copy ( cp ) instead.

#!/bin/bash
for i in *.jpg *.gif *.img
do
n=$(( $n + 1 ))
echo cp "$i" "/home/$n$i"
done


All times are GMT -5. The time now is 03:36 AM.