本文提供了50最常用的命令在Linux/UNIX的实际例子。收藏这篇文章供日后参考。DidImissanyfrequentlyusedLinuxcommands?Leaveacommentandletmeknow.1.tarcommandexamplesCreateanewtararchive.$tarcvfarchive_name.tardirname/Extractfromanexistingtararchive.$tarxvfarchive_name.tarViewanexistingtararchive.$tartvfarchive_name.tarMoretarexamples:TheUltimateTarCommandTutorialwith10PracticalExamples2.grepcommandexamplesSearchforagivenstringinafile(casein-sensitivesearch).$grep-ithedemo_filePrintthematchedline,alongwiththe3linesafterit.$grep-A3-iexampledemo_textSearchforagivenstringinallfilesrecursively$grep-rramesh*Moregrepexamples:GetaGripontheGrep!–15PracticalGrepCommandExamples3.findcommandexamplesFindfilesusingfile-name(casein-sensitvefind)#find-inameMyCProgram.cExecutecommandsonfilesfoundbythefindcommand$find-inameMyCProgram.c-execmd5sum{}\;Findallemptyfilesinhomedirectory#find~-emptyMorefindexamples:Mommy,Ifoundit!—15PracticalLinuxFindCommandExamples4.sshcommandexamplesLogintoremotehostssh-ljsmithremotehost.example.comDebugsshclientssh-v-ljsmithremotehost.example.comDisplaysshclientversion$ssh-VOpenSSH_3.9p1,OpenSSL0.9.7aFeb192003Moresshexamples:5BasicLinuxSSHClientCommands5.sedcommandexamplesWhenyoucopyaDOSfiletoUnix,youcouldfind\r\nintheendofeachline.ThisexampleconvertstheDOSfileformattoUnixfileformatusingsedcommand.$sed's/.$//'filenamePrintfilecontentinreverseorder$sed-n'1!G;h;$p'thegeekstuff.txtAddlinenumberforallnon-empty-linesinafile$sed'/./='thegeekstuff.txt|sed'N;s/\n//'Moresedexamples:AdvancedSedSubstitutionExamples6.awkcommandexamplesRemoveduplicatelinesusingawk$awk'!($0inarray){array[$0];print}'tempPrintalllinesfrom/etc/passwdthathasthesameuidandgid$awk-F':''$3==$4'passwd.txtPrintonlyspecificfieldfromafile.$awk'{print$2,$5;}'employee.txtMoreawkexamples:8PowerfulAwkBuilt-inVariables–FS,OFS,RS,ORS,NR,NF,FILENAME,FNR7.vimcommandexamplesGotothe143rdlineoffile$vim+143filename.txtGotothefirstmatchofthespecified$vim+/search-termfilename.txtOpenthefileinreadonlymode.$vim-R/etc/passwdMorevimexamples:HowToRecordandPlayinVimEditor8.diffcommandexamplesIgnorewhitespacewhilecomparing.#diff-wname_list.txtname_list_new.txt2c2,3JohnDoe---JohnMDoeJasonBourneMorediffexamples:Top4FileDifferenceToolsonUNIX/Linux–Diff,Colordiff,Wdiff,Vimdiff9.sortcommandexamplesSortafileinascendingorder$sortnames.txtSortafileindescendingorder$sort-rnames.txtSortpasswdfileby3rdfield.$sort-t:-k3n/etc/passwd|more10.exportcommandexamplesTovieworaclerelatedenvironmentvariables.$export|grepORACLEdeclare-xORACLE_BASE=/u01/app/oracledeclare-xORACLE_HOME=/u01/app/oracle/product/10.2.0declare-xORACLE_SID=meddeclare-xORACLE_TERM=xtermToexportanenvironmentvariable:$exportORACLE_HOME=/u01/app/oracle/product/10.2.011.xargscommandexamplesCopyallimagestoexternalhard-drive#ls*.jpg|xargs-n1-icp{}/external-hard-drive/directorySearchalljpgimagesinthesystemandarchiveit.#find/-name*.jpg-typef-print|xargstar-cvzfimages.tar.gzDownloadalltheURLsmentionedintheurl-list.txtfile#caturl-list.txt|xargswget–c12.lscommandexamplesDisplayfilesizeinhumanreadableformat(e.g.KB,MBetc.,)$ls-lh-rw-r-----1rameshteam-dev8.9MJun1215:27arch-linux.txt.gzOrderFilesBasedonLastModifiedTime(InReverseOrder)Usingls-ltr$ls-ltrVisualClassificationofFilesWithSpecialCharactersUsingls-F$ls-FMorelsexamples:UnixLSCommand:15PracticalExamples13.pwdcommandpwdisPrintworkingdirectory.Whatelsecanbesaidaboutthegoodoldpwdwhohasbeenprintingthecurrentdirectorynameforages.14.cdcommandexamplesUse“cd-”totogglebetweenthelasttwodirectoriesUse“shopt-scdspell”toautomaticallycorrectmistypeddirectorynamesoncdMorecdexamples:6AwesomeLinuxcdcommandHacks15.gzipcommandexamplesTocreatea*.gzcompressedfile:$gziptest.txtTouncompressa*.gzfile:$gzip-dtest.txt.gzDisplaycompressionratioofthecompressedfileusinggzip-l$gzip-l*.gzcompresseduncompressedratiouncompressed_name237099797575.8%asp-patch-rpms.txt16.bzip2commandexamplesTocreatea*.bz2compressedfile:$bzip2test.txtTouncompressa*.bz2file:bzip2-dtest.txt.bz2Morebzip2examples:BZisEazy!bzip2,bzgrep,bzcmp,bzdiff,bzcat,bzless,bzmoreexamples17.unzipcommandexamplesToextracta*.zipcompressedfile:$unziptest.zipViewthecontentsof*.zipfile(Withoutunzippingit):$unzip-ljasper.zipArchive:jasper.zipLengthDateTimeName--------------------4099511-30-9823:50META-INF/MANIFEST.MF3216908-25-9821:07classes_1596408-25-9821:07classes_names1054208-25-9821:07classes_ncomp18.shutdowncommandexamplesShutdownthesystemandturnthepoweroffimmediately.#shutdown-hnowShutdownthesystemafter10minutes.#shutdown-h+10Rebootthesystemusingshutdowncommand.#shutdown-rnowForcethefilesystemcheckduringreboot.#shutdown-Frnow19.ftpcommandexamplesBothftpandsecureftp(sftp)hassimilarcommands.Toconnecttoaremoteserveranddownloadmultiplefiles,dothefollowing.$ftpIP/hostnameftpmget*.htmlToviewthefilenameslocatedontheremoteserverbeforedownloading,mlsftpcommandasshownbelow.ftpmls*.html-/ftptest/features.html/ftptest/index.html/ftptest/othertools.html/ftptest/samplereport.html/ftptest/usage.htmlMoreftpexamples:FTPandSFTPBeginnersGuidewith10Examples20.crontabcommandexamplesView