cp $item > mkdir $map+copy
Ok, so, >
redirects the output of a command, the part that's printed (usually to your terminal). E.g. ls
outputs a list of files, so ls > list.txt
would output that list to list.txt
instead. But e.g. cp foo bar
explicitly opens the files foo
and bar
, but doesn't output anything to the terminal.
Thus, the redirection here, gives you an empty file called mkdir
, but the rest of the command cp $list $map+copy
would copy the file named by $list
to whatever $map+copy
expands to (the contents of the variable $map
and the fixed string +copy
, concatenated together).
On the other hand, cat foo
would open foo
and print it out, and you could use cat foo > bar
to direct that printout to a file called bar
. Pretty much the same as doing cp foo bar
, actually, except that cp
has options to like -a
and -p
to also copy the owner and permission information.
And, in the shell, you can concatenate strings by just sticking them next to each other (without any whitespace in between). So, if you set the variables x=foo y=bar
, all of these print foobar
:
echo foobar
echo "foo""bar"
echo "$x$y"
echo "foo$y"
echo "${x}bar"
Which means that you can just do "${map}copy"
or "$map""copy"
to concatenate the two parts.
You do need to run mkdir
separately from cp
, though, so if $map
contains foobar
, and $item
is hello.txt
, this would create a directory foobarcopy
, and copy hello.txt
to that directory:
mkdir -p "${map}copy"
cp "$item" "${map}copy"
(-p
tells mkdir
not to error if the directory already exists.)
See:
for issues wrt. (not) double-quoting the variables.
Also, instead of:
list=$( ls )
for item in $list; do
You can have the shell produce a list of filenames without calling ls
:
for item in ./*; do
See:
shellcheck.net
and fix the errors it tells you about. (1) cp does not deal with redirects. (2)+
is not a shell operator to concatenate strings. (3)mkdir
runs on its own command line, and only needs to be run once (at most) because you only specify it once. (4)ls
is not suitable for subsequent processing as it deals badly with spaces, special characters, and directories – Paul_Pedant Apr 02 '21 at 22:22cp $item > mkdir $map+copy
attempts to copy$item
to$map+copy
, writing any output of thecp
command to filemkdir
. I don't think this is your intention. I can guess what your script is supposed to accomplish, but to avoid misunderstandings and incorrect assumptions, please tell us what the desired result is. – berndbausch Apr 02 '21 at 23:49./
before$item
is a good idea, but you've got the wrong reason — it has nothing to do with $PATH. Also, rather than saying "cp ./$item $map", you should saycp "./$item" "$map"
(putting the variables in quotes). – Scott - Слава Україні Apr 12 '21 at 01:55