A script to copy files.
Hi:
Could you give me a script for copying all the files in a directory that are not directories to another directory. May be this could work: Code:
FILE=(ls -l|grep ^-) |
Hi,
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. Hope this helps. |
what's wrong with the regular
Code:
cp * directory_target/ it just outputs an "ommiting directory $dirname" string when source is a directory, but you can just redirect that to /dev/null like: Code:
cp * directory_name 2>/dev/null |
Hi,
@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.
|
Hi,
Code:
#!/bin/bash Do mind the quoting in the cp line, without it files with spaces will cause an error. Hope this helps. |
Hi:
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. |
Hi,
Quote:
Quote:
|
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 |
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?
|
The pipe forces a subshell, hence it is like calling a bash script from a bash script and the outer has no knowledge of what the inner has done.
It is not really all that dramatic to solve, merely it comes dow3n to how the loop gets its information, like so: Code:
#!/bin/bash |
Quote:
|
Quote:
|
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 Note: if I had a file named "uma turman" in dir from where I run the script, that file would be listed too. |
May I suggest you perform the following change and check the output:
Code:
echo "|$i|" |
It does surprise me. Seems a bit of an arcane to me.
If the # of filenames in LIST is N, I'd say, in post #14 the for is traversed N times, because I see the output includes N line terminators, output by the echo command. Hence control is transfered to first sentence in the loop just after echo printed |$i, this sentence being the echo sentence itself. It seems as if iteration had converted into recursion. |
No not nearly as exciting as you make it out to be :)
The inverted commas you have placed around "$LIST" has told the interpreter to preserve the whitespace. So if you have the following scenario: Code:
dir - u1.sh Code:
LIST="$(ls u*.sh)" Then when you use the following: Code:
for i in "$LIST" ; do This of course then only has to run the for loop once and do a single echo. |
I see, I see. The fault in my reasoning was not seeing that newlines are whitespace too.
Reading bash's man page I discovered the trap DEBUG builtin command. In fact, there is a full set of debugging facilities. Thanks for your kind help. |
All times are GMT -5. The time now is 02:33 PM. |