19best com­mand line prompts


PC Pro - - Front Page -

Ex­pert short­cuts to save time and keep Win­dows run­ning smoothly

Y ou prob­a­bly think of the Win­dows Com­mand Prompt as a his­tor­i­cal hang­over from the MS-DOS days. To be fair, that’s pretty much what it is. But it’s still a pow­er­ful and ef­fi­cient way of get­ting things done, some of which would be com­pli­cated or im­pos­si­ble with the mouse. Here’s our pick of com­mands worth know­ing – all of which still work in Win­dows 10.

1 at­trib View and change file prop­er­ties Ex­am­ple: at­trib -h -r c:\ex­am­ple.dat

Ev­ery file in Win­dows has a set of ba­sic at­tributes: some are “sys­tem” files, and Win­dows will com­plain if you try to delete or move them. Oth­ers might be hid­den, so by de­fault they won’t be shown in Ex­plorer. The “at­trib” com­mand lets you view or change th­ese at­tributes. The ex­am­ple above re­moves the “hid­den” and “read-only” flags from the tar­get file – use­ful if you’re clear­ing out old, un­needed files. You can use wild­cards to ap­ply at­tributes – so you could type “at­trib -s *.*” – and ap­ply at­tributes to fold­ers as well as files.

Note that th­ese low-level at­tributes co-ex­ist with the more so­phis­ti­cated multi-user per­mis­sion sys­tem in Win­dows. Just be­cause a file doesn’t have its “read­only” at­tribute set, that doesn’t guar­an­tee that you’ll be al­lowed to delete it.

2 del Se­lec­tively delete files and fold­ers Ex­am­ple: del /p /s c:\ex­am­ple\

You doubt­less know that you can use “del” (or its syn­onym, erase) to delete files – but the com­mand has some hid­den tricks. Use the /p switch, for ex­am­ple, and you’ll be prompted for con­fir­ma­tion be­fore delet­ing each file. That can be use­ful if you only want to clean out se­lected items, although be warned that files deleted through the Com­mand Prompt don’t go to the Re­cy­cle Bin – in­stead they’re in­stantly wiped.

As our ex­am­ple shows, you can use del with a direc­tory path, rather than a file­name, to delete all of the items in a folder in one go. More­over, if you use the /s switch, files in all sub­di­rec­to­ries within the spec­i­fied path will be re­cur­sively deleted. You can’t use del to delete a direc­tory, how­ever: for that you must use the “rmdir” com­mand.

3 robo­copy A hugely ver­sa­tile file-copy tool Ex­am­ple: robo­copy c:\ex­am­ple\ c:\ des­ti­na­tion\ /s

If you need to copy files and fold­ers around, “robo­copy” has you cov­ered. Our ex­am­ple above copies the con­tents of a folder into a folder called des­ti­na­tion – cre­at­ing it if it doesn’t al­ready ex­ist. The /s switch means any sub­di­rec­to­ries in­side our ex­am­ple folder will also be copied.

But robo­copy can han­dle much more com­pli­cated tasks. With the right switches, you can tell it only to copy files with cer­tain at­tributes, or files older or younger than a cer­tain age, or files of a cer­tain size. If files al­ready ex­ist in the des­ti­na­tion folder, you can tell it to over­write them, or over­write them only if they’re older, or newer. You can even tell it to “mir­ror” a direc­tory, delet­ing any files in the des­ti­na­tion folder that aren’t in the source lo­ca­tion.

4 tree See what’s where on your hard disk Ex­am­ple: tree c:\ex­am­ple /f

Ever get lost try­ing to nav­i­gate around a com­plex direc­tory struc­ture? “Tree” out­puts a hi­er­ar­chi­cal map of all sub­fold­ers be­low the cur­rent direc­tory – or be­low the spec­i­fied one if you sup­ply a path, as in our ex­am­ple. We’ve also used the /f switch, which lists all of the files in each direc­tory, as well as show­ing the over­ar­ch­ing folder struc­ture.

Un­less you’re work­ing with a very sim­ple lay­out, tree is likely to pro­duce pages and pages of out­put, which will scroll past in the blink of an eye – so it’s a good can­di­date for use with “more” ( see over­leaf). Al­ter­na­tively, if you’re writ­ing a batch file, you can pipe its out­put into an­other com­mand, or re­di­rect its out­put to a file you can process or pe­ruse at your leisure ( see Pipes and redi­rects tip).

5 where Search for spe­cific files and fold­ers Ex­am­ple: where /r /t *.xls

The “dir” com­mand shows the con­tents of a spec­i­fied direc­tory, but what if you don’t know which direc­tory a par­tic­u­lar file is in? The “where” com­mand is de­signed for this spe­cific job. The ex­am­ple syn­tax above will find all files with the spec­i­fied ex­ten­sion at or be­low the cur­rent direc­tory – the /r tells where to re­cur­sively search sub­di­rec­to­ries. The /t switch tells it to show com­plete in­for­ma­tion about any files it finds, in­clud­ing each file’s size and when it was last mod­i­fied.

