================================================================ THE COMMAND LINE - NOTES A one-page reference from the workshop. Keep it. Add to it. It is only a text file. ================================================================ Nothing in here is something you have to memorise. You will remember the five commands you use every day, and look up the rest forever. That is what everybody does. ---------------------------------------------------------------- THE THREE QUESTIONS ---------------------------------------------------------------- Every session starts by answering the same three things. pwd Where am I? ls What is here? cd How do I move? If you are ever lost, run all three. They cannot break anything. ---------------------------------------------------------------- MOVING AROUND ---------------------------------------------------------------- cd Documents go into the Documents folder cd Documents/notes go two levels in at once cd .. go up one level cd ~ go to your home folder cd also home cd - go back where you just were Three shorthands that work almost anywhere a folder name does: ~ your home folder . right here, the folder you are in .. one level up Two kinds of path: RELATIVE Documents/notes Starts from wherever you are standing. Short, but it depends on where you are. ABSOLUTE /home/anna/Documents/notes Starts from the very top. The leading / is important. Long, but it always means the same place. ---------------------------------------------------------------- LOOKING AT FILES ---------------------------------------------------------------- cat notes.txt print the whole file to the screen less notes.txt scroll through it - press q to quit head notes.txt the first 10 lines tail notes.txt the last 10 lines head -3 notes.txt the first 3 lines wc -l notes.txt count the lines The q key quits less. ---------------------------------------------------------------- MAKING AND CHANGING ---------------------------------------------------------------- mkdir practice make a new folder touch notes.txt make a new, empty file cp notes.txt backup.txt copy a file cp -r folder1 folder2 copy a whole folder mv notes.txt final.txt rename a file mv final.txt practice/ move it into a folder mv does both jobs. As far as the computer is concerned, renaming something IS moving it - to the same place, under a different name. ---------------------------------------------------------------- DELETING - READ THIS ONE TWICE ---------------------------------------------------------------- rm notes.txt delete a file rm -r oldfolder delete a folder and everything inside it rm -i notes.txt ask me first (good habit while learning) There is no recycle bin. rm does not ask, no warnings, and it cannot be undone. This is not a reason to be frightened of the terminal. It is a reason to read the line before you press Enter. ---------------------------------------------------------------- WILDCARDS ---------------------------------------------------------------- * stands in for anything at all ? stands in for exactly one character ls *.txt every file ending in .txt cp *.jpg photos/ copy all the jpegs at once rm report-* everything starting with report- Before you delete with a wildcard, run the same pattern with ls first. You will see exactly what the star matched. ls report-* <- look rm report-* <- then execute ---------------------------------------------------------------- JOINING COMMANDS UP ---------------------------------------------------------------- The output of one command can become the input of the next. This is what makes the command line worth learning. | feed the output to the next command > write the output into a file, replacing it ls | wc -l how many things are in here? ls -l | head just the top of a long listing ls > filelist.txt save the listing into a file Read a pipeline from right to left: "count the lines, of the listing". ---------------------------------------------------------------- GETTING UNSTUCK ---------------------------------------------------------------- Ctrl+C stop whatever is currently running q quit a full-screen viewer like less or man clear tidy up the screen ls --help a quick reminder of the options man ls the full manual, q to quit Closing the window always works too. Nothing here is precious. ---------------------------------------------------------------- THE TWO KEYS THAT MAKE YOUR LIFE EASIER ---------------------------------------------------------------- Tab finish this word for me Up arrow the command I just ran Tab is not a shortcut for fast typists. It is how you CHECK that you typed something correctly. If Tab will not complete the name, the thing you are typing does not exist. Press Tab constantly. It will really make your life easier. ================================================================ A few commands cover most of what most people do most days. The rest, you look up. ================================================================