Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Shell脚本将文件从一个目录移动到另一个目录_Shell_Sh - Fatal编程技术网

使用Shell脚本将文件从一个目录移动到另一个目录

使用Shell脚本将文件从一个目录移动到另一个目录,shell,sh,Shell,Sh,我编写了一个shell脚本,它将所有文件从一个特定目录移动到另一个目录 #!/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

我编写了一个shell脚本,它将所有文件从一个特定目录移动到另一个目录

#!/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"
但当我运行此程序时,它会给出以下错误:

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")
有人能帮忙解决这个问题吗

PS:文件\u备份已创建,并且已向脚本授予适当的权限

使用-x运行脚本会得到以下结果:

+
: 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")
请尝试使用此代码

#!/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"

你可以试试这个,应该管用

#!/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 \;

仅供参考:粘贴您的代码,它将指出一些改进。@Nic3500不是一个很好的选择。它不是一个解决方案,因此我没有将其作为答案。@Sumit:我认为您没有发布导致问题的确切脚本,因为错误消息中的行号与您发布的行号不一致。使用
-x
运行脚本并让我们知道输出。尝试添加双引号:
$execute\u cmd“$f”$backup\u dir
我尝试过,但它给出了相同的错误@Sumit这对我有效,因为我已经测试过了。请检查一下其他东西。喜欢这个版本。谢谢。你的意思可能是:查找$path-name'*.xlsx'-exec mv{}$actual_dir\;