Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving files to folder issue

Status
Not open for further replies.

swanand100

Programmer
Sep 11, 2015
1
IN
I have a script to move files of type .txt to a particular folder .It looks for the files in work folder and move it to completed folder.
Example: It takes the .txt file from /tmp/swan/test/work to /tmp/swan/test/completed, but I would like the script to also move the files from a subfolder to the completed folder(which it is not doing as of now). Example : Files from /tmp/swan/test/work/APX should move to /tmp/swan/test/completed

Below is the current script.

#!/bin/bash

MY_DIR=/tmp/swan

cd $MY_DIR

find . -path "*work*" -iname "*.txt" -type f -execdir mv '{}' ../completed\;
 
Add another 'find' command line to the script with the -path predicate altered to suit.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
The find command does recursive search. The issue with your notation may be the double quotes which cause shell to expand the patterns before find locates the actual files. Use single quotes like '*.txt'.
 
Maybe try this? Anything with the file extension ".txt" anywhere under [tt]/tmp/swan/test[/tt] will get moved to the completed directory.

Code:
#!/bin/bash

MY_DIR=/tmp/swan/test

find ${MY_DIR} -name '*.txt' -type f -print -exec mv \{\} ${MY_DIR}/completed

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top