Welcome to the Linux world! Whether you’re just starting on an intro to CS course or wandering through your very first UNIX-based home server, learning how to navigate around in Linux (more specifically, using the command line) is going to make your life much easier and much much more powerful.
There are many distributions of Linux out there. Each of them has its own variation of commands to work with. Specifically for this tutorial, I’m going to be using Ubuntu 14.04.2 LTS on Cloud9. Keep in mind that all commands demonstrated here will probably be usable on earlier and future versions of Ubuntu, as well as Debian and Debian-based distros.
To check for the distribution and version number, simply type this into the terminal.
1cat /etc/issue
Not only do different distros have different commands, they also have different default shell (scripting environment). What this means is that this window — often called the terminal — works differently.
In this case, I’m going to be using Bash (short for Bourne-Again Shell), which is the default shell for Ubuntu and Debian.
In Bash, you’ll typically be presented with this kind of prompt:
where brillydev
is my username, and ~/workspace
is the directory I’m currently in, followed by a dollar sign ($
), or depending on some user, a hash (#
).
Because I’m lazy, I’m going to omit that brillydev:~/workspace
, and leave only $
when I show you my terminal window. You don’t need to type all of these brillydev:~/workspace $
when you type the command.
Oh, and keep in mind that Linux is case-sensitive. A
is different from a
!
mkdir
(make directory) is used to create a directory.
1$ ls
2apple.txt foo/ somefiles.txt
3
4$ mkdir bar
5$ ls
6apple.txt bar/ foo/ somefiles.txt
To move files, the command mv
(move) is used.
1$ ls
2apple.txt bar/ foo/ somefiles.txt
3
4$ mv apple.txt foo
5
6$ ls
7bar/ foo/ somefiles.txt
8
9$ ls foo
10apple.txt
Interestingly, it can also be used to rename things as well!
1$ cd foo
2$ ls
3apple.txt
4
5$ mv apple.txt hello.txt
6$ ls
7hello.txt
And if you’re tired of typing ls
everytime after the command, a semicolon can condense your code into one line. This produces the same effect:
1$ mv apple.txt hello.txt; ls
2hello.txt
rm
(remove) removes files. Take note that this action cannot be undone.
1$ cd ..
2$ ls
3bar/ foo/ somefiles.txt
4
5$ rm somefiles.txt
6rm: remove regular file ‘somefiles.txt’? (y/n) y
7
8$ ls
9bar/ foo/
rm
cannot be used with directories, unless accompanied by -r
flag (short for recursive). What this means is that rm
will go into every directory in the specified path and delete files one by one, recursively up each directory inside. If you don’t understand this, it’s fine, unless you do programming. Just remember that it deletes all the files and directories in the specified path.
1$ ls
2bar/ foo/
3
4$ rm -r foo
5rm: remove regular file ‘apple.txt’? (y/n) y
6rm: remove ‘foo’? (y/n) y
7
8$ ls
9bar/
If you’re tired of typing y
s for all the files, rm -rf
can help you. Be extremely careful, however, as this command-flag combination is incredibly dangerous. If you accidentally type the wrong path — boy, there’s no going back!
If you try to use rm
alone for directories, you’ll be faced with this:
1$ ls
2bar/
3
4$ rm bar
5rm: cannot remove ‘bar’: Is a directory
In this case, rmdir
(remove directory) can be used instead. Note also that this command can only be used with empty directories.
1$ ls
2bar/
3
4$ rmdir bar
5$ ls
6$
When you wish to create new (empty) files, use touch
.
1$ ls
2$
3$ touch file1 file2.txt file3
4$ ls
5file1 file2.txt file3
Copying is undeniably one of the greatest inventions of all time. To do that, use cp
(copy).
1$ ls
2file1 file2.txt file3
3
4$ cp file2.txt file2copy.txt
5
6$ ls
7file1 file2.txt file2copy.txt file3
To copy a directory recursively, use cp -r
.
You’ll encounter this quite often in the future. So it’s probably good to know it now. To create a symbolic link (Google for explanations), use ln -s
.
1$ ln -s ~/apache ~/Desktop/apache
echo
prints out whatever you say to it.
1$ echo "hello world"
2hello world
To display the content of a file, use any of the commands above.
1$ cat hello.txt
2hello, world! I am a little puppy.
3Lorem ipsum blah blah blah.
4Three lines are enough for a sample, aren't they?
These commands don’t do exactly the same thing. Do some little experiments to find out how they differ :)
grep
is an extremely powerful tool to search the content of a file. It prints out the line containing the word(s) specified, if it is present in the file. For example.
1$ grep "blah blah blah" hello.txt
2Lorem ipsum blah blah blah.
(actual result will have blah blah blah
highlighted)
There are a lot more powerful tools out there, such as sed
and awk
, that will unleash your potential in Linux. We can’t cover all of them here, so be sure to do some research to find out more about them!
If you happen to forget, or see a new command you’ve never heard of, apart from Google, your user manual will also be incredibly helpful. To use it, simply type man
.
1$ man mkdir
2[LONG INSTRUCTIONS ON HOW TO USE mkdir]
|
(that vertical bar right above the Enter
key) is called a pipe. It redirects the output of the left command to the input of the right. For example:
1$ echo “hello world
2
3You’re really cute.” | grep “cute”
4You’re really cute.
Redirections are similar to pipe; but instead of passing the output as an input, they save the output to a file, or read the content of a file.
>
saves the output to a file<
reads the content of a file1$ ls foo > abc.txt
2$ cat abc.txt
3bar/
4somefiles.txt
5
6$ wc < abc.txt
721
Redirection overwrites the files each time it is used. Use appends (>>
and <<
) should you decide to not overwrite them, and append instead.
1$ ls foo >> abc.txt | grep "bar"
2bar/
3bar/
Working with computers nowadays usually requires external software from the Internet. You can choose to download these external software files manually or use a package manager to manage all the stuff for you. apt-get
(advance packaging tool) is the default package manager for Ubuntu and Debian.
1$ apt-get install vim
2$ apt-get remove vim
3$ apt-get update
If you ever get stuck, sudo
can help you.
wget
is a tool to download files from the Internet.
1$ apt-get install wget
2$ wget https://www.google.com/thefilethatIwant.zip
Sometimes you’ll get files in archived form. To extract them, use unzip or tar.
1$ apt-get install unzip
2$ apt-get install tar
3
4$ unzip thatfile.zip
5$ tar -xzvf anotherfile.tar.gz
vi
or vim
(vi improved) is an awesome tool to do all things text. It’s incredibly difficult to use at first, but through time, you’ll realize that it’s one of the best out there.
1$ apt-get install vim
2$ vim sometext.txt
For instructions, I recommend this awesome interactive guide:
If you’re kinda lazy to learn vim
, nano
is a simple intuitive alternative.
1$ apt-get install nano
2$ nano sometext.txt
All-time rival of vim
, here comes emacs
.
1$ apt-get install emacs
2$ emacs sometext.txt
Remember this?
1$ ls -l
2total 8
3-rw-r--r-- 1 brillydev brillydev 0 Oct 22 13:18 apple.txt
4drwxr-xr-x 2 brillydev brillydev 4096 Jan 31 03:55 bar/
5drwxr-xr-x 2 brillydev brillydev 4096 Oct 22 13:18 foo/
6-rw-r--r-- 1 brillydev brillydev 0 Feb 6 14:08 somefiles.txt
Those arcane characters on the leftmost column are where your permission settings lie. To characterize, there are three main permissions you can set for a file or a directory: read, write and execute, represented by r
, w
and x
respectively.
The permissions are arranged in the following order: user, group and others.
For example,
1-rw-r--r-- 1 brillydev brillydev 0 Oct 22 13:18 apple.txt
This means that the user (owner of apple.txt
) can read and write to apple.txt
, the user group which the owner of apple.txt
is in, as well as other people from somewhere else can only read apple.txt
.
Execute, in the context of a directory, also means access. Hence if you deny permission to execute a directory, you can’t access it.
To change permissions, chmod
(change mode) can be used. I leave it to you to interpret what this means :P
1$ sudo chmod 777 apple.txt
2$ sudo chmod a-r apple.txt
Similar to chmod
, chown
(change owner) changes the owner of the file.
1$ chown root apple.txt
1$ history
1$ cal
2 February 2016
3Su Mo Tu We Th Fr Sa
4 1 2 3 4 5 6
5 7 8 9 10 11 12 13
614 15 16 17 18 19 20
721 22 23 24 25 26 27
828 29
You can find out more about ssh
(secure shell) from the Internet.
1$ ssh http://prakrit.d/
1$ sudo apt-get install cowsay
2$ cowsay -f ghostbusters "Hi, How are you"
1$ sudo apt-get install figlet
2$ figlet "Welcome"
1$ sudo apt-get install fortune
2$ fortune
1$ sudo apt-get install telnet
2$ telnet towel.blinkenlights.nl
Not a command, but helpful to know. If you notice, all the commands here are really just compiled binary files underneath the hood. To know where they stay, check out:
Phew! That was an awful lot of stuff! Use it often, and you’ll remember all the commands by heart. For now, this cheat sheet may be of help.
Keep in mind that there are still tons of things which I did not cover in this tutorial. In fact, I didn’t even go into details about how these commands work. Because this tutorial is only designed to be a broad overview (for absolute beginners), I encourage you to dive in further and actually do stuff with it.
But if you’re still not very confident, or maybe you got lost in this tutorial, that’s okay too! There are many alternatives out there on the Internet, but be warned that not a lot of them are beginner-friendly. I also recommend checking out Ryan's Linux Tutorial for more explanations and use cases.
Good luck :)