Command Line Tuesdays
Command Line Tuesdays
Today we’re introducing a new series, called ‘Command Line Tuesdays‘. Why command line Tuesdays? Because in this series, everyday computer enthusiasts like yours truly, will try to step a little out of bounds of the Graphical User Interface (GUI) Culture, which is today synonymous to ‘making stuff easier for the masses‘.
Of course, if you visited any of the GNU/Linux related community forums, you’ve probably read, and this is an assumption, a very fiery debate over which is actually easier. Using GUI for everything, or simply learning and enjoying the Command-line Interface (CLI).
There are many arguments for or against the use of GUI tools. One of the most commonly referred pros is that we’ve been brought up in the point-and-click computing paradigm, so it’s basically a form of a language, or culture if you may, in which we interact with our machine. The patterns are mostly recurring, with big button clicking doing everything we need it to do.
On the other side, one of the most interesting comments I’ve read about the pros of command-line was: you can simply do more via the command-line much quicker, easier and faster than when using GUI tools. But there’s a catch: you have to learn the commands. Yes, learn them, as if learning a poem. Now, since to us, the semi-indoctrinated clueless users, it all sounds like some useless geek overreaching and overdoing, let me humanize it with a fable:
I’ve been using Linux for more then 3 years now. I’m a 27 year old literature major, who never held much of an interest in technology of any sort. I didn’t even own a computer until I was in high-school, so let’s say somewhere around 17. That’s barely ten years of computer usage. The only three programs I ever ran were BS Player, Winamp and Football Manager, with occasional usage of MS Word. Three and bit years ago, a colleague at a precarious job I was performing at the time was talking to another coworker, saying there’s a new edition of something which immediately occupied my attention, as it looked different. It was Ubuntu’s Natty Narwhal, I think. After discussing it with him, he pointed out the usual pros we gospel to the newcomers, no antivirus necessary, faster boot, better security, software center blah blah blah, and the peak of it: Free and Open Source (FOSS) philosophy behind it. And he did it in a very non-invasive, non fanboyish manner.
I decided to give it a go, and I never turned back since then. How I got into openSUSE around the 11.4 release, shortly after trying out Ubuntu for the first time is going to have to fit in another time, and how I find it to be the perfect sweet spot is a whole different story. The point is, I finally entered the Linux realm. It was fun, it was different, it made my computer run better, it was more stable. I barely had issues with it (seems I not-knowingly purchased fairly orthodox hardware). It seems that Linux came really far regarding the desktop, so I barely had to use the terminal. But let’s just say it was one of the most dreadful, terrifying and horrific tools to use in Linux. If there wasn’t a GUI for a specific operation, I immediately became nervous as I didn’t know what I was getting into when copy/pasting commands from the forums. After time, things changed. As opposed to other consumer-oriented systems, what happened to me was maybe somewhat of a human reverse-engineering. Instead of me consuming the system, I feel that in three years, the system consumed me. I learned how to search for basic issues, file bug reports, properly ask questions on forums. With every issue I had, I learned how to be better at providing the information needed for someone more competent to help me with my problem.
A short while ago, my girlfriend commented how she can’t believe I’m using my computer mostly for the sake of using the computer, rather than doing anything else. ‘You use it only for your music collection and Linux thingies, Nenad. Gaawd.’ It made me think, and think hard. Linux, specifically the openSUSE distro has become a hobby of mine. Not at an expert level, by any means, which is reasonable since it isn’t my area of expertise. But I know enough to get around, fix minor issues, play around with some configuration files etc. But what I came to know, is that with my increasing playing and tinkering around the system, I really need to get familiar with CLI. I learned that I’m playing around without knowing of the basics under the hood. What for crying out loud is ETC?!?!? What’s BIN? Why is there .sh at the end? Why is this file here, and this file there? Why do I have to click through gazillion of folders to find the right file to modify? Then I read you can easily list all the files somewhere with a single command. It became interesting. I suddenly felt an urge to try it out.
So Let’s Do It!
…and here we are. As a new openSUSE news contributor, I’ll try to contribute in a way I see productive for what I suppose is a large portion of the community. It’s time to plant the banner and start learning the command-line together. If philosophy is what attracted you to Linux and openSUSE, like me, it got you this far. Now it’s time to take a step further. Pinky And The Brain Look, I know there’s a bunch of you like me out there. I know you maybe don’t see the point in all that 90′s geeky black-screen-green-letters-quick-typing-make-everything-go-boom thing. But it’s useful in many aspects. We’ll learn in a fun and communal way which directories are for what, what files are located where, most commonly used terminal commands. It will make our lives easier when we learn to perform tasks through the console, make it even more rewarding when we run into issues. It will also make the lives of bug-squashers easier when they receive proper information from our end, and it will make our hobby infinitely more fun. I mean, for crying out loud, you’ve already made a transition to a phase where Dilbert and xkcd are actually funny. It’s time for the terminal!
Reference material that will be used: William Shotts – The Linux Command Line and a short tutorial on most commonly used commands by a Croatian hacker Velimir Baksa aka Lutherus. Requirements on your part: - An hour or two of spare time weekly – A pint of cold beer or any other beverage – Someone already pointed out that using Gedit, or a manual notepad (you know, pen and paper hehe) would be good for easier learning.
The book by Mr Shotts is exquisite. But there’s probably an issue of time/will-power etc. This way, I’ll try to humanize the lessons a bit more, take some time off your hands, and what’s most important – I count on us learning together, helping each other together (Comments and Forums, people!!) and tying a close-knit openSUSE community! This time next week (it will be June already!), we’ll learn our first commands. And remember…
…have a lot of fun!
Here we are geekos, back in action! Sorry it’s been a while, but let me just assure you we’re back on track, raging to meet the deadlines and to, well, have some fun :)
Now, the whole idea was pretty much completely summed up in the introductory text posted a fortnight ago, so we may get down to business. As the book by Mr. Shotts instructs, we need to know what a shell is, as we’ll be operating withing it, and what a terminal emulator is.
A shell, Mr Shotts states, is actually what we’re talking about when we’re talking about command-line. The shell is basically a program that passes the strokes of your keyboard to your computer, a translator of some sort, so your computer knows what you’re saying. There are many shells in existence, but the most widespread shell used in GNU/Linux distributions is called bash, or Bourne Again Shell. It’s a clever wordplay, as Mr Bourne created the bash’s predecessor, sh, and Brian Fox wrote it as a free alternative for sh. Ahhh, GNU people and their humor, very clever :)
Next thing we need so we can interact with the shell is a terminal emulator. Every Linux distribution comes with one, depending on the desktop environment you use, so it should be Konsole from KDE, Gnome terminal in Gnome etc. Mr. Shotts says you probably develop a preference for one, but I use what the desktop environment provides me, most of the time.
Now, fire up your terminal. You get a line, stating your user name and the machine’s host name. This is called the shell prompt. It means it’s ready for you to enter a command. Let’s try something random. Type in anything, and press enter.
Hehe, remember the introductory saying we need to learn the commands as a poem? There we go, random doesn’t work.
Now, press the up arrow, and you’ll see the command reappear. What’s this sorcery? The up arrow key on your keyboard is for accessing the command history. The terminal saves up to 500 commands you entered, so to not type them over and over, you can look for them with the up/down arrow. The left and right arrows are for moving the cursor within a specific line, so you can edit the text in between. Now there’s another thing – ctrl+v for pasting text doesn’t work. You can set it up as a shortcut somewhere, but it’s usually something else. Check your terminal emulator’s shortcuts! (in Konsole, it’s Settings > Configure shortcuts)Now, for us not to get the ‘command not found’ slap to the face, let’s try something simple. Type date. (Yes, I didn’t know there’s a command for this, so it’s exciting for me also) :)
There you go. Why bother looking at your built in calendar in the clock, when you can fire up your terminal and type date, and see what day it is :) Just kidding, as Mr Shotts states, it’s a simple command, the more useful/difficult ones are coming next time. The related command to date is cal – it will display the current month’s calendar.
You may also try df, and it will list free space on your drives.
Or free, to display the amount of free memory:
If you’re already in the type-only mood, you can enter the command exit to get out of the terminal emulator instead of pressing the x button.
…and we’ll stop to a halt for this week so it won’t be too much to ingest on your already tight schedule. To sum it up (you can write this conclusion down, for repetition), in part one, we learned:
what is a shell what do we need to communicate with the shell (terminal emulator) navigating the terminal commands with cursor buttons and exiting the terminal
Four simple commands:
date – displays the current date cal – displays the current month’s calendar df – shows the amount of free space on your hard drives free – amount of free memory
What will we do next Tuesday?
We will learn navigation through the file system (what are all those bin etc etc. folders, what are they used for, how to navigate through them via the terminal). Until then… …have a lot of fun!
Let’s refresh our memories. Last week, we skimmed through some basic commands, learned what a shell is actually, and made a steady introduction into our CLI Tuesdays series.
Today’s menu offers something else: navigation through the file system.
Now, the best picture I managed to find on the www is from a site called devopsbootcamp. You can find their tutorials and the rest here. But anyway, here’s a very nice diagram of what a linux root filesystem looks like.
For example, as stated in the above picture, your user directory (where you usually store your movies, music, documents etc.) is located in the /home folder. /home folder is located under /. Then you have the /etc folder, where most of the files for configuration are located. Anyway, you can find the detailed description here, as we’ll not be getting into which folder is for what, until we start using and configuring them. Today is reserved for navigation only. And on that note, let’s get down to the first command of the day…
pwd, or ‘print working directory’ is a very useful command if you think you are lost navigating through the folders. At any given moment, type pwd, and voila! What appears is a complete pathway you took to arrive to this folder. Those guys in The Cube could sure use it, losers.
Imagine yourselves walking from room to room inside a massive apartment, loosing your way. pwd is like the breadcrumb trail leading you to your starting point, so you don’t loose your way inside the folder maze! cd
Now you know in which directory you’re situated thanks to your usage of the pwd command. Now you want to take the next step and move to another directory. Let’s say you have a folder/directory inside your home folder you want to relocate your top secret terminal operation to. For this, you use the ‘cd’ command. cd, or ‘change directory’, will change the location of wanted directory. How do you use it? Simple, type cd and the path to your folder. Let’s say, for example, you want to enter your Hello Kitty picture collection in your home folder. You type ‘cd /home/username/Hello\ Kitty’.
As you see, we didn’t only use the space bar in the folder name. That’s because the terminal won’t recognize it. Whenever you want to navigate to a folder containing a space in it’s name, you replace it with the backslash character, followed by space. You can also, without using the backslash+space option, just put the whole folder name into quotes, f.ex. cd /home/username/”Hello Kitty”.
Try it out yourself. Navigate to a different directory using cd, and when there, type pwd to see if everything worked as it’s supposed to. Mr. Shotts’ Shottcuts
Mr. Shotts reminds us there are also some shortcuts available.
If you type only cd, without the path following it, your terminal will change your working directory (whatever it may be) to your /home folder.
The same, if you type cd ~user_name it will lead you to the home folder of the specific user you stated.
Next week, we’ll head over to the next chapter – we’ll learn how to list files and directories, view text files and classify file’s contents so it will be a bit more work than we’re used to, but I hope you’ll have enough time. Command by command, and if you haven’t the time to do it yourself, we’ll learn the basics together in a matter of months!
…have a lot of fun!
P.S.: Thanks to bwl’s comment, we fixed an error in the text regarding the space bar in directory names.
P.P.S.: GreatEmerald also added some fresh information about file hierarchy. You can read it in the comments.
Thanks for your input/corrections.