문제

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