The power of where is that it will let you search in mul­ti­ple spec­i­fied lo­ca­tions, or for files match­ing mul­ti­ple file­name tem­plates: mean­ing, for ex­am­ple, that you could use one com­mand to find all files from Word and Ex­cel lo­cated ei­ther on your desk­top or in your doc­u­ments folder.

6 find­str Find files con­tain­ing a spe­cific phrase Ex­am­ple: find­str /s “pass­word”

The “find” com­mand lets you search through a par­tic­u­lar file for a spec­i­fied phrase. Its big brother, “find­str”, is de­signed for more ad­vanced tasks. It can search for strings in mul­ti­ple files at once – the ex­am­ple us­age above will re­turn all files in the cur­rent direc­tory, and all sub­di­rec­to­ries, con­tain­ing the word “pass­word”.

Find­str also sup­ports reg­u­lar ex­pres­sions, pro­vid­ing enor­mous flex­i­bil­ity when it comes to spec­i­fy­ing the text you’re look­ing for. For ex­am­ple, if you want to find files con­tain­ing ei­ther “pass­word” or “pass­port”, you could search for “pass.o..” – just re­mem­ber to use the /r switch to ac­ti­vate reg­u­lar ex­pres­sion pars­ing. You can also use the /g switch to spec­ify a text file as a source for search strings, so you can eas­ily search for a whole list of phrases all in one go.

7 fc Find dif­fer­ences be­tween two files Ex­am­ple: fc /b c:\ex­am­ple1.dat c:\ex­am­ple2. dat

“Fc” stands for file com­pare, and the com­mand sim­ply com­pares files to see if they’re iden­ti­cal. What comes back isn’t nec­es­sar­ily a “yes” or “no”: if you’re deal­ing with text files, fc will out­put the dif­fer­ences that have been found, and if you use the /n switch, it will tell you on which lines of your files they ap­pear. The /LBn switch will tell fc to give up the com­par­i­son if more than n lines don’t match. You can also per­form a bi­nary com­par­i­son us­ing the /b switch, as in our ex­am­ple us­age.

You can use wild­cards in the file­names you pro­vide. For ex­am­ple, the com­mand “fc *.txt tem­plate.txt” will go through all the text files in the cur­rent direc­tory and com­pare each one in turn to your tar­get file.

8 for Re­peat one ac­tion across mul­ti­ple files Ex­am­ple: for %a in (*.txt) copy %a c:\ textfiles\ The “for” com­mand lets you ap­ply one op­er­a­tion to a whole set of files. Our ex­am­ple above may look daunt­ing, but it’s easy to un­der­stand if we break it into two halves. The first half says we want to work with ev­ery file match­ing the pat­tern in brack­ets (you can use the /r switch to search not only the cur­rent direc­tory, but also sub­di­rec­to­ries too). The vari­able %a is a place­holder for match­ing files. Mean­while, the second half of the com­mand tells Win­dows what to do with each file – in this case, copy­ing it into a folder called c:\textfiles.

The “for­files” com­mand does the same ba­sic job, but its more com­plex syn­tax lets you se­lect files by date, and you can pass a wider range of pa­ram­e­ters (such as the path of each match­ing file) to the tar­get com­mand. 9 more View in­for­ma­tion one page at a time Ex­am­ple: dir | more When a Com­mand Prompt pro­gram has a lot of in­for­ma­tion to show, it will nor­mally zoom past far too quickly for you to read. You can al­ways scroll up­wards to catch up on what you missed, but next time, con­sider us­ing the “more” com­mand. This makes the out­put au­to­mat­i­cally pause af­ter each page; press Re­turn to ad­vance by a sin­gle line, or Space to show the next page.

As in our ex­am­ple, a com­mon way to use more is by pip­ing the out­put from a com­mand through it ( see Pipes and redi­rects

tip). You can also use it to read a text file, by en­ter­ing “more ex­am­ple.txt”. If you do this you can use the /e switch to en­able “ex­tended fea­tures”, such as the abil­ity to skip both for­ward and back­ward by a cer­tain num­ber of lines. 10 sort Shuf­fle data into al­pha­bet­i­cal or­der Ex­am­ple: sort my­data.txt /o sorted.txt The “sort” com­mand sorts the lines in a file into al­pha­bet­i­cal or­der. By de­fault, the sorted data ap­pears onscreen, but you can use the /o switch to spec­ify a tar­get file, as above. You can also pipe the out­put of an­other com­mand into sort; for ex­am­ple, you might use the “where” com­mand to gen­er­ate a list of files match­ing a given tem­plate, then pipe the out­put through sort to al­pha­be­tise the list.

