{"id":208285,"date":"2021-01-02T08:48:10","date_gmt":"2021-01-02T00:48:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=208285"},"modified":"2020-12-28T09:48:45","modified_gmt":"2020-12-28T01:48:45","slug":"basename-cmd-example","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/basename-cmd-example.html","title":{"rendered":"Linux\u4e2d\u7684basename\u547d\u4ee4\u4f7f\u7528\u5b9e\u4f8b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nbasename\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u4e2d\u5b9e\u7528\u7684\u5c0f\u5de5\u5177\uff0c\u53ef\u4ece\u7ed9\u5b9a\u7684\u6587\u4ef6\u540d\u4e2d\u5220\u9664\u76ee\u5f55\u548c\u540e\u7f00\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u7cfb\u7edf\u73af\u5883<\/strong><\/div>\n

Centos7<\/p>\n

\u5982\u4f55\u4f7f\u7528basename\u547d\u4ee4<\/strong><\/div>\n

\u5728Centos7\u7cfb\u7edf\u4e2d\uff0c\u5df2\u7ecf\u9ed8\u8ba4\u5b89\u88c5\u4e86basename<\/code>\u547d\u4ee4\u4e86\uff0c\u8be5\u547d\u4ee4\u5305\u542b\u5728coreutils<\/code>\u5b89\u88c5\u5305\u91cc\u3002
\n\"\"
\nbasename<\/code>\u6709\u4e24\u79cd\u8bed\u6cd5\uff1a<\/p>\n

\r\nbasename NAME [SUFFIX]\r\nbasename OPTION... NAME...\r\n<\/pre>\n

basename\u6700\u540e\u4e00\u90e8\u5206\u3002\u4e5f\u53ef\u4ee5\u5220\u9664\u4efb\u4f55\u7ed3\u5c3e\u7684\u540e\u7f00\u3002\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u547d\u4ee4\uff0c\u6700\u57fa\u672c\u7684\u662f\u53bb\u6389\u6587\u4ef6\u660e\u524d\u9762\u7684\u76ee\u5f55\u5e76\u6253\u5370\u51fa\u6765\uff1a<\/p>\n

\r\n[root@localhost ~]# basename \/etc\/yum.repos.d\/CentOS-Base.repo \r\nCentOS-Base.repo\r\n<\/pre>\n

basename\u547d\u4ee4\u9ed8\u8ba4\u5220\u9664\u6240\u6709\u7ed3\u5c3e\u7684\/<\/code>\u5b57\u7b26\uff1a<\/p>\n

\r\n[root@localhost ~]# basename \/usr\/local\/\r\nlocal\r\n[root@localhost ~]# basename \/usr\/local\r\nlocal\r\n<\/pre>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6bcf\u6761\u8f93\u51fa\u884c\u4ee5\u6362\u884c\u7b26(\\n)\u7ed3\u5c3e\u3002\u8981\u4ee5NUL\u7ed3\u5c3e\uff0c\u8bf7\u4f7f\u7528-z\uff08--zero\uff09\u9009\u9879\u3002<\/p>\n

\r\n[root@localhost ~]# basename -z \/usr\/local\r\nlocal[root@localhost ~]# \r\n<\/pre>\n

\"\"<\/p>\n

basename\u63a5\u53d7\u591a\u4e2a\u6587\u4ef6<\/strong><\/div>\n

basename\u547d\u4ee4\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u540d\u79f0\u4f5c\u4e3a\u53c2\u6570\u3002\u53ef\u4ee5\u4f7f\u7528-a\uff08--multiple\uff09\u9009\u9879\uff0c\u7136\u540e\u4f7f\u7528\u7a7a\u683c\u5206\u9694\u6587\u4ef6\u5217\u8868\u3002\u4f8b\u5982\uff0c\u8981\u83b7\u53d6\/etc\/passwd\u548c\/etc\/shadow\u7684\u6587\u4ef6\u540d\uff0c\u53ef\u4ee5\u8fd0\u884c\uff1a<\/p>\n

\r\n[root@localhost ~]# basename -a \/etc\/passwd \/etc\/shadow\r\npasswd\r\nshadow\r\n<\/pre>\n
\u5220\u9664\u6307\u5b9a\u7ed3\u5c3e\u7684\u540e\u7f00<\/strong><\/div>\n

\u8981\u4ece\u6587\u4ef6\u540d\u4e2d\u5220\u9664\u4efb\u4f55\u7ed3\u5c3e\u7684\u540e\u7f00\uff0c\u8bf7\u5c06\u540e\u7f00\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u9012\uff1a<\/p>\n

\r\n[root@localhost ~]# basename \/etc\/hostname name\r\nhost\r\n\u53e6\u4e00\u79cd\u65b9\u6cd5\uff1a\r\n[root@localhost ~]# basename -s name \/etc\/hostname \r\nhost\r\n<\/pre>\n

\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u6307\u5b9aname\u4e3a\u540e\u7f00\uff0c\u53ef\u4ee5\u770b\u5230\u8f93\u51fa\u7ed3\u679c\u4e2d\u53ea\u663e\u793a\/<\/code>\u540e\u9762\u548cname<\/code>\u524d\u9762\u7684\u5185\u5bb9\u4e86\u3002
\n\"\"
\n\u901a\u5e38\uff0c\u6b64\u529f\u80fd\u7528\u4e8e\u5220\u9664\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff1a<\/p>\n

\r\n[root@localhost ~]# basename -s .conf  \/etc\/httpd\/conf\/httpd.conf\r\nhttpd\r\n\u6216\u8005\r\n[root@localhost ~]# basename \/etc\/httpd\/conf\/httpd.conf .conf\r\nhttpd\r\n<\/pre>\n

\"\"
\n\u4e0b\u9762\u4f8b\u5b50\uff0c\u4f7f\u7528-a\u9009\u9879\u6307\u5b9a\u591a\u4e2a\u6587\u4ef6\uff0c-s\u9009\u9879\u6307\u5b9a\u540e\u7f00\u5185\u5bb9\uff1a<\/p>\n

\r\n[root@localhost ~]# basename -a -s .conf \/etc\/sysctl.conf \/etc\/httpd\/conf\/httpd.conf \r\nsysctl\r\nhttpd\r\n<\/pre>\n

\u5220\u9664\u672b\u5c3e\u540e\u7f00\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528-s\uff08--suffix = SUFFIX\uff09\u9009\u9879\u6307\u5b9a\u540e\u7f00\u3002\u4e0a\u9762\u5b9e\u4f8b\u4e2d\u4ee5\u5c55\u73b0\u3002<\/p>\n

\u4f7f\u7528\u5b9e\u4f8b<\/strong><\/div>\n

\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u4e86\u5982\u4f55\u5728bash\u811a\u672c\u4e2d\u4f7f\u7528for\u5faa\u73af\u3001mv\u547d\u4ee4\u548cbasename\u547d\u4ee4\uff0c\u901a\u8fc7\u5c06\u5f53\u524d\u76ee\u5f55\u4e0b\u9762\u7684\u56fe\u7247\u6587\u4ef6\uff0c\u6587\u4ef6\u6269\u5c55\u540d\u4ece\u201c .jpg\u201d\u66ff\u6362\u4e3a\u201c .jpeg\u201d\uff1a<\/p>\n

\r\n[root@localhost test]# vim convert.sh \r\n\r\n#!\/bin\/bash\r\nfor file in *.jpg\r\ndo\r\n  mv \"$file\" \"$(basename $file .jpg).jpeg\"\r\ndone\r\n<\/pre>\n

\"\"
\n\"\"<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

basename\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5b9e\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ece\u7ed9\u5b9a\u7684\u6587\u4ef6\u540d\u4e2d\u5220\u9664\u76ee\u5f55\u548c\u540e\u7f00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Centos7 \u5728Centos7\u7cfb\u7edf\u4e2d\uff0c\u5df2\u7ecf\u9ed8\u8ba4\u5b89\u88c5\u4e86basename\u547d\u4ee4\u4e86\uff0c\u8be5\u547d\u4ee4\u5305\u542b\u5728coreutils\u5b89 […]<\/p>\n","protected":false},"author":309,"featured_media":208286,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-208285","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=208285"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208285\/revisions"}],"predecessor-version":[{"id":208295,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208285\/revisions\/208295"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208286"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=208285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=208285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=208285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}