Soundtoxin

[GUIDE] GNU/Linux & Unix CLI

Nov 18th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.57 KB | None | 0 0
  1. Let's learn a bit about GNU/Linux. To try to keep things fairly universal we will focus on the CLI (Command Line Interface) mostly. As long as you can get to a shell on a unix-like operating system, you should have an experience that is generally the same, whether you are on Ubuntu, Arch, Gentoo, OpenBSD, or even Mac OS.
  2.  
  3. One of the most common modern shells is 'bash'. Bash is a shortening of 'bourne again shell'. It's a bit of a play on words, because it's the sequel to 'sh', aka the bourne shell. Bash has 100% compatibility with sh, so even when starting with the more modern of the two, you should pick up skills that could come in handy even on very old unix-like operating systems. This trend can also be seen in programs like 'vim', where many of its features are the same in the older 'vi'. There are some other modern shells like fish and zsh. Sometimes they may break backwards compatibility in the name of newer features, but you should still have a basic idea on how to use them after dealing with any other unix-like shell.
  4.  
  5. How do you access a shell? Well, there are a few common ways. Assuming your computer is running some sort of GUI, you'll most likely use a terminal emulator. As the name implies, it is emulating a terminal. It's still okay in most cases to just call it a terminal. It's a terminal emulator due to much older things being plain old terminals. In addition to your terminal emulator, on GNU/Linux you have access to various TTYs. This name is also descendant of much older technology, teletypes. In simple terms, a TTY is basically a fullscreen terminal for you to use. It may have a few less features than a terminal emulator in a graphical environment, but all your programs should work in it just fine. You can access your TTYs with the keybindings ctrl-alt-f1 through ctrl-alt-f7. Generally one of your TTYs will be taken up by your running graphical environment, if you have one. This is often either on TTY1 or TTY7.
  6.  
  7. Now that you know how to get to a shell, let's talk about what important programs / commands you'll want to know a bit about. An extremely important one that many people neglect to learn about early on is the 'man' command. It's short for manual, and as you might've guessed, it lets you access a manual right on the command line. Complete with a description of the program, usage examples, etc. In addition to 'man', there is also 'info'. Info is the GNU variant, and it works a bit similarly. The bindings are more similar to GNU software such as 'emacs', whereas the bindings with the 'man' command are taken from 'less', which also has bindings in common with 'vi'. Both will accept arrow keys as movement. Man will also accept j to go down and k to go up. To search in man/less, press the / key and begin typing to search forward from your position for your keyword. To do this in reverse, you hold shift, therefore typing ?. You can also press n or N to advance through results following the same logic. With GNU info, you use s to search. One interesting difference that 'info' has is that it has links you can follow, sort of like wikipedia. You can navigate through related articles as if they were a file structure. Because of this, if you want to learn something but aren't sure what, 'info' is the way to go. You can run the command on its own and see a very broad table of contents for stuff you can read up on.
  8.  
  9. I also mentioned 'vim' and 'emacs' while explaining 'man' and 'info'. These are the two most common command line text editors. Both are extremely powerful, but as far as features go, 'emacs' takes the lead for sure. Some go as far as to call it an operating system. Despite this, you will still find vim has a huge following, as well as a large plugin selection for expanding its features. As a new user, you should probably learn vim to start with. On most machines you will be able to find 'vi' installed, but you are unlikely to find 'vim' or even 'emacs'. This means that you can edit text efficiently on more machines by learning 'vim' or 'vi'. There is also a more simple and barebones editor that is beginner friendly known as 'nano'. It is a bit of a successor to an older editor called 'pico'. You might prefer to use nano, but I highly encourage you to learn vim for the future.
  10.  
  11. You now know how to learn about commands, and you have a few text editors at your disposal. You might still be feeling a bit lost, though. Let's learn how to navigate around the system with bash a little bit. The most important basic commands for this are 'cd' and 'ls'. The 'cd' command is to change directories, and the 'ls' command is to show what files are in a folder. You can run 'ls' on its own, or follow it up with a folder name to see what is inside that folder without being in it. Some other common commands are 'cp' for copy, 'mv' for move, and 'rm' for remove. Keep in mind that almost every command I have listed can be given arguments to alter its functionality. These arguments, or 'flags' as some might call them, are listed in the man pages for the respective commands. You can also sometimes use '-h' or '--help' to see a short bit of info on how to use a command without opening the man page. Don't expect this to be available for every command, though. It's important that you learn to read man pages.
  12.  
  13. Let's talk about package managers now. This is very distro-dependant, and on Mac OS you won't have one at all by default, although third party ones are available. (e.g. pkgsrc http://www.pkgsrc.org/) With GNU/Linux you are probably either dealing with apt, yum, pacman, or portage. These are on Debian, RedHat, Arch, and Gentoo, respectively. Once again, you can read man pages on these commands. Some basic usage examples for apt are 'apt search' to search for a package to install, 'apt install' to install a package, 'apt update' to update your package list, and 'apt dist-upgrade' to update all installed packages. The pacman equivalents are 'pacman -Ss' to search, 'pacman -S' to install a package, and 'pacman -Syu' to update all installed packages. All of these excluding the search commands will most likely require you to run them with 'sudo', which elevates you to the level of an administrator temporarily. Alternatively you can just login to the root account for system maintenance, but I highly recommend avoiding this and just learning when to use sudo, mostly for security reasons. If you are used to Windows or Mac OS, you might not understand the concept of package managers at first. This is a bit of a backwards analogy, but some people compare them to the 'app stores' on modern smartphones. Basically, most software you will need is in a repository that is managed by the maintainers of your distribution. This means you won't have to randomly search the web for executables to install the programs you need. This also means that programs will often be smaller because they don't have to all ship with their dependencies. Instead, the package manager handles dependencies. They are still being installed, but if two separate programs both rely on the same package, it will just be installed once. On Windows or Mac OS, you often have all this built into the executable, resulting in many different versions of the same libraries installed on your computer, wasting space and leading to everyone updating their software separately. Since all your packages can get along together like this, you're also able to update everything together instead of having to rely on each program to have its own updater, or worse, remember to manually check a website for newer versions.
  14.  
  15. At this point you should now have the knowledge to keep your system up to date, move around files, and edit text, all from the command line. You may have noticed that the text editors take over your whole terminal and come with their own bindings and such. There are many other programs like this, including web browsers (links), irc clients (irssi, weechat), mail clients (mutt), instant messengers (finch), file managers (mc, ranger), music players (cmus, mpd+ncmpcpp), niche programs for things like checking file sizes (ncdu), and more. As you start to learn about all these useful programs, you might wish you could use them all together more efficiently. If you're only using TTYs, this probably means you're stuck using one per TTY, hitting a hard cap of 6 or 7 programs. In a graphical environment you'll have a million terminal emulators open, which can become a mess very quickly. This is a good time to introduce terminal multiplexers. These will run both in graphical environments and TTYs. They basically allow you to split up your terminal's screen into small panes, as well as additional windows. You can resize panes and have several per window to suit your needs. The main terminal multiplexers out there are GNU 'screen' and 'tmux'. Personally, I recommend tmux. It is to my understanding that it has a lot more features, and it seems to be the more modern and popular of the two. One additional benefit to these terminal multiplexers is that you can detach from them. This means you can run system updates, detach from tmux, and have your terminal free to do other things again. Then you can re-attach later to check on it. What you can also do with tmux (but not screen) is attach to the same session from multiple shells. At this point, you might wonder what the point of this is. We'll come back to this in just a second, after I explain another major concept.
  16.  
  17. Let's talk about ssh!. Standing for secure shell, 'ssh' allows you to essentially open a terminal on a remote machine. You can 'ssh' into another machine by its IP, or if it's local, by its hostname. This can save some time if you are often connecting to another computer on your LAN, as its faster to type words than IP addresses in most cases. Ssh will allow you to manage your machines remotely to run updates or use any of the command line programs we have talked about up to this point... including tmux! This is where the ability to detach really comes in handy. Normally if you ssh into a machine, run updates, and disconnect, the program will stop running. If you are running updates and such inside 'screen' or 'tmux', however, they will continue to run after you detach from the session and disconnect from the computer. This allows for some pretty awesome setups, such as having a remote server running your irc client inside tmux, allowing you to connect from various computers and locations and still access the same irc client. Some other uses can include things as trivial as using ssh to control the music playing on your computer across the room from your phone or laptop. A good ssh app on Android is JuiceSSH, although you can just use ssh from any terminal app as well. JuiceSSH just gives a bit of added functionality like the ability to bring up keys that aren't on your phone keyboard, including the function keys, arrow keys, ctrl, shift, alt, etc. If you'd rather your default keyboard had all of these built right in, you can also get the Hacker's Keyboard off the Google Play store. You can also use ssh from an iOS device, although I believe you'll need to jailbreak first and get the app to do so from Cydia.
  18.  
  19. There's a lot to do from the command line, so definitely give it a chance! Many people are scared away from it, or just find it a pain to use. Chances are in the case of some sort of problem in the future, you'll end up having to use it anyway, so you might as well learn to love it, and find some fun uses for it. If you ever need help with something that doesn't seem to be covered just in the man pages, I highly recommend reading the Arch wiki (even if you're not on Arch!), or finding some people to talk to in IRC on Freenode. Most major distros have their own channel on Freenode for people to get help.
Add Comment
Please, Sign In to add comment