Note that sort isn’t case sen­si­tive, so “aard­vark” will al­ways come be­fore “Ap­ple”. The /r switch tells sort to use re­verse al­pha­bet­i­cal or­der. If you just type sort with­out any pa­ram­e­ters at all, it will take its in­put from the key­board, so you can paste in a list of data that you’ve copied from else­where. To end your list, press Ctrl+Z fol­lowed by re­turn. 11 clip Copy data to the Win­dows clip­board Ex­am­ple: dir *.txt | clip “Clip” copies data onto the Win­dows clip­board, so you can eas­ily paste it into an ap­pli­ca­tion such as Notepad or Ex­cel. One com­mon way to use clip is by pip­ing the out­put of an­other com­mand through it. Our ex­am­ple makes a list of all text files in the cur­rent direc­tory (the /b switch tells dir to out­put bare file­names, with no other in­for­ma­tion), then places it on the clip­board. It’s a lot eas­ier than copy­ing and past­ing by hand, es­pe­cially if you’re deal­ing with a long list of files.

You can also use re­di­rect­ion to copy the con­tents of a file to the clip­board, us­ing the syn­tax “clip < ex­am­ple.txt”. Keep in mind that this doesn’t copy the file it­self – you can’t then press Ctrl+C to du­pli­cate it in Win­dows File Ex­plorer. 12 mk­link Cre­ate a short­cut to a spec­i­fied file or folder Ex­am­ple: mk­link /d c:\docs c:\Users\PCPro\ Doc­u­ments If you fre­quently jump back and forth be­tween cer­tain di­rec­to­ries, you might find a short­cut use­ful. The “mk­link” com­mand al­lows you to cre­ate a sym­bolic link – a vir­tual folder that acts as a gate­way to some other lo­ca­tion on your hard disk (or even on a dif­fer­ent vol­ume).

The ex­am­ple above cre­ates a link that takes you straight from the root direc­tory into our per­sonal Doc­u­ments folder – to ac­cess it, sim­ply type “cd docs”. You can use links in Win­dows too – your sym­bolic link will ap­pear as a short­cut.

Note the /d switch above, which tells mk­link to cre­ate a vir­tual direc­tory: if you omit it, you can cre­ate links to in­di­vid­ual files. You can delete a link just as if it were a reg­u­lar short­cut; the orig­i­nal folder won’t be af­fected. 13 subst As­sign a vir­tual drive let­ter to a path Ex­am­ple: subst f: c:\ex­am­ple\ Sym­bolic links are great for hop­ping from one lo­ca­tion to an­other. If you want a short­cut that works ev­ery­where, “subst” is the an­swer: it cre­ates a vir­tual drive that maps to a path on your lo­cal disk. Once you’ve set up a vir­tual drive, it’ll be avail­able in Win­dows as well as the Com­mand Prompt. To re­move a vir­tual drive, use the /d switch and no tar­get path.

Note that subst can’t cre­ate map­pings across a net­work. If you want to as­sign a drive let­ter to a par­tic­u­lar place on a lo­cal server or NAS ap­pli­ance, you can do this through the Win­dows File Ex­plorer – or you can achieve it from the Com­mand Prompt us­ing the net com­mand: the syn­tax would be “net use f: \\server\share”. 14 ip­con­fig View and trou­bleshoot net­work set­tings Ex­am­ple: ip­con­fig /all Win­dows’ net­work set­tings are, frankly, a bit of a maze. If you just want to check your IP ad­dress and gate­way set­tings, it’s much quicker to open a Com­mand Prompt and type “ip­con­fig” for an over­view of your net­work sta­tus. If you use the /all switch, as in our ex­am­ple above, you’ll see a se­lec­tion of ad­di­tional de­tails, in­clud­ing your DNS server and DHCP de­tails.

Ip­con­fig also has a use­ful se­lec­tion of trou­bleshoot­ing func­tions, ac­cessed with switches. For ex­am­ple, “ip­con­fig /re­new” will try to re­new your IP ad­dress. If you’re suf­fer­ing from DNS prob­lems then “ip­con­fig /dis­playdns” will show you all cached DNS res­o­lu­tions (be warned, there may be a lot of them) while “ip­con­fig /flushdns” will for­get them all and tell Win­dows to look ev­ery­thing up afresh.

15 doskey Cre­ate com­mand-line short­cuts Ex­am­ple: doskey txt=dir *.txt In older ver­sions of Win­dows, “doskey” let you use the cur­sor keys to browse pre­vi­ously en­tered com­mands. Nowa­days, that ca­pa­bil­ity is built into the Com­mand Prompt, but doskey has an­other use, too: it can cre­ate macros (that is, short­cuts) for com­monly used com­mands. The ex­am­ple above lets you sim­ply type “txt” to list all the text files in the cur­rent direc­tory. If you want your macro to run mul­ti­ple com­mands in a row, use the $t marker to tell doskey where to “press Re­turn”.

