´Ó¾ø¶Ô·¾¶µ½Ïà¶Ô·¾¶£ºLinux·¾¶Ê¹Óü¼ÇÉ
´Ó¾ø¶Ô·¾¶µ½Ïà¶Ô·¾¶£ºLinux·¾¶Ê¹Óü¼ÇÉ
ÔÚ Linux ϵͳÖУ¬Â·¾¶ÊÇÎÒÃDzÙ×÷ÎļþºÍĿ¼ʱ¾³£»áÓöµ½µÄÒ»¸öÖ÷Òª¿´·¨¡£ÔÚ·¾¶µÄʹÓÃÀú³ÌÖУ¬¾ø¶Ô·¾¶ºÍÏà¶Ô·¾¶ÊÇÁ½ÖÖ³£¼ûµÄÌåÏÖ·½·¨¡£±¾ÎĽ«ÉîÈë̽ÌÖÔõÑùÔÚ Linux ϵͳÖÐÎÞаÔËÓ÷¾¶£¬ÌØÊâÊÇ´Ó¾ø¶Ô·¾¶×ª»»ÎªÏà¶Ô·¾¶µÄ¼¼ÇÉ£¬²¢Í¨¹ýÏêϸ´úÂëʾÀýÀ´¾ÙÐÐÑÝʾ¡£
ʲôÊǾø¶Ô·¾¶ºÍÏà¶Ô·¾¶£¿
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÃ÷È·¾ø¶Ô·¾¶ºÍÏà¶Ô·¾¶µÄ¿´·¨¡£
¾ø¶Ô·¾¶£º¾ø¶Ô·¾¶ÊÇ´Ó¸ùĿ¼×îÏȵÄÍêÕû·¾¶£¬Ö¸Ã÷ÎúÎļþ»òĿ¼ÔÚÎļþϵͳÖмòÖ±ÇÐλÖá£ÔÚ Linux ϵͳÖУ¬¾ø¶Ô·¾¶Í¨³£ÒÔб¸Ü / ¿ªÍ·£¬ÀýÈç /home/user/documents/file.txt¡£
Ïà¶Ô·¾¶£ºÏà¶Ô·¾¶ÊÇÏà¹ØÓÚÄ¿½ñÊÂÇéĿ¼µÄ·¾¶¡£¼´£¬ÌåÏÖÎļþ»òĿ¼Ïà¹ØÓÚÄ¿½ñλÖõÄ·¾¶¡£Ïà¶Ô·¾¶²»ÒÔб¸Ü¿ªÍ·£¬ÀýÈç ../documents/file.txt ÌåÏÖÎļþ file.txt λÓÚ documents Îļþ¼ÐÄÚ£¬¶ø documents Îļþ¼ÐλÓÚÄ¿½ñĿ¼µÄÉϲãĿ¼¡£
ÔõÑù´Ó¾ø¶Ô·¾¶µ½Ïà¶Ô·¾¶×ª»»£¿
ÔÚÏÖʵ¿ª·¢ºÍ²Ù×÷Àú³ÌÖУ¬ÎÒÃÇÓÐʱ¼äÐèÒª½«¾ø¶Ô·¾¶×ª»»ÎªÏà¶Ô·¾¶£¬Àû±ãÔÚ²î±ðÇéÐÎÖÐÎÞа²Ù×÷ÎļþºÍĿ¼¡£ÏÂÃæÎÒÃÇÀ´ÏÈÈݼ¸ÖÖÒªÁìºÍ¼¼ÇÉ¡£
ÒªÁìÒ»£ºÊ¹Óà realpath ÏÂÁî
realpath ÏÂÁî¿ÉÒÔ½«¾ø¶Ô·¾¶×ª»»Îª¹æ·¶»¯µÄ¾ø¶Ô·¾¶¡£È»ºó£¬Í¨¹ý±È¶Ô¾ø¶Ô·¾¶ºÍÄ¿½ñĿ¼£¬¿ÉÒԵóöÏà¶Ô·¾¶¡£ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룺
# »ñÈ¡¾ø¶Ô·¾¶ abs_path=$(realpath /home/ubuntu/workspace/file.txt) # »ñÈ¡Ä¿½ñĿ¼·¾¶ current_dir=$(realpath .) # ʹÓÃÏà¶Ô·¾¶ÌåÏÖÎļþ rel_path=$(realpath --relative-to=$current_dir $abs_path) echo "Ïà¶Ô·¾¶£º$rel_path"
µÇ¼ºó¸´ÖÆ
ÒªÁì¶þ£ºÍ¨¹ý cd ÏÂÁîÇл»Â·¾¶
ÁíÒ»ÖÖÒªÁìÊÇͨ¹ý cd ÏÂÁîÇл»Â·¾¶£¬È»ºóʹÓÃÏà¶Ô·¾¶¾ÙÐÐÌåÏÖ¡£ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룺
# ½øÈëÄ¿µÄÎļþËùÔÚµÄĿ¼ cd /home/ubuntu/workspace # ʹÓÃÏà¶Ô·¾¶ÌåÏÖÎļþ rel_path=documents/file.txt echo "Ïà¶Ô·¾¶£º$rel_path"
µÇ¼ºó¸´ÖÆ
ÏÖʵӦÓÃʾÀý
¼ÙÉèÄ¿½ñĿ¼Ϊ /home/ubuntu/work£¬ÎÒÃÇÒª½«Îļþ /home/ubuntu/work/files/image.jpg ת»»ÎªÏà¶Ô·¾¶¡£ÎÒÃÇ¿ÉÒÔÍŽáÒÔÉÏÒªÁ죬»ñµÃÈçÏ´úÂ룺
# »ñÈ¡¾ø¶Ô·¾¶ abs_path=$(realpath /home/ubuntu/work/files/image.jpg) # »ñÈ¡Ä¿½ñĿ¼·¾¶ current_dir=$(realpath .) # ʹÓÃÏà¶Ô·¾¶ÌåÏÖÎļþ rel_path=$(realpath --relative-to=$current_dir $abs_path) echo "Ïà¶Ô·¾¶£º$rel_path"
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏÒªÁ죬ÎÒÃÇ¿ÉÒÔÇáËɵؽ«¾ø¶Ô·¾¶×ª»»ÎªÏà¶Ô·¾¶£¬²¢ÎÞаµØ²Ù×÷ÎļþºÍĿ¼¡£
×ÜÖ®£¬ÔÚ Linux ϵͳÖУ¬Â·¾¶µÄÎÞаÔËÓÃÊǺÜÊÇÖ÷ÒªµÄ¼¼ÇÉÖ®Ò»¡£Í¨¹ý±¾ÎÄÏÈÈݵÄÒªÁìºÍʾÀý´úÂ룬ϣÍû¶ÁÕßÄܹ»¸üºÃµØÃ÷È·ºÍÓ¦Óþø¶Ô·¾¶ºÍÏà¶Ô·¾¶µÄת»»£¬Ìá¸ß²Ù×÷ÎļþºÍĿ¼µÄЧÂʺͱã½ÝÐÔ¡£
ÒÔÉϾÍÊÇ´Ó¾ø¶Ô·¾¶µ½Ïà¶Ô·¾¶£ºLinux·¾¶Ê¹Óü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