Beruflich Dokumente
Kultur Dokumente
How will you find which directory is taking how much space?
By using disk usage (DU) command in Unix for example du sh . | grep G
will list down all the directory which has GIGS in Size.
Whatarethefourfundamentalcomponentsofeveryfilesystemonlinux?
bootblock,superblock,inodeblockanddatablock
Whatisabootblock?
ThisblockcontainsasmallprogramcalledMasterBootrecord(MBR)whichloadsthe
kernelduringsystembootup.
Whatisthedifferencebetween$$and$!?
$$givestheprocessidofthecurrentlyexecutingprocesswhereas$!showstheprocess
idoftheprocessthatrecentlywentintobackground.
Whatarezombieprocesses?
Thesearetheprocesseswhichhavediedbutwhoseexitstatusisstillnotpickedbythe
parentprocess.Theseprocessesevenifnotfunctionalstillhaveitsprocessidentryinthe
processtable.
Iwanttoconnecttoaremoteserverandexecutesomecommands,howcanI
achievethis?
Wecanusetelnettodothis:
telnethostnameluser
>Enterpassword
>Writethecommandtoexecute
>quit
Ihave2filesandIwanttoprinttherecordswhicharecommontoboth.
Wecanusecommcommandasfollows:
comm12file1file212willsuppressthecontentwhichare
uniqueto1stand2ndfilerespectively.
Writeascripttoprintthefirst10elemenstofFibonacciseries.
a=1
b=1
echo$a
echo$b
forIin12345678
do
c=a
b=$a
b=$(($a+$c))
echo$b
done
Iwanttoreadallinputtothecommandfromfile1directalloutputtofile2anderror
tofile3,howcanIachievethis?
command<file11>file22>file3
WhatwillhappentomycurrentprocesswhenIexecuteacommandusingexec?
execoverlaysthenewlyforkedprocessonthecurrentprocess;sowhenIexecutethe
commandusingexec,thecommandgetsexecutedonthecurrentshellwithoutcreating
anynewprocesses.
Eg:Executingexeclsoncommandpromptwillexecutelsandoncelsexits,theprocess
willshutdown
Whatisthedifferencebetweengrepandegrep?
egrepisExtendedgrepthatsupportsaddedgrepfeatureslike+(1ormoreoccurrence
ofpreviouscharacter),?(0or1occurrenceofpreviouscharacter)and|(alternate
matching)
:Howwillyoufindthetotaldiskspaceusedbyaspecificuser?
dus/home/user1.whereuser1istheuserforwhomthetotaldisk
spaceneedstobefound.
HowcanIsetthedefaultrwxpermissiontoallusersoneveryfilewhichiscreated
inthecurrentshell?
1
umask777
Howdowecreatecommandaliasesinshell?
aliasAliasname=Commandwhosealiasistobecreated
Whatisasuperblock?
Superblockcontainsalltheinformationaboutthefilesystemlikesizeoffilesystem,block
sizeusedbyit,numberoffreedatablocksandlistoffreeinodesanddatablocks.
Whatisaninodeblock?
Thisblockcontainstheinodeforeveryfileofthefilesystemalongwithallthefileattributes
exceptitsname.
Whatarecandbpermissionfieldsofafile?
candbpermissionfieldsaregenerallyassociatedwithadevicefile.Itspecifies
whetherafileisacharacterspecialfileorablockspecialfile.
$echowelcome|awk'{for(i=length;i!=0;i)x=xsubstr($0,i,1);}END{printx}'
Findallemptyfilesinhomedirectory
#find~empty
Iftheuserwantstoredirectboththeoutputanderrortothesamefile:
#./StartScript.sh>f12>&1
Twothingstobenotedintheaboveexample:
First,theoutputgotredirectedtothefilef1
2>&1Thismeansredirecttheerror(2)tothesameterminalastheoutput(&1).
Sinceoutputisredirectedtof1,theerrorisalsoredirectedtof1
NetworkingCommands
findinghost/domainnameandIPaddresshostname
testnetworkconnectionping
gettingnetworkconfigurationifconfig
Networkconnections,routingtables,interfacestatisticsnetstat
queryDNSlookupnamenslookup
communicatewithotherhostnametelnet
outingstepsthatpacketstaketogettonetworkhosttraceroute
viewuserinformationfinger
checkingstatusofdestinationhosttelnet
hostname
hostnamewithnooptionsdisplaysthemachineshostname
hostnameddisplaysthedomainnamethemachinebelongsto
hostnamefdisplaysthefullyqualifiedhostanddomainname
hostnameidisplaystheIPaddressforthecurrentmachine
ping
Itsendspacketsofinformationtotheuserdefinedsource.Ifthepacketsarereceived,the
destinationdevicesendspacketsback.Pingcanbeusedfortwopurposes
1.Toensurethatanetworkconnectioncanbeestablished.
2.Timinginformationastothespeedoftheconnection.
Ifyoudopingwww.yahoo.comitwilldisplayitsIPaddress.Usectrl+Ctostopthetest.
ifconfig
Viewnetworkconfiguration,itdisplaysthecurrentnetworkadapterconfiguration.Itis
handytodetermineifyouaregettingtransmit(TX)orreceive(RX)errors.
netstat
Mostusefulandveryversatileforfindingconnectiontoandfromthehost.Youcanfindout
allthemulticastgroups(network)subscribedbythishostbyissuing"netstatg"
netstatnap|grepportwilldisplayprocessidofapplicationwhichisusingthatport
netstataornetstatallwilldisplayallconnectionsincludingTCPandUDP
netstattcpornetstattwilldisplayonlyTCPconnection
netstatudpornetstatuwilldisplayonlyUDPconnection
netstatgwilldisplayallmulticastnetworksubscribedbythishost.
nslookup
IfyouknowtheIPaddressitwilldisplayhostname.TofindalltheIPaddressesforagiven
domainname,thecommandnslookupisused.Youmusthaveaconnectiontotheinternet
forthisutilitytobeuseful.
E.g.nslookupblogger.com
traceroute
Ahandyutilitytoviewthenumberofhopsandresponsetimetogettoaremotesystemor
websiteistraceroute.Againyouneedaninternetconnectiontomakeuseofthistool.
finger
Viewuserinformation,displaysausersloginname,realname,terminalnameandwrite
status.thisisprettyoldunixcommandandrarelyusednowdays.
telnet
Connectsdestinationhostviatelnetprotocol,iftelnetconnectionestablishonanyport
meansconnectivitybetweentwohostsisworkingfine.
telnethostnameportwilltelnethostnamewiththeportspecified.Normallyitisusedtosee
whetherhostisaliveandnetworkconnectionisfineornot.
du==DiskUsage.Itwalksthroughdirectorytreeandcountsthesumsizeofallfiles
therein.Itmaynotoutputexactinformationduetothepossibilityofunreadablefiles,
hardlinksindirectorytree,etc.Itwillshowinformationaboutthespecificdirectory
requested.Think,"Howmuchdiskspaceisbeingusedbythesefiles?"
df==DiskFree.Looksatdiskusedblocksdirectlyinfilesystemmetadata.Becauseof
thisitreturnsmuchfasterthatdubutcanonlyshowinfoabouttheentiredisk/partition.
Think,"HowmuchfreediskspacedoIhave?"
basic'findfile'commands
find/namefoo.txttypefprint#fullcommand
find/namefoo.txttypef#printisn'tnecessary
find/namefoo.txt#don'thavetospecify"type==file"
find.namefoo.txt#searchunderthecurrentdir
find.name"foo.*"#wildcard
find.name"*.txt"#wildcard
find/users/alnameCookbooktyped#search'/users/al'
searchmultipledirs
find/opt/usr/varnamefoo.scalatypef#searchmultipledirs
caseinsensitivesearching
find.inamefoo#findfoo,Foo,FOo,FOO,etc.
find.inamefootyped#samething,butonlydirs
find.inamefootypef#samething,butonlyfiles
findfileswithdifferentextensions
find.typef\(name"*.c"oname"*.sh"\)#*.cand*.shfiles
find.typef\(name"*cache"oname"*xml"oname"*html"\)#threepatterns
findfilesthatdon'tmatchapattern(not)
find.typefnotname"*.html"#findallfilesnotendingin".html"
findfilesbytextinthefile(find+grep)
find.typefname"*.java"execgreplStringBuffer{}\;#findStringBufferinall*.java
files
find.typefname"*.java"execgrepilstring{}\;#ignorecasewithioption
find.typefname"*.gz"execzgrep'GET/foo'{}\;#searchforastringingzip'd
files
5linesbefore,10linesaftergrepmatches
find.typefname"*.scala"execgrepB5A10'null'{}\;
(seehttp://alvinalexander.com/linuxunix/findgrepprintlinesbeforeafter...)
findfilesandactonthem(find+exec)
find/usr/localname"*.html"typefexecchmod644{}\;#changehtmlfilestomode
644
findhtdocscgibinname"*.cgi"typefexecchmod755{}\;#changecgifilestomode
755
find.name"*.pl"execlsld{}\;#runlscommandonfilesfound
findandcopy
find.typefname"*.mp3"execcp{}/tmp/MusicFiles\;#cp*.mp3filesto
/tmp/MusicFiles
copyonefiletomanydirs
finddir1dir2dir3dir4typedexeccpheader.shtml{}\;#copythefileheader.shtmlto
thosedirs
findanddelete
find.typefname"Foo*"execrm{}\;#removeall"Foo*"filesunder
currentdir
find.typednameCVSexecrmr{}\;#removeallsubdirectoriesnamed
"CVS"undercurrentdir
findfilesbymodificationtime
find.mtime1#24hours
find.mtime7#last7days
find.mtime7typef#justfiles
find.mtime7typed#justdirs
findfilesbymodificationtimeusingatempfile
touch09301330poop#1)createatempfilewithaspecifictimestamp
find.mnewerpoop#2)returnsalistofnewfiles
rmpoop#3)rmthetempfile
findwithtime:thisworksonmacosx
find/newerct'1minuteago'print
findandtar
find.typefname"*.java"|xargstarcvfmyfile.tar
find.typefname"*.java"|xargstarrvfmyfile.tar
(seehttp://alvinalexander.com/blog/post/linuxunix/usingfindxargstarcrea...
formoreinformation)
find,tar,andxargs
find.nametypef'*.mp3'mtime180print0|xargs0tarrvfmusic.tar
(print0helpshandlespacesinfilenames)
(seehttp://alvinalexander.com/macosx/macbackupfilenamedirectoriesspace...)
findandpax(insteadofxargsandtar)
find.typefname"*html"|xargstarcvfjwhtmlfiles.tar
find.typefname"*html"|paxwfjwhtmlfiles.tar
(seehttp://alvinalexander.com/blog/post/linuxunix/usingpaxinsteadoftar)
Deletetheemptylinesofafile
sed'/^$/d'/tmp/detail.txt
currentshell
echo$SHELL
Thereisanimportantdifferencebetweensingleanddoublequotes.Singlequoteslimit
substitution.youcanplacevariablesindoublequotedtextandtheshellstillperforms
substitution.
Doublequotesdonotsuppressthesubstitutionofwordsthatbeginwith"$"buttheydo
suppresstheexpansionofwildcardcharacters.
Xargsandexec
$find.name"t*"
./temp2
./temp1
./temp3
$find.name"t*"|xargsls
./temp1./temp2./temp3
$find.name"t*"execls{}\;
./temp2
./temp1
./temp3
$
find/home/ganeshtypefexecchmod644{}\;
Andtoaccomplishsteps#2and#4togather.
find/home/ganeshtypedexecchmod755{}\;
ALittleexplaination
findcommandsdoesthefollowing:
1.
2.
3.
4.
findsthefilesoftypef,thatmeansitfindsregularfiles.
Thenitchangesthefilepermissionsbyexecutingchmod.
exectellsfindcommandthatyouwanttorunchmodonthefilesfoundout.
{}actsasaplaceholderforthefilefoundout.Letmetellyouhow.Letssayitfinds
filesemployees,designationsandfewothers.So,thisstepactsas:
chmod644employees
\;actsastheendofthecommandforthatparticularfile.
Thatallmeans,whenitfindsafile,itrunsachmodcommandforeveryfileinthehome
directory.
Sameappliestodirectorypermissionsaswell.
Nowletssayyouvegotabout10000filesand2000directories.Inthatcaseabove
methodrunschmodcommand10000timesforfilesand2000timesfordirectories.So,
chmodrunsintotal10000+2000=12000times.
Veryinefficient.Isntit?
Nowletsmakeitefficient.Herexargscomestoourrescue.
Letmeshowyouhowthisacts.
find/home/ganeshtypef|xargschmod644
Thismeansnowfirstfindfindsallfilesthen,attheendxargschangespermissionsofall
filesatonce.Thatmeans,itrunsonlyonetime.10,000timesfaster!!!Great.
Similarlyfordirectorypermissionchanges.
find/home/ganeshtyped|xargschmod755
Thishelpstochangepermissionsofalldirectoriesfoundinasinglestep.2000times
faster!!!goodtosee.
Andtoaddafile,Icansay:
:n test.pl