site/wiki/pages/cli-for-beginners.md

403 lines
18 KiB
Markdown
Raw Permalink Normal View History

2018-07-20 18:58:05 +00:00
---
author: ~cmccabe
published: true
title: command line for absolute beginners
description: guide for those looking for a foothold in learning and using gnu+linux
2019-01-25 18:29:10 +00:00
category:
2022-09-29 18:53:09 +00:00
- beginners
- guides
- technical
2018-07-20 18:58:05 +00:00
---
2022-09-29 18:53:09 +00:00
So, you want to join a public-access shell community like tilde.team, but you don't yet have experience using GNU+Linux
or other UNIX-like operating systems? This tutorial is designed to give you enough guidance that you can get started and
move on to successfully directing your future learning. Once you get a basic level of self-sufficiency, tilde.team is a
great place to practice and learn more.
2018-07-20 18:53:06 +00:00
2019-01-25 18:29:10 +00:00
## GNU+Linux is a text-based operating system. And it takes work and thought to start using.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
You'll find a lot of people online arguing that GNU+Linux is _not_ a text-based operating system, and that it in fact
has a GUI interface just like Windows. It is true that you can use GNU+Linux through a graphical user interface (GUI)
like Gnome, or that you can use services from GNU+Linux servers like tilde.team through a web interface. But the people
who are so keen on GUIs are saying this to make GNU+Linux sound like an easy transition for Windows or Mac users.
However: (1) to really leverage the power of GNU+Linux, you need to learn to interact with it as a text-based system,
and (2) while it is different, it's not really that hard. It will take effort to learn the differences, but that effort
will pay huge dividends.
2018-07-20 18:53:06 +00:00
## How do I connect to a shell server?
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
The most common way to connect remote GNU+Linux system is with an SSH client. SSH stands for secure-shell. SSH allows
you to make a private connection between your computer and a shell server like [tilde.team](https://tilde.team), and it
ensures that nobody else along the wire can listen in on your connection. Check
out [our SSH page](https://tilde.team/wiki/ssh) for information on connecting to tilde.team over SSH.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
If you are having trouble with making your first SSH connection to tilde.team, or anything else while you're learning
from this tutorial, drop by the [tilde.team web chat](https://web.tilde.chat/) or email an admin for
help ([sudoers@tilde.team](mailto:sudoers@tilde.team)).
2018-07-20 18:53:06 +00:00
## What is a shell?
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
An operating system (OS) is the nuts and bolts that makes all the parts of your computer work together for you. At its
core, the OS is not friendly for day to day computer usage. A shell is a user friendly "wrapper" around the operating
system that allows you to use it easily. A shell can be graphical, like the Windows or Android GUIs. Or a shell can be
text-based. A text based shell, also called a command line interface (or CLI), is a tool you can use to control the
operating system by sending it text commands.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
What kind of things can you make the OS do? Things like opening files, listing the files in a directory, displaying the
current system load, or telling you what other users are currently doing.
2018-07-20 18:53:06 +00:00
## What is a command?
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
Commands are simple words, often abbreviated, that make the system do things when typed into the shell. Some simple
examples are 'ls' which lists the files in a directory, or 'cd' which changes your location to a new directory (cd =
change directory), or 'exit' which logs you out of your current shell session. There are thousands of useful commands,
but you only need to know a few to get started and be self-sustaining.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
This tutorial will teach you the few commands that should allow you to take care of yourself and start down the real,
longer-term path of self-directed learning. Once you're logged into tilde.team (or any GNU+Linux shell server), you can
practice the following commands as you learn them.
2018-07-20 18:53:06 +00:00
## What are the first commands a new user should learn?
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
When you're first starting to use a shell in a UNIX-like environment, you will want to be able to do the following
things:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
1. logging in and logging out
2022-09-29 15:14:01 +00:00
2. list the files or directories in a directory
3. move between directories
4. read, write, and save files
5. create new files or directories
6. move or copy files between directories
7. delete files or directories
8. download files from elsewhere on the internet
9. learn more about new commands
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
When you're logged into a shell, you should see a command prompt and a blinking cursor. At this point, simply type a
command and hit Enter to run it. You can try this as you work through learning the commands below.
2018-07-20 18:53:06 +00:00
### Logging In, with `ssh`
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
Recall from the How-Do-I-Connect section above that you can use a SSH client to log into tilde.team. Once you're logged
in, you can use the command line SSH client to log into any other shell server; in the example below, let's say you want
to log into tilde.town from tilde.team.
2018-07-20 18:53:06 +00:00
Skipping some specifics for now, you can log into tilde.town from a tilde.team shell by using SSH as follows:
2019-01-25 18:29:10 +00:00
> `ssh tilde.team`
2022-09-29 18:53:09 +00:00
Some shell servers allow you to log in with nothing more than a username and password. But increasingly, many servers (
like both tilde.team and tilde.town) require you to use ssh keys. To learn more about ssh keys, again, see
our [SSH page](ssh).
2018-07-20 18:53:06 +00:00
### Logging Out, with `logout` or `exit`
2019-01-25 18:29:10 +00:00
`logout` is a simple command you can use to log out of a shell. You could also use `exit`.
2018-07-20 18:53:06 +00:00
### Listing Files, with `ls`
2019-01-25 18:29:10 +00:00
To list the files in a directory, simply type `ls`. This will print a list of the files in your current directory.
2018-07-20 18:53:06 +00:00
> `ls`
### Changing Directories, with `cd`
2019-01-25 18:29:10 +00:00
2022-09-29 18:53:09 +00:00
You may move from one directory to another with `cd`. Wherever you are in the file system, you can type `cd` by itself
to return to your home directory:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `cd`
Change to the directory with your html files as follows:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `cd public_html`
### Read the Contents of a File, with `less`
2019-01-25 18:29:10 +00:00
2022-09-29 18:53:09 +00:00
If you're still in your public_html directory, you should see a file called 'index.php' when you use the `ls` command.
Let's peek inside 'index.php' as follows:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `less index.php`
2022-09-29 18:53:09 +00:00
`less` has opened the 'index.php' file for you to read. You cannot edit it; only read it. Type `q` (quit) to stop
viewing the file contents and return to the shell.
2018-07-20 18:53:06 +00:00
### Edit and Save Changes to a File, with `nano`
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
`nano` is one of many text editors availble for GNU+Linux. There are many more powerful editors, but we'll start with
this one because it is simple. Let's open your 'index.php' file and make some changes.
2018-07-20 18:53:06 +00:00
> `nano index.php`
2022-09-29 18:53:09 +00:00
Now you're viewing the contents of 'index.php' again, but this time you can change the contents. If you don't know HTML,
be careful here. Use your arrow keys to move the cursor down to the line that says the following:
2018-07-20 18:53:06 +00:00
`<p>Just log in with your secure internet shell to change this file!</p>`
2019-01-25 18:29:10 +00:00
Leave the `<p>` and `</p>` tags as they are, but change the sentence in between them.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
Now, save and quit by hitting the key combination Ctrl+x, and then typing 'y' in response to the question about wanting
to save the modified buffer.
2018-07-20 18:53:06 +00:00
2019-01-25 18:29:10 +00:00
Now you can pull up a browser to see the change at your tilde.team URL:
2018-07-20 18:53:06 +00:00
'https://tilde.team/~yourUserName'
### Create a New File with `nano`
2018-07-20 18:53:06 +00:00
Let's create a new file in your public_html directory, called 'testing.html'.
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `nano testing.html`
'testing.html' did not exist before you opened it with `nano`, so it was created for you.
2022-09-29 18:53:09 +00:00
Now, add some quick contents by opening the file for editing with `nano`, and adding whatever you want. Then Ctrl+x to
save, you will have created a new file.
2018-07-20 18:53:06 +00:00
Type `ls` to view the contents of your directory an confirm that you did indeed make the file.
Later in this tutorial, we will come back to this file and make it viewable in your web space.
### Create a New Directory, with `mkdir`
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
First, hop back to your home directory with the `cd` command (remember that `cd` from anywhere in the file system will
take you back to your home directory).
2018-07-20 18:53:06 +00:00
Now create a new directory called 'downloads' in your home directory:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `mkdir downloads`
Use `ls` to see that it was created, and even move into the new directory with `cd downloads`.
### Moving Files Between Directories, with `mv` or `cp`
2018-07-20 18:53:06 +00:00
First, `cd` back to your home directory, and use `nano` to create two new files called 'fileone.txt' and 'filetwo.txt'.
Lets move those into your 'downloads' directory using two different commands, to demonstrate how they workd differently.
Move 'fileone.txt' into 'downloads':
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `mv fileone.txt downloads/`
2022-09-29 18:53:09 +00:00
Now if you `ls` the contents of your home directory, you will no longer see 'fileone.txt', because it has been moved
into 'downloads'. If you 'ls' the contents of 'downloads' (a shortcut command is `ls downloads`), you will see it there.
2018-07-20 18:53:06 +00:00
Next, copy 'filetwo.txt' into 'downloads' as follows:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `cp filetwo.txt downloads`
2022-09-29 18:53:09 +00:00
Now if you `ls` the contents of your home directory, 'filetwo.txt' will still be there. This is because `cp` made a copy
of 'filetwo.txt' and put the copy in 'downloads'. It did not touch the original file in your home directory. Verify this
with `ls` in your home directory and in 'downloads'.
2018-07-20 18:53:06 +00:00
### Delete Files and Directories, with `rm` and `rmdir`
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
As long as you're in one of your own directories (e.g. your home, or 'downloads' or 'public_html'), you can create a new
files. Create a new file called 'testtrash.txt':
2018-07-20 18:53:06 +00:00
> `nano testtrash.txt`
Then save it as you have already learned, and confirm that it exists by listing (`ls`) the contents of the directory.
Now, you can delete the file with the `rm` (remove) command:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `rm testtrash.txt`
2022-09-29 18:53:09 +00:00
Notice that you don't get a warning that you're about to delete it, and you don't even get a confirmation that it is
deleted. You've learned your first command that you need to be careful with. If you delete an important file with `rm`,
it is gone forever.
2019-01-25 18:29:10 +00:00
2022-09-29 18:53:09 +00:00
You can delete directories the same way, only using the `rmdir` command (remove directory) instead of `rm`. If you
use `mkdir testtrash`, you can then delete it as follows:
2018-07-20 18:53:06 +00:00
> `rmdir testtrash`
Note that you can only delete empty directories with `rmdir`.
If you want to delete directories that still have contents in them, use the following:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `rm -rf directoryName`
2022-09-29 18:53:09 +00:00
Be very, very careful with this command. Many a user, new and seasoned, has been stung by hastily deleting directories
like this. This is also the source of the classic sysadmin joke/horror story about `rm -rf /` which deletes the entire
file system.
2018-07-20 18:53:06 +00:00
### Downloading Files, with `wget`
2018-07-20 18:53:06 +00:00
Now `cd` into your 'downloads' directory because we're going to use it for actual downloads.
2022-09-29 18:53:09 +00:00
Use the `wget` (WWW get) command to download a text copy of this tutorial from tilde.team user cmccabe's public_html
directory:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `wget https://tilde.team/~cmccabe/gnu-linux-toot.txt`
2022-09-29 18:53:09 +00:00
You will see output of the command that confirms it is downloading. You can also verify that it has downloaded with
your `ls` command. You can also peek at the contents with the `nano` or `less` commands that you learned above.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
If you know the URL of other files you'd like to download, you can grab those too, just swapping the URL above for any
URL:
2018-07-20 18:53:06 +00:00
> `wget [URL here]`
2022-09-29 18:53:09 +00:00
A brief note on security here, if you do pull any scripts from the Internet using `wget`, it's important that you do not
execute those scripts until you've read over what it does. Otherwise, you run the risk of compromising your account or
allowing other malicious actions to take place.
2018-07-20 18:53:06 +00:00
### Learn More About Commands, with `man` (and `help`)
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
At this point, you've learned most of the commands you need for basic self-sufficiency in a GNU+Linux shell environment.
With just a few more, you can go a long way. When you want to learn more about a command, you can look at its "man page"
with the `man` command. "Man pages" are the instruction manuals for most commands and programs in GNU+Linux.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
Try out `man` by looking at any of the commands you've learned already (except `cd`\*). For example, `man ls` would open
the man page for the `ls` command. When looking at a man page, type `q` at any time to quit and return to the shell.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
The `man` command will be one of your most valuable tools for as long as you're using the GNU+Linux shell. You will
always be learning new commands and new ways to use old commands, and `man` will help you do it.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
\* Note: technically speaking `cd` is a shell built-in, not a command. Make a mental note of that and you can learn more
about the distinction later. For now, note that you can use `help cd` to learn more about the `cd` command.
2018-07-20 18:53:06 +00:00
## Commands have options and arguments.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
When you look at the man page for a command like `ls`, you'll see in the DESCRIPTION section a number of **options**
that you can use to modify how the command works. They look like `-a` or `-h` or `-l`. Try adding the `-a` option
to `ls` and note the difference:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `ls -a`
2022-09-29 18:53:09 +00:00
The `-a` option now lists "all" contents of your directory, including "hidden" files (aka dot files). You could combine
the three options listed above in the form of `ls -alh` to list "all" files, in "long" form, and display file sizes in "
human" readable format. Most commands have
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
Commands also have **arguments**, or information passed into a command for some kind of processing. You have already
used these arguments when you told nano to open a file: `nano testtrash.txt`. In this case, "testtrash.txt" was the
argument to the nano command. You also used "testtrash.txt" as an argument to the `rm` command when you
did `rm testtrash.txt`.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
Commands will often combine options and arguments, sometimes in specific sequences. You can learn about these when read
a command's man page.
2018-07-20 18:53:06 +00:00
## The Filesystem Hierarchy
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
You already know that you get dropped into your "home" directory when you first log in. Your home directory is just one
of many, many other directories on the system. All of these directories are organized under one master directory called
the "root directory". The root directory is often referred to with a single forward slash, like this: /
2018-07-20 18:53:06 +00:00
You can list all the directories at the root level by using the `ls` command again, as follows:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `ls /`
2022-09-29 18:53:09 +00:00
Want to check out some of the directories you see in root? You could either `cd` into them and `ls` the contents, or
just `ls` the comments directly as follows:
2018-07-20 18:53:06 +00:00
> `ls /etc`
2022-09-29 18:53:09 +00:00
This would display the contents of the "etc" directory, which itself lives in the "root" directory. Notice that the
command uses "/" + "etc" to create a path to the destination. In "etc" is another directory called "cron.d", and you can
use the same principle to view its contents:
2018-07-20 18:53:06 +00:00
> `ls /etc/cron.d`
2022-09-29 18:53:09 +00:00
You now know enough to look around the file system. Note that most GNU+Linux systems (like tilde.team) adhere somewhat
to an organization scheme called the Filesystem Hierarchy
Standard ([Wikipedia link](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard)). This is another subject for
you to read up on later.
2018-07-20 18:53:06 +00:00
## How can I keep my things private, or share them with others?
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
As you explore the filesystem, you might bump into some directories that won't let you in. For example, if you try
to `cd` into the home directory for the root user (not the same as the root directory), you'll see this error: "/root:
Permission denied". This is because GNU+Linux systems maintain a "mode" for each file that limits which users can read,
write or execute it.
2019-01-25 18:29:10 +00:00
If you don't own a file, then you can't change its mode. This is a basic security principle in GNU+Linux systems.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
For the files you own (i.e. the files within your home directory), you can change the file modes yourself. You do this
using the "change mode" command, `chmod`.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
Each file has three permission levels: for the file owner, for members of the file's group\*, and for all other system
users. For each level, you can permit any combination of "read", "write", and "execute" permissions.
2018-07-20 18:53:06 +00:00
2019-01-25 18:29:10 +00:00
(/\* Do a web search for GNU+Linux users and groups to learn more about this important concept.)
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
You can change a file's mode with `chmod` one of two ways. The first is a symbolic way in which you add or subtract '
r', 'w', and/or 'x' (read, write, execute) to 'u', 'g', or 'o' (user, group, or other). For example:
2018-07-20 18:53:06 +00:00
> `chmod g+x filename.txt`
This gives 'execute' privileges to members of filename.txt's group.
2022-09-29 18:53:09 +00:00
You can also use `chmod` numerically, through which you may set the user, group and other permissions all at once. For
example:
2019-01-25 18:29:10 +00:00
2018-07-20 18:53:06 +00:00
> `chmod 755 filename.txt`
This gives the owner read, write and execute privileges, and gives only read and execute privileges to group and other.
Use `man chmod` to get a fuller understanding of `chmod`.
2019-01-25 18:29:10 +00:00
To get an interactive, visual feel for numeric file modes, try the tilde.team [file mode widget](/~cmccabe/mode-gui.php)
2018-07-20 18:53:06 +00:00
--
2022-09-29 18:53:09 +00:00
Finally, remember that 'testing.html' file we made above? Let's use that as an example of how you can control who can
view your files. Use the following to make the 'testing.html' file visible in your website:
2018-07-20 18:53:06 +00:00
2019-01-25 18:29:10 +00:00
> cd ## to return you to your home directory
> `chmod 644 public_html testing.html`
2018-07-20 18:53:06 +00:00
Now if you bring up the following URL (with your username) in a browser, you should see the testing.html file you made.
https://tilde.team/~username/testing.html
## The End (of The Beginning)
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
There you have it -- you know about logging in, using basic shell commands, and the file system, and you're now
self-sustaining (and a little more).
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
tilde.team is about community, but it is about community of individuals who work hard to learn. what you have just
leaned will give you a platform on which you can learn by doing and trying things out.
2018-07-20 18:53:06 +00:00
2022-09-29 18:53:09 +00:00
Below are some other common programs you'll likely want to use. Most of these have man pages, so you can read more about
them. Others you'll just have to try out to see how they work.
2018-07-20 18:53:06 +00:00
## List of Common Programs
`mutt`, `alpine` - for email
`wget`, `curl` - for grabbing files from elsewhere
`weechat`, `irssi` - for irc
`scp` - for securely moving files between networked systems. this copies files (i.e. 'cp') over ssh, hence 'scp'
`lynx` - web browsing
`cron` - scheduling recurring jobs (job = sequence of commands, often stored in a script)
`dict` - dictionary for definitions and synonyms
`aspell` - a program for spell checking
`motd` - list the message of the day, which on tilde.team displays all the other commands below
## some tilde.team specific programs
`bbj` - a bulletin board for asynchronous discussions
`tilde` - a manager for user-submitted scripts
2022-09-29 18:53:09 +00:00
`webirc` - register for [thelounge](https://irc.tilde.team) webchat (in case you don't like weechat or irssi, or want to
get push notifications for mentions and query messages :)
2018-07-20 18:53:06 +00:00
`chat` - open `weechat` preconnected to our irc
## Some shell use cases
And in this corner, we shall describe some common activities people perform in a shell... [Feel free to add here.]
## Other Intro-to-Linux Material
2022-09-29 18:53:09 +00:00
Not suprisingly, you'll find a lot of other intro material online or in your local library. Here are a few that have
been mentioned by tilde.team members:
2018-07-20 18:53:06 +00:00
Terminus - an interactive game-like introduction to shell commands
http://www.mprat.org/Terminus/