-1

I have written a shell script which moves all the files from a specific directory to another directory.

#!/bin/bash

# Command to execute
execute_cmd=mv

path=/home/ypsvc/sa_automation

# Files inside actual_dir has to be moved
actual_dir="$path/sa_cx_data"

# This is the directory where files will be moved and kept
backup_dir="$path/file_backup/"

# Move each file from actual_dir to backup_dir

echo "Moving files to backup_dir"

for f in $actual_dir/*.xlsx
# echo f
do
 $execute_cmd "$f" $backup_dir
done

echo "Moving of files completed"

But when I run this, it gives below error:

Moving files to backup_dir
: not found.sh: 14: file_backup.sh:
file_backup.sh: 16: file_backup.sh: Syntax error: word unexpected (expecting "do")

Can someone help in resolving this?

PS: file_backup is already created and proper permission has been given to the script.

Running the script with -x gives below result:

+
: not found.sh: 2: file_backup.sh:
+ execute_cmd=mv
+
: not found.sh: 5: file_backup.sh:
+ path=/home/ypsvc/sa_automation
+
: not found.sh: 7: file_backup.sh:
/sa_cx_datar=/home/ypsvc/sa_automation
+
: not found.sh: 10: file_backup.sh:
/file_backup//home/ypsvc/sa_automation
+
: not found.sh: 13: file_backup.sh:
+
: not found.sh: 15: file_backup.sh:
+ echo Moving files to backup_dir
Moving files to backup_dir
+
: not found.sh: 17: file_backup.sh:
file_backup.sh: 20: file_backup.sh: Syntax error: word unexpected (expecting "do")
Sumit
  • 1,360
  • 3
  • 16
  • 29

2 Answers2

0

Try with this code

#!/bin/bash

# Command to execute
execute_cmd=mv

path="/home/ypsvc/sa_automation"

# Files inside actual_dir has to be moved
actual_dir="$path/sa_cx_data"

# This is the directory where files will be moved and kept
backup_dir="$path/file_backup/"

# Move each file from actual_dir to backup_dir

echo "Moving files to backup_dir"

for f in $(find $actual_dir -type f -name *.xlsx); ## used find here, with semicolon
do
 echo $f
 $execute_cmd $f $backup_dir
done

echo "Moving of files completed"
ggupta
  • 675
  • 1
  • 10
  • 27
0

You can try this one , should work

#!/bin/bash
path="/home/ypsvc/sa_automation"
# Files inside actual_dir has to be moved
actual_dir="$path/sa_cx_data"
find $path -name '*.xlsx' -exec mv {} $actual_dir \;
Brudex
  • 35
  • 7