Note that doskey macros only work within the Com­mand Prompt win­dow where they were de­fined. If you want your macros to work ev­ery­where, cre­ate an Au­toRun script that au­to­mat­i­cally de­clares them ev­ery time you open a new Com­mand Prompt win­dow ( see Au­toRun fun tip). 16 bootrec Re­pair startup in­for­ma­tion when Win­dows won’t boot Ex­am­ple: bootrec /fix­boot When you make a change to your PC hard­ware, the ar­range­ment of your hard disks may change, pre­vent­ing Win­dows from start­ing up. “Bootrec” can save the day. This tool isn’t built into the stan­dard Com­mand Prompt, but it’s ac­ces­si­ble from the re­cov­ery con­sole, which you can ac­cess by boot­ing from your Win­dows in­stal­la­tion me­dia.

Our ex­am­ple above tells bootrec to write a new Win­dows-com­pat­i­ble boot sec­tor to the pri­mary hard disk: if that doesn’t get the OS work­ing, try the /fixmbr switch to up­date the Mas­ter Boot Record, then use the /scanos and /re­build­bcd switches in turn to lo­cate your Win­dows in­stal­la­tion and make it bootable. Note that bootrec doesn’t know about dual-boot sys­tems; ad­di­tional trick­ery with the bcdedit com­mand will prob­a­bly be needed to re­gain ac­cess to a second OS. 17 diskpart Cre­ate, edit and delete disks and par­ti­tions Ex­am­ple: se­lect disk 0 “Diskpart” is a tool­box for all sorts of low-level disk op­er­a­tions. To use it, type “diskpart”, then type “list disk” to see the vol­umes avail­able. Use the se­lect key­word to in­di­cate which disk you want to work on – our ex­am­ple above would se­lect the first hard disk on the sys­tem, which is usu­ally the sys­tem disk.

From here you are able to re­size par­ti­tions (us­ing the shrink and ex­tend com­mands), con­vert MBR disks to GPT or vice versa, or com­pletely erase a disk us­ing the “clean” com­mand. Us­ing diskpart for the op­er­a­tions has two ad­van­tages over Win­dows: first, it often lets you delete vol­umes or par­ti­tions that ap­pear locked in Win­dows; and second, you can launch it with the /s switch to pass it a script, to au­to­mate pro­cesses such as con­vert­ing or par­ti­tion­ing disks. 18 pow­er­cfg Man­age power set­tings and mon­i­tor en­ergy us­age Ex­am­ple: pow­er­cfg /se­tac­tive SCHEME_ BAL­ANCED The “pow­er­cfg” com­mand lets you man­age your power op­tions. You can see a list of power schemes by en­ter­ing pow­er­cfg /l, and switch be­tween them us­ing the /se­tac­tive switch. Our ex­am­ple syn­tax sets the cur­rent power pro­file to “Bal­anced”, us­ing an alias rather than a lengthy hexa­dec­i­mal GUID (Glob­ally Unique Iden­ti­fier). Type “pow­er­cfg /aliases” to see a list of all avail­able aliases.

Pow­er­cfg also pro­vides use­ful di­ag­nos­tic in­for­ma­tion. En­ter­ing “pow­er­cfg /en­ergy” will an­a­lyse your sys­tem and cre­ate an HTML re­port warn­ing you of any­thing that might be drain­ing your bat­tery. The /sleep­study switch will cre­ate an HTML page de­tail­ing all of the times your com­puter has re­cently been to sleep, mean­ing you can track down any prob­lems. Fur­ther­more, you can get a re­port on the state of your bat­tery by us­ing the /bat­teryre­port switch. 19 sfc Check and re­pair sys­tem files Ex­am­ple: sf c /scan­file=c:\win­dows\ ex­am­ple.sys “Sfc” is Win­dows’ built-in Sys­tem File Checker. Hope­fully, you’ll never need to use the com­mand, but if your com­puter gets in­fected by a med­dling virus – or if it hap­pens to crash in the mid­dle of an up­date – then your sys­tem could end up in a semi-work­ing state. If ei­ther sce­nario comes to pass, sfc checks that all your sys­tem files are work­ing, and that they’re the cor­rect ver­sions: in most cases, you will want to scan your en­tire Win­dows folder, which you can do by en­ter­ing “sfc /scan­now”. (If you run the com­mand with no ar­gu­ment you’ll just see the help text.)

Scan­ning your whole sys­tem can be slow, how­ever, and de­pend­ing on the er­ror mes­sages you’re see­ing, you may only want to check one par­tic­u­lar file. You can do this us­ing the /scan­file switch, as in our ex­am­ple above.


Newspapers in English

Newspapers from UK

© PressReader. All rights reserved.