user2507818 user2507818 - 1 month ago 7
Linux Question

Should I put trailing slash after source and destination when copy folders

I want to copy this folder:

ajax
(/home/thej/public_html/JC/ajax) into this folder:
/home/thej/public_html/demo/conf/
the final result will be
/home/thej/public_html/demo/conf/ajax
, I know the
cp
command should be something like:

cp -r /home/thej/public_html/JC/ajax /home/thej/public_html/demo/conf


My question is:


  1. should I put
    /
    after
    ajax
    ,
    ajax/
    ?

  2. should I put
    /
    after
    conf
    ,
    conf/
    ?



I googled online, some put '/', some not, so really confused with it.

Answer

I try to put a trailing / on the target.

If I make a mistake and the source and target are both files rather than directories, adding the / means that I'll get an error; without the / it will clobber the target file.

(Tab completion will usually add the trailing / anyway, so it's usually easier to add it than not.)

See Volker Siegel's answer for more details.

Comments