2

I have created the following script that move old days files as defined from source directory to destination directory. It is working perfectly.

#!/bin/bash

echo "Enter Your Source Directory"
read soure

echo "Enter Your Destination Directory"
read destination 

echo "Enter Days"
read days



 find "$soure" -type f -mtime "-$days" -exec mv {} "$destination" \;

  echo "Files which were $days Days old moved from $soure to $destination"

This script moves files great, but It also move files of source subdirectory, that I don't want. it should not take subdirectory files. How can I do that ?

1 Answer 1

4

Add -maxdepth 1 to your find command so that it doesn't not go into sub-directories.

From the find man page:

-maxdepth levels
    Descend  at  most  levels (a non-negative integer) levels of directories below the command line arguments.
Sign up to request clarification or add additional context in comments.

3 Comments

My find command should find -maxdepth 1 "$soure" -type f -mtime "-$days" -exec mv {} "$destination" \; or something ????
not working gives following error find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
Try: find "$soure" -maxdepth 1 -type f -mtime "-$days" -exec mv {} "$destination" \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.