Author |
Message |
arivas_2005
Joined: 25 Feb 2007 Posts: 212
|
Posted: Sun 04 Jun 2017, 17:23 Post subject:
Copy or remove using variable with space and asterisk? |
|
Greetings !
I can not copy file to group using variables (using asterisk)
I have
files in folder dirOr:
la le li.jpg
la lo li.jpg
lu le li.jpg
# variables
dirOr="/Z seccion"
dirDs="/S seccion"
file1="/Z seccion/la le li.jpg"
filet="/Z seccion/*li.jpg"
a) copy ---------------
cp -- "$file1" "$dirDs"
# Using the name of each file I can copy OK
but
Using asterisk
cp -- "$filet" "$dirDs"
show message
cp: cannot stat ‘/Z s seccion/*li.jpg’: No such file or directory
How can I copy into groups using an asterisk in the variable ?
b) remove? ---------------------
rm -- "$file1" ## remove OK
but
rm -- "$filet"
show message
rm: unrecognized option '--/Z s seccion/*li.jpg'
Thanks !
|
Back to top
|
|
 |
musher0
Joined: 04 Jan 2009 Posts: 15041 Location: Gatineau (Qc), Canada
|
Posted: Sun 04 Jun 2017, 21:21 Post subject:
|
|
Hello arivas_2005.
Off the top of my head:
-- do you absolutely need the double-dash on the cp command line? If you
don't need it, it can get confusing to use it.
-- As you wrote it in your message above, "/Z section" means a directory
named "Z section" at the very top ("/") of your Linux directories. Is this
really the case?
Any copy of your variable for "/Z section" will fail because only one
directory can be named "Z section" at "/". The cp command will normally
refuse to copy any file or directory over itself.
Another reason why we avoid diagonal bars in file names and variables is:
-- the right-sloped diagonal bar ("/") is used by the Linux and Unix
systems as delimiter for directories. For ex, you will type to go to that directory. Please note the the first right-sloped diagonal bar
("/") represents the very top of the directories and that the other ones tell
Linux where to fork to the sub-directories.
-- the left-sloped diagonal bar ("\") is considered a warning sign in bash
scripts and perhaps other computer languages for the quotation mark and
similar characters.
For ex., at a terminal if you type Code: | echo "Rabbits eat lettuce." | the sentence Quote: | Rabbits eat lettuce. | will be written on the next line of your terminal.
If at the same terminal you want to see the command Code: | echo "Wolves eat rabbits." | , you have to write Code: | echo "echo \"Wolves eat rabbits.\"" |
~~~~~~~~~
Same comments for your directory "/S section".
~~~~~~~~~
-- In Linux and Unix we try to avoid spaces in filenames. The newer Linux
kernels are able to understand the space, between quotation marks, for
ex. file "a b c". But it is always better in Linux, IMO, in order to avoid
confusion and potential problems, to keep the old practice of replacing the
space with the underscore, for ex., to rename file "a b c" as file "a_b_c".
To this end your Puppy has a rename tool. Look for it in your Puppy menu
under the Utilities or File_Management sections.
One other rename tool that I like is "qmv", at
http://www.nongnu.org/renameutils.
It lets you use your text editor to rename files, which makes it very easy.
IHTH.
Description |
(Sorry for the typo.) |
Filesize |
28.8 KB |
Viewed |
366 Time(s) |

|
_________________ musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
|
Back to top
|
|
 |
MochiMoppel

Joined: 26 Jan 2011 Posts: 2084 Location: Japan
|
Posted: Sun 04 Jun 2017, 23:57 Post subject:
Re: Copy or remove using variable with space and asterisk? |
|
arivas_2005 wrote: | How can I copy into groups using an asterisk in the variable ? |
Try this:
Code: | TMP=$IFS; IFS=
cp $filet $dirDs
IFS=$TMP |
arivas_2005 wrote: | rm -- "$filet"
show message
rm: unrecognized option '--/Z s seccion/*li.jpg' | No, it doesn't. Your code will fail, but not with the message you posted. You get this message when you forget to put a space between -- and "$filet"
To remove the files in $filet you again have to temporarily reset the IFS variable so that bash does no split filenames at space characters:
Code: | TMP=$IFS; IFS=
rm $filet
IFS=$TMP |
Last edited by MochiMoppel on Wed 28 Jun 2017, 03:47; edited 1 time in total
|
Back to top
|
|
 |
arivas_2005
Joined: 25 Feb 2007 Posts: 212
|
Posted: Mon 05 Jun 2017, 23:15 Post subject:
|
|
Thanks MochiMoppel
It's a very fine solution
Thank you again
Almost at the same time and After hours of experiment and google built an option.
But it is very hard ( and the way of MochiMoppel is very easy)
Place the * outside the string
Example:
Code: | dirDs="/S seccion"
filet2="/Z seccion/*li.jpg"
# Cut into the * --> produces 2 substrings
cadeizq=${filet2%%"*"*}
cadeder=${filet2##*"*"}
# --> put out the *
cp "$cadeizq"*"$cadeder" "$dirDs"
#idem for remove
#rm "$cadeizq"*"$cadeder" |
Thank you again MochiMoppel
|
Back to top
|
|
 |
arivas_2005
Joined: 25 Feb 2007 Posts: 212
|
Posted: Fri 17 May 2019, 21:53 Post subject:
|
|
regards
I relive the problem of using parameters in variable.
I have again the problem of inserting parameters with * such as -name "* .txt" as variable of find ./ $ parameter,
Example:
Code: | CONDITION=" maxdepth 1 -type f -name "*.txt""
find ./ $CONDITION |
but the asterisk can not make it work.
The IFS technique suggested above, does not work for me.
'----------------------------------------------------
also parameters like the following: grep .txt | grep FORM THE PROBLEM IS THE |
Or in grep '.txt\|FORM'too, is \|
Example2:
Code: | CONDITION=" maxdepth 1 -type f"
CONDITION2=" | grep .txt | grep FORM"
find ./ $CONDITION $CONDITION2 |
'--------------------------------------------------
also and tried to use the content of the URL
http://murga-linux.com/puppy/viewtopic.php?p=959851&sid=d6c43423e9444cb8ae2fd8c5455d1e3e
but I can not master the technique of expressions
Thanks for your considerations
|
Back to top
|
|
 |
MochiMoppel

Joined: 26 Jan 2011 Posts: 2084 Location: Japan
|
Posted: Fri 17 May 2019, 22:42 Post subject:
|
|
CONDITION=" -maxdepth 1 -type f -name \"*.txt\""
eval find ./ $CONDITION
|
Back to top
|
|
 |
arivas_2005
Joined: 25 Feb 2007 Posts: 212
|
Posted: Fri 17 May 2019, 23:33 Post subject:
|
|
Thanks for the prompt response
I comment. use find ver 4.4.2
and
the segment -name \"*.txt\"" in
eval find ./ $CONDITION
not work in my computer. I use tahrpup version 6.0.6
error:
find: ` -maxdepth 1 -type f -name "*.txt"': No such file or directory
and shows the entire list without filter only txt
my lab script is:
Code: | #!/bin/bash
cd /mnt/sde1/Z_SECCION-2GD=sacarnotadeFORMAS/4
## CONDITION=" -maxdepth 1 -type f -name \"*.txt\"" # for reference
CONDITION=`Xdialog --stdout --separator "|" --inputbox "parametro para find " 8 125 ""`
eval find ./ "$CONDITION" > tempo.txt |
|
Back to top
|
|
 |
MochiMoppel

Joined: 26 Jan 2011 Posts: 2084 Location: Japan
|
Posted: Sat 18 May 2019, 05:50 Post subject:
|
|
It works for the example you posted and asked help for.
In the example you posted you must surround the stuff after '=' with quotes and you must escape the double quotes within the surrounding double quotes.
If you use Xdialog you must not do this.
When using Xdialog inputbox this would result in the error you mentioned:
Code: | " -maxdepth 1 -type f -name \"*.txt\"" |
This is how the Xdialog input should look like:
Code: | -maxdepth 1 -type f -name "*.txt" |
|
Back to top
|
|
 |
arivas_2005
Joined: 25 Feb 2007 Posts: 212
|
Posted: Sat 18 May 2019, 21:05 Post subject:
|
|
my sincere thanks for the support!
|
Back to top
|
|
 |
|