简体   繁体   English

shell脚本:将文件移动到另一个目录

[英]shell scripting: moving files to another directory


I have a directory called directory1 to which we store some files daily. 我有一个名为directory1的目录,我们每天都会存储一些文件。
I first try to remove the previous backup stored in directory2. 我首先尝试删除存储在directory2中的先前备份。 Then I try to move the files in directory1 to directory2. 然后我尝试将directory1中的文件移动到directory2。 I run the following bash script with cron but seems to fail. 我用cron运行以下bash脚本但似乎失败了。
What is wrong with my code? 我的代码出了什么问题?

#!/bin/sh
/bin/rm -rf /directory2/*
/bin/mv /directory1/* /directory2/

And my /etc/crontab has the following line: 我的/ etc / crontab有以下行:

0 6 * * 6 root /root/scripts/files.move.sh 

I don't see why your commands would fail, but the script can be made more defensive: 我不明白为什么你的命令会失败,但脚本可以更加防御:

set -e; # bash specific, fail on error
/bin/mv /directory2 /directory3
/bin/mv /directory1 /directory2
/bin/rm -rf directory3
/bin/mkdir -p /directory1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Shell Scripting:打印具有特定内容的目录名称和文件 - Shell Scripting: Print directory names and files with specifics 如何在Shell脚本中使用文件和目录的大小进行算术运算 - how to do arithmetic operations with the size of the files and directory in shell scripting 在 Shell Scripting Array of folders in directory 和显示每个文件夹中的文件,重命名/删除选项 - In Shell Scripting Array of folders in directory and show files in each folder, rename/delete option 将具有指定数量文件的所有目录移动到另一个目录 - Moving all directories which have a specified number of files, to another directory Shell脚本-将xml拆分为多个文件 - Shell scripting - split xml into multiple files Shell脚本-将一个变量分配给另一个-数组 - shell scripting - assigning one variable to another - arrays Shell脚本,用于替换文件夹中所有文件中的令牌 - shell scripting for token replacement in all files in a folder 在BASH Shell脚本中创建文件和目录 - Creating files and directories in BASH Shell Scripting shell 脚本:我有一个包含几个其他目录的主目录,我想打印这些目录中的所有文件 - shell scripting : I have a main directory which contain few other directories and I want to print all files inside those directories Bash脚本-显示目录中文件和文件夹的数量 - Bash Scripting - Display number of files and folders in a directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM