I am trying to do something like "copy the newest file in a directory." I have come up the following command simple command using backticks, which works fine for filenames without embedded white space:

cp -rp `ls -1d searchstring | head -1` destination

As you can see this should work fine when the returned file has no space within it. However, this will obviously not work when there is such a space.

I need either a way to handle the output of the backticks, or some alternate approach.

有帮助吗?

解决方案

You can treat the result of the command substitution as a single word by adding double quotes around it:

cp -rp "`ls -t searchstring | head -n 1`" destination

The double quotes are not needed when assigning to a variable. a=`uptime` is equivalent to a="`uptime`".

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top