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.
Would this helps: find . -maxdepth 1 -type f -exec cp {} /destination/path/{} \;
The above will look in the current directory (the . [dot]) for "normal" files (-type f) and copy them to /destination/path/.
The -maxdepth 1 option tells find to only look in the current directory.
@rikijpn: You are correct if you change that to: cp .* * /destination/path/
The .* needs to be included to also take care of all the dot files. And that is indeed the simplest way to do it.
Maybe the OP has a specific reason (exploring different ways to do things or getting some other inside?) for asking a script/one-liner.
The reason of using a loop instead of a single command is the loop would be just a skeleton to later fill with the appropriate stuff. So the question remains open: what corrections are necessary to make my script work? Thanks to both of you.
#!/bin/bash
find . -maxdepth 1 -type f | \
while read thisFile
do
cp "${thisFile}" "/destination/path/${thisFile}"
done
The above keeps the loop intact (although it is not a for x in y do loop). The output of the find command is piped to the while loop and files found are copied one at the time to the destination directory.
Do mind the quoting in the cp line, without it files with spaces will cause an error.
It hurts me deeply to have missed your post, so didactic (I _can_ say didactic, by the sheer fact of learning a lot by reading it). I specially liked that about the spaces. And of course the pipe. Regards.
P.S.: something weird is happening to my control panel. Some posts arrive at it but some do not.
It hurts me deeply to have missed your post, so didactic (I _can_ say didactic, by the sheer fact of learning a lot by reading it). I specially liked that about the spaces. And of course the pipe. Regards.
You're welcome
Quote:
P.S.: something weird is happening to my control panel. Some posts arrive at it but some do not.
I never use the control panel/LQ UserCP for tracking posts. I enabled the 'Instant email notification' (Edit options -> Messaging & Notification -> Default Thread Subscription Mode).
Also be aware that when using the piped method shown that any variables set / changed inside the loop will have all changes lost once leaving the loop.
Code:
#!/bin/bash
x=0
find . -maxdepth 1 -type f | \
while read thisFile
do
cp "${thisFile}" "/destination/path/${thisFile}"
((x++))
done
echo "Number of copied files is $x"
This test will show that x still equals 0 after the loop.
And what makes the difference? I know of only one method of loosing those changes: a new instance of bash. So, this while runs in a new incarnation of bash?
This simple change will now yield the correct results as the loop is now part of the original script.
That reasoning seems plausible (and a satisfying statement) to me. There being so much didactic material on shell scripting I'm beginning to feel a bit ashamed at making such elementary questions. Regards.
I'm beginning to feel a bit ashamed at making such elementary questions.
You should never feel ashamed. We all start somewhere. I like so many others are just happy to see people make an effort once supplied with information
Happy am I too to see you guys happy. And I'm now ready to mark SOLVED.
It is a sign of courtesy, I've been told, to make public a solution once found. No really _a_ solution, but the kind of solution I was seeking. So here it is:
Code:
#!/usr/bin/bash
# It has been tested.
LIST="$(ls u*.sh)"
for i in "$LIST" ; do
echo "$i"
done
exit
The loop inner line is merely a placeholder here. Maybe I could use a while instead of the for. When I find how, I'll post it.
Note: if I had a file named "uma turman" in dir from where I run the script, that file would be listed too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.