tsquared
tsquared

Reputation: 99

Copying images to another directory using command line arguments bash

I'm really new to bash and I'm trying to write a script to loop through all pictures in a directory and copy them individually to a different directory. I'm not really familiar with syntax, so I can't really figure out what I'm doing wrong.

func ()
{
FILES=$1
FILES+=/*.jpg
for f in $FILES
do
   echo "$f"
   cp "$f" $2

done;
}
func $1 $2

If i enter arguments like script ./images ./test it echos every image as

/images/image1.jpg
/images/image2.jpg

and so on, but it's not actually copying them to the test directory. Any ideas on what the problem could be?

Upvotes: 1

Views: 1653

Answers (1)

sjsam
sjsam

Reputation: 21965

I'm trying to write a script to loop through all pictures in a directory and copy them individually to a different directory

You may use the find command like this

find path_to_your_directory -type f -name *.jpg -exec cp {} where_to_copy \;

As @shelter mentioned you could use cp *.jpg /path/to/destination_dir/ but in this case you have to write some extra code to take case of the files with spaces say file with spaces.jpg.

But find takes care of it automatically.

Upvotes: 2

Related Questions