[SOLVED] move files or a directory to another directory using shell script
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
move files or a directory to another directory using shell script
Hi all,
I need help in moving a file to a different folder using k-shell script. I am doing a file pattern search in a directory (two levels deep), and if found need to move that file to a different folder.
For eg.
input_dir = /opt/oracle/client1/
searching in /opt/oracle/client1/test1/work/
All the files with ".done" extension need to be moved to /opt/oracle/client1/test1/Done folder.
This is what I have so far
find "$input_dir" -type f -mindepth 3 -maxdepth 3 | while read filename;
do
#echo "$filename"
echo "$filename" | grep -q '.done' && echo "$filename is done".
done.
I don't know how I can move the file to the folder when I don't have a folder name. Thanks for the help.
input_dir=/opt/oracle/client1
output_dir=Done
for i in $input_dir/*/*/*.done; do
loc=$(dirname "$i")
dest=$(dirname "$loc")/$output_dir
if [[ ! -d "$output_dir" ]]; then
mkdir -p "$output_dir"
fi
mv "$i" "$output_dir"
done
Last edited by suicidaleggroll; 11-14-2013 at 08:33 AM.
Thanks for the quick reply. The only problem is that I have multiple folders in client which I forgot to mention in the original question.
Eg - /opt/oracle/client1/test1/A - file needs to be moved to opt/oracle/client1/test1/Done
/opt/oracle/client1/test2/A - file needs to be moved to opt/oracle/client1/test2/Done
/opt/oracle/client1/test3/A - file needs to be moved to opt/oracle/client1/test3/Done
Also, is there a way to detect if the file at that level is a file or a folder. If it is a folder, then the folder needs to be moved since it would have ".done" extension.
It worked like a charm. Thanks!
If I also have to move the .NOTDONE files/folders to a NOTDONE folder, then should I write another similar loop or can it be taken care of in the same loop.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.