Command Line Examples

First, an example to show why we might even bother learning the command line:

me@ubu:~$ du --max-depth=2 /usr | sort -nr | less

This tells the du command to show disk usage to a maximum depth of two directories, all the directories in the /usr directory. Then send the output of that to the sort command, sorting them numerically, in reverse order, and then send that to the less command to page them in case there are too many to view on a screen.

There may be a GUI program to do that, but these commands would be available on virtually all Unix, Linux, or similar operating systems. If your disk is getting full, this would help you find where you might be able to delete some files.

In all of our examples, unless we say otherwise, the part you type is in red, though it would not show red on the screen, and you would press Enter at the end of the part you type in.

Moving about

me@ubu:~$ pwd
/home/ubu

The pwd command shows you where you are in the directory tree. We will see why this is handy later.

me@ubu:~$ cd Desktop
me@ubu:~/Desktop/

The cd command allows you to change the working directory. If you type cd by itself it will take you back to your home directory. The command cd .. will take you up one level to the parent of this directory. In fact, the two dots signifies the parent directory, and a single dot means the current directory. To return to the directory where you were before this, type cd -

Exploring files and directories

me@ubu:~$ mkdir examples
me@ubu:~$ cp -rv Examples/* examples/
'Examples/one.png' -> 'examples/one.png'
'Examples/two.png' -> 'examples/two.png'
me@ubu:~$ cd examples
me@ubu:~/examples$

Wow! What did that do? We first used the mkdir command to create a new directory named examples. We already had a directory named Examples, but remember filenames and directories in Linux are case sensitive, so that is OK, even if many people would not recommend doing it.

Then we told the cp command to copy, recursively and verbosely (show us what it is doing), everything from the Examples directory into the examples directory we made. That gives us some examples we can actually change.

Finally we used the cd command to change into the directory we made. And to be sure where we are, we used the pwd command again.

me@ubu:~$ ls
me@ubu:~$ mv ubuntu Sax.ogg ubuntu_sax.ogg
me@ubu:~$ ls -l

We used the ls command to list the directory. Then we told the mv command to move the file ubuntu Sax.ogg to the filename ubuntu_sax.ogg — which means rename it. Then we used the ls command again, but this time we told it to give a -l (long listing).

me@ubu:~$ cp /usr/share/dict/words .

This copies a file called words from where it was into our working directory.

me@ubu:~$ alias
me@ubu:~$ alias ll='ls -l'
me@ubu:~$ 

The alias command by itself lists all the aliases we have created. The one that was there was to make the ls command show certain filenames in color. Then we defined ll as an alias for ls -l so it will be easier to show a long listing. Then we tested the alias we had created.

me@ubu:~$ ll *.od? *.xls
me@ubu:~$ rm *.xls
me@ubu:~$ ll *.od? *.xls

For the last line, you could have pressed the Up Arrow key twice to get the same command from the command history. This is very handy, along with command and filename completion.

me@ubu:~$ mkdir ooo
me@ubu:~$ mv *.od? ooo/
me@ubu:~$ ll

Here we made a new directory named ooo and then we used the mv command to actually move files rather than rename them. Then we asked for a long listing of the working directory.

me@ubu:~$ chmod o-r ubuntu_sax.ogg
me@ubu:~$ ll

We used the

chmod change mode
command to remove the read permission from the ubuntu_sax.ogg file, for others who are not me or in the me group.

How would you know how to use chmod? Well, we could check the manual page:

me@ubu:~$ man chmod

System information

me@ubu:~$ df

The df command shows us how much is free on all our mounted file devices (something like drive letters on Windows).

me@ubu:~$ free -m

The free command shows us how much memory is free. We asked for it in megabytes.

The next one (if you have it on your system) will show lots of information about running processes (programs) and more, that changes about every second. Press q or Q to quit:

me@ubu:~$ top

Has anyone ever asked which kernel you are running? No? Well, in case they do, you can do this:

me@ubu:~$ uname -a

You might one day need some information about your network:

me@ubu:~$ ifconfig

Searching and grepping

Suppose you are looking for a file that has to do with the spelling dictionary in LibreOffice. There are a lot of files to look through, but we can use some powerful commands to help:

me@ubu:~$ locate libreoffice

Whoa! Too many. The locate command found about 19,171 files.

me@ubu:~$ locate libreoffice grep dict

The grep command got us closer. We are down to about 177 files that we could check. If we only want the English language files, we can make it:

me@ubu:~$ locate libreoffice grep dict-en

And now we're down to about 74 files. How did we count those 19 thousand files? Easy:

me@ubu:~$ locate libreoffice wc -l

This means we located the 19 thousand libreoffice files, then told the shell to take the output of that and run it through the wc program, and report lines (-l) instead of words.

Looks confusing!

The purpose of these pages are not actually to teach you to be a command line wizard — there are plenty of tutorials online for that. But maybe this will give you an idea about why someone might want to learn to use the command line rather than doing everything with a gui program.

Printed from linux.bz (Command Line Examples - Linux.bz, Linux in Belize)