Skip navigation.

Missing pieces

Everything you should know about technology, but have been missing out

Posts tagged with "command line"

Screen - the terminal window manager

, , ,

Imagine tiling window manager(with only horizontal tiles) and session management like VNC, but for terminal. You'll get program that can manage number of terminal sessions and keeps them running even after you log out. That makes it popular among people who have remote shell account and run IRC client there. Though most people know only about fraction of the useful features it has. It's available for most Unix-like operating systems out there. Some Linux distributions might have installed by default.

You can start it by typing:

screen


By default this displays license and then open shell, but you can define it to start up predefined list of programs. At that point it's hard to tell if Screen is running or not. One way to check it is to try to change window. You can do that by pressing control+a and then 0 (which is number of the first window). If Screen is running, it notifies that window 0 is already open.

You can detach the session by pressing control+a and then ctrl+d. To resume the session, you can type:

screen -r


Though if for some reason Screen is already attached you have to use:

screen -dr


If you want to attach session, without detaching already running session, you can also use:

screen -x


Though keep in mind, screen size is always same in all the attached sessions, so if you want to use whole screen (or shrink to see everything type: control+a and F.

To quit and close all windows, type control+a :quit.

I'll end with summary of commands I use most often (you can always read the manual, if you want to see the full list). Line that starts with screen is command line option. Other commands are typed when screen is runnig.

Attaching and detaching session

Start new session: screen
Attach a session: screen -r
Detach and then attach a session: screen -dr
Share session: screen -x

Working with windows
Create new window: control+a c
Change to window number 1:control+a 1
Change to previously open window: control+a control+a
Go to next window: control+a n
Go to previous window: control+a p

Window tiling
Split window:control+a S
Focus next region:control+a tab
Remove region:control+a X
Remove all regions: control+a Q

Monitoring windows
Monitor for activity: control+a M
Monitor for inactivity: control+a _

Working with multiple sessions
List sessions: screen -ls
Resume session: screen -r session_name
Rename session: control+a :sessionname name
Quitting
quit and close all windows permanently: control+a :quit

Linux/Unix command line: part 3 - managing files

, , , ...

Computer is made for copying and processing information. So sometimes you need to make directories (to keep things organized), copy and remove files. Just telling what the commands are is not very efficient, so it's better to try those commands out and maybe learn some things about them along the way.

Let's start by making a directory. But let's first make sure that you're in your home directory:

cd


Let's keep home directory and make a directory for our experiment and move to our new directory:

mkdir experiments
cd experiments


So mkdir is command for making directories. That directory is bit empty, let's create some content.

touch foo


Check if the file is really there:

ls


Curious? Let's look inside.

cat foo


Nothing happened. Don't worry though, the file is empty so there was nothing to display. That's what touch does, creates empty files and that's fine for our experiments. Let's continue by creating another directory:

mkdir bar


Now move previously created file to new directory and enter that directory:

mv foo bar
cd bar


Another command again, mv is for moving files.

Oops, we need that file in it's original location too, let's make a copy:

cp foo ..


Make sure that the file is really there:

ls ..


Were done with this directory, let's move back:

cd ..


Let's make copy of a directory:

cp bar test


Didn't work? I guess we missed something:

cp -r bar test


It works now. You need to tell cp that you want to copy whole directory (and all it's files as a side effect). Let's check if everything is OK:

ls test


We're done now, let's clean up.

cd ..
rm experiments


Same mistake again. Let's fix that:

rm -r experiments


Now you should know how to manage your files. And as always, read the manuals if you wan't to know more about commands, most have all kinds of options that could come handy sooner or later.

Linux/Unix command line. Part 2 - moving on

, , , ...

In some cases you can't avoid command line. Ubuntu for example has good graphical administration utilities, but if you search for instructions or ask for help all you get is cryptic commands. Some think it's frustrating and others like me don't mind. Graphical tools are great when you don't have much time or just don't feel like reading manuals. Why using command line is better is personal thing, but maybe you find your own reasons once you get bit more comfortable with it. But if you're planning to script some common tasks, command line teaches you many useful commands that you can use.

To follow this tutorial, you need access to Unix/Linux system. You can get one for free for example at http://www.ubuntu.com/. Once you burn the CD, just boot from it and you should have working system without messing up your current operating system.

If you have used DOS command line before, there are few important differences. Everything is case sensitive and commands are in lowercase. Environment variables are usually in uppercase, but you can use lowercase names with your own custom environment variables. Also, don't use \ as a directory separator, in Unix it's /. \ is used as a escape character, you need it when you have to use some character that has special meaning, but you need to use it as normal character.

Another peculiarity is that everything is treated as a file, you can all sorts of things with files in /dev directory. Files in there actually represent devices on your system.

Some commands also have different function, for example find is used to find files, not to find strings inside a file. If you need to find strings, use grep. Some commands have the same name, but vowels are removed, for example copy is cp in Unix.

If you didn't understand much about previous paragraphs, don't worry, I'll explain those things later on, so DOS knowledge is not necessary.

Let's assume that you're in graphical environment. You can usually find terminal from menus, but if you can't see it anywhere just find run dialog and type xterm there. There are many different terminal emulators out there, xterm is just one that's usually already installed.

Most basic thing is moving around. It's hard to do anything if you're stuck in your home directory. Default shell prompt depends on shell you're using. Some of them don't show where you are, just on what computer you are. You can see where you are by typing following command:

pwd


If you want to know more about pwd coommand (you can use this for other commands too), type:

man pwd


What you can see on your desktop is actually just small part of the filesystem, it's not even your whole home directory. Here's how Linux filesystem is organized, some distributions have different practices how to use certain directories, but at least it gives good overview what those directories are. If you're using FreeBSD, man hier has good overview of the filesystem hierarchy.

Unix doesn't use drive names like a: or c:, everything is attached to root directory which is called /. Look what is in your root directory by typing:

ls /


Lot's of cryptic directories there, especially if you didn't open that earlier link. Let's try moving to one of those directories. First thing you can do is to move to root directory:

cd /


The you can move to for example var:

cd var


You can also combine those two:

cd /var


If you want to move up in directory hierarchy:

cd ..


Whoops, a mistake. Let's move back to previous directory:

cd -


Now can try some combining again. Let's move up in hierarchy and then to another directory with one command:

cd ../etc


If you're planning on editing your systems configuration files by hand, they're here. But let's not do any damage to your system and move safely back to home. There's no need to write full path to your home directory, all you need to write is:

cd


Home directory has also a special character that you can use. So for example if you want to move to Desktop directory (everything you put there shows up on your desktop) in your home directory, this command works, no matter where you are:

cd ~/Desktop


Now you should know how to move around. You can experiment as much as you want and home is always one command away.

But wait, before you start typing long filenames (or directory names), there's a shortcut. You don't have to write long filenames all by yourself, your shell can help. In most shells you can complete filenames and directories by pressing tab key when you have typed enough characters to find unique filename. Otherwise you'll just get list of possible files. There are some old shells that don't support that, but it's quite likely that you're using Bash. It's usually default on Linux systems, but on other systems it could be something else. You can check what shell you are using with following command:

echo $SHELL


You can display text with echo, it may not seem very useful right now, but it has its uses.

For me it displays:

/usr/local/bin/zsh


So that last word tells that I'm using ZSH.

That's all for now, we'll see what next part brings up. You can't do much just by moving around. As always, please leave a comment if you didn't understand something or have some ideas about what you would like to know.

Linux/Unix commandline. Part 1

, , , ...

Command line in general might look something that's not very useful and it's certainly not pretty. But looks can be deceiving, Unix command line actually has long history and people who developed it certainly believed in it too (though there's sort of Unix version 2 available). I would say it's still relevant for some people including me.

Unix (including Linux, I'll just use Unix from now on) is actually kernel and collection of utilities from various sources. For example yes is an utility that prints y on infinite loop. So next time you see program that asks stupid questions that can be dismissed with just y (or any other letter), yes could be handy. I'm not going to show how to use it right now though (maybe in some later post). And yes, it's included in most Unix-style operating systems.

So you can imagine, that somebody got irritated babysitting program that wanted users to answer yes to questions and developed a utility to do the task for him/her and continued being lazy (as good programmers and sysadmins are). I would imagine too that someone saw that the utility was useful and wanted a copy. And then it gets part of the standard, though I'm not sure if yes is part of the POSIX standard or not. Maybe not, as it's not exactly essential utility for the standard at all.

I'll start with something that's bit more easy to grasp, programs that actually have some kind of user interface. Good thing about these programs is that they don't take up much memory, startup doesn't take long and they don't need that much bandwidth if you use them remotely. Downside is that learning curve is usually bit higher than with usual GUI programs, there are keyboard shortcuts to learn and configuring usually happens by editing configuration files with text editor. And that means you have to first find the examples to work with.

Window manager: Screen

Screen is so versatile that it deserves whole article. In short it's like window manager on command line, windows take up full screen though, so screenshot doesn't make much sense. It keeps running if you log out, so most people use it to keep Irssi running while they are logged out from the remote computer where Irssi runs in, but that's only one of its features. For example you can create new windows, share session with yourself or with other user account.

Chat: Irssi

Irssi is IRC-client done right. No public away messages by default (though you can do that with a script, if you want to do that for some strange reason), support for multiple servers and it's easy enough to use for people that don't use command line much otherwise, or at least with some help from someone who knows how to use it. Only thing it lacks is (always) visible user list (though there are at least one script to fix that). If you have friends that don't use IRC, you can use Bitlbee to connect to various instant message networks.

E-mail: Mutt

Mutt is just a e-mail client, so there's no calendar. But it starts up real fast, and every function has keyboard shortcut. It's not so hard to use once you learn most important keyboard shortcuts. There's some tricks though how to use it effectively.

For example you can delete message matching certain pattern and change folder with new messages with few keypresses. Which folder it suggests depends on their order in config file, so you can check always messages in most important folders first, no matter which folder you have open. But whole feature means you need to put those folders into config file, otherwise you can just browse the folders manually as they are on disk.

Text-editor: Vi...wait...Emacs...whatever

There has been war going on between vi (or vim) and Emacs users since they were developed, but most people don't really understand either one of them. By default vi is on command mode and you can't really write anything until you press the right key. And both of them are not easy to even exit (ctrl-x-c in Emacs. esc and then :q! in vi). Both of them take some time to learn properly and are very efficient editors. And any Unix you use, vi is always available, so learning it has some benefits. But if your time is not worth it, try Nano. Or if you miss that paperclip from MS Office, try Vigor :smile:

WWW-browser: Lynx, Links or Elinks

All three are browsers, but links is actually black sheep here. It actually can display pictures and use the mouse if it's in graphical mode. Still very fast, if you don't mind that most of the pages look bit strange. Great browsers if you want to open browser instantly, even if you're not in graphical mode (in more technical terms, X). You can also use them to debug things from remote computer, for example if you are suspecting that some firewall is blocking your connection to some site. Or save pages from scripts.

News-reader: slrn

If you're into news (I mean Usenet, not just news in general), slrn is great program. Default settings are almost sane (I don't like automatically subscribing to new groups though, but at least I found how to automativally unsubscribe from new groups) and it supports non US-ASCII character sets just fine (I had some problems with TIN). It's also possible to read messages straight from server, which didn't also work so well with TIN. Not that hard to use, but as always, there are some keyboard shortcuts to learn to use it effectively. Help is easily available with ?, so no need to panic if you forget some keyboard shortcut.

Multimedia

Sound is not a problem in command line, you just need a player that doesn't need a gui. At least MPD or XMMS2 should be fine enough and there's even more players available. Both are more userfriendly with some nice frontend. Frontends can be graphical too. Separate backend means that you can quit frontend and music keeps playing.

As for movieplayer, MPlayer is good, if you can't display graphics at all for some reason, you can render the video in textmode. Some SDL based games work also, at least Kobo Deluxe did, others that I have tried didn't. There's also NetHack, which doesn't need graphics mode at all. I'm sure that's there at least one picture viewer, but I have not researched them much yet.

I wrote this article on request (or at least sort of, request was to write about Linux command line), so as always, you can make requests. One of the possible ones would be more in depth look into some of the programs that I mentioned, but that happens much faster if there's clearly demand for one, so please let me know if you want to know more.

PS: Screenshots are taken with terminal with transparent background. All pictures are links, so you can see the picture in original size. They almost fit in this blog, but limit for pictures resolution is just little bit lower that the originals have. And sorry about that last picture is JPG, it got automatically converted and filesize actually grew a bit. PNGs size is bit smaller, but resolution is bigger. Well, at least page layout should be alright in most cases.
December 2009
M T W T F S S
November 2009January 2010
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31