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
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.
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/
cd command allows you to change the working directory. If you
cd by itself it will take you back to your home directory. The
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,
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
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
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 alias ls='ls --color=auto' me@ubu:~$ alias ll='ls -l' me@ubu:~$ ll
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 used our alias
ll to get a long listing of the working directory.
me@ubu:~$ chmod o-r ubuntu_sax.ogg me@ubu:~$ ll
We used the chmod 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
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
free command shows us how much memory is free. We asked for it in
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.
Q to quit:
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:
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 thousands of files.
me@ubu:~$ locate libreoffice [|] grep dict
And now we’re down to a few dozen files.
How did we count those thousands of files? Easy:
me@ubu:~$ locate libreoffice [|] wc -l
This means we located the thousands of 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.
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.