ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
But "cp -r /home/satimis/Document/*" does not suggest the folder name. Thus only copies the "Document/" contents only everytime. So it is not fuctional for what satimis is trying to do.
How about:
Code:
for i in `cat q_file`
do
read -p "Whould you like $i <yes/no>? "
if [ "$REPLY" = "yes" ]; then
cp -r /home/satimis/$i/* /home/satimis/Temp/dir1/
fi
done
This assuming "q_file" looks like:
Photo
Documents
New_Folder
Whatever
Also I think you want:
cp -r /home/satimis/$i/ /home/satimis/Temp/dir1/
Because you want to copie the whole directory and not just the files within right?
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
But micxz is missing the dir1, dir2, ... requirement, what about:
Code:
i=0
for dir in Document Photo etcetera
do
read -p "Add $dir directory <yes/no> "
if [ "$REPLY" = "yes" ]; then
i=$((i+1))
cp -r /home/satimis/$dir/* /home/satimis/Temp/dir$i/
else
break # not sure of that one, what was the initial "cancel" for ?
fi
done
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
If you look closely to the requirements, each copy is going to a different directory:
Document is going to dir1
Photo is going to dir2,
your solution is copying all to dir1, so doesn't comply.
By the way:
Code:
cp -r /home/satimis/$i/ /home/satimis/Temp/dir1/
is doing the very same operation that
Code:
/home/satimis/$i /home/satimis/Temp/dir1/
assuming $i is a directory, I dont see a point here.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Nope, the same.
The only difference in using a trailing slash is that if $i is a file, an error is triggered and the file is not copied, while with no slash, whether it is a file or a directory doesn't matter, it is copied to the destination directory.
For the first remark, I was not arguing of using a separate file for the directory list, which can be a good idea, especially if the file itself need to created by another program, but about the extra level of directories necessary to avoid same name files to be overwritten.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Some comments:
you call the cancel command if the user is not willing to add Document of Photo directories. This command's purpose is to cancel a printing job in progress, I'm quite sure it's not what you meant.
Why didn't you take or comment any of our suggestions about optimisation ?
You last loop isn't testing the validity of the directory entered.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.