Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 753  / 3 Years ago, thu, september 30, 2021, 4:24:56

I am new to bash script and want to create bash script that moves some days old files between source and destination as per days defined in script.



When I run this script I get error



find: paths must precede expression: mv Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]



#!/bin/bash

echo "Enter Your Source Directory"
read soure

echo "Enter Your Destination Directory"
read destination

echo "Enter Days"
read days




find $soure -mtime +$days mv $soure $destination {} ;

echo "Files $days old moved from $soure to $destination"


What's missing or wrong ???? please help me to create this script.


More From » bash

 Answers
5

I'm sure you've already come across them already, but the two best places to begin with learning bash scripting are the Bash Guide for Beginners and the Advanced Bash-Scripting Guide. If you haven't seen these two resources yet, I would highly recommend bookmarking them if you're looking to do further bash scripting.



Removed portion regarding flow control due to edit in question. The find command needs -exec before the command or else it won't know that mv is actually a command you're attempting to execute on the files. Also, the {} in the find is used to refer to the results of the find command, thus you won't need $source as part of the find. The find command would probably look more like:



find $source -mtime +$days -exec mv {} $destination ;


If the find results will include files/directories with spaces in them, then you may need to loop through the results (which will bring the do/done back into the mix) so that you can include the results of the find in a variable that can then be enclosed in quotes. That will give a structure similar to the following:



for result in $(find $soure -mtime +$days)
do
mv "${result}" $destination
done


If you're still having problems, I'd suggest including a copy of your terminal window from running the script. Also, try running just the find command by itself without the -exec mv... portions and replacing the variables with what you'd normally fill in for them during the script execution. That way you can get an idea as to what the find command is matching so you can be certain that it's matching the files you want it to before making any changes to the location of your files.


[#33588] Thursday, September 30, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ampolinhad

Total Points: 88
Total Questions: 100
Total Answers: 116

Location: South Georgia
Member since Tue, Feb 1, 2022
2 Years ago
ampolinhad questions
Thu, Sep 8, 22, 15:45, 2 Years ago
Tue, Aug 10, 21, 20:03, 3 Years ago
Sat, Oct 16, 21, 22:44, 3 Years ago
Sat, Oct 23, 21, 03:11, 3 Years ago
Thu, Nov 17, 22, 15:34, 1 Year ago
;