User Tools

Site Tools


tutorials:2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
tardis_beginner_tutorials:2 [2017/04/10 18:10] – Slight grammar change _neaniastutorials:2 [2023/04/03 11:49] tcmal
Line 1: Line 1:
 +====== 2. Using the command line ======
  
-======= Tutorial 2: Basic navigation of the command line environment ======= 
  
 +When you log in via SSH, you are presented with the command line interface, by means of the shell.
 +The current default is Bash. Bash stands for "Bourne Again SHell", and is one (but the most commonly used by far) of many shells available for Linux.
 +A shell is is what interprets what you type into the prompt and makes things happen - different shells do things differently.
 +Your default shell is bash, although you can change it if you wish, but this document will only give you a brief introduction to using the shell to run programs and use the filesystem.
  
-When you log in via SSH, you are presented with the command line interface, by means of the shell. The current default is Bash. Bash stands for "Bourne Again SHell", and is one (but the most commonly used by far) of many shells available for Linux. A shell is is what interprets what you type into the prompt and makes things happen - different shells do things differently. Your default shell is bash, although you can change it if you wish, but this document will only give you a brief introduction to using the shell to run programs and use the filesystem.+===== The Prompt =====
  
  
-===== Prevent your home directory from being globally accessible =====+The first thing you should notice is that your shell puts something at the start of every line. This is called the prompt and it looks something like this:
  
 +<code>
 +tcmal@sontaran:~$
 +</code>
  
-Since Linux is a Unix-like system, it inherits many features from Unix's design. On multi-user systemsthis includes having everyone's home directories (where you store your own files and program configurationsglobally readable by any user. +The default prompt tells us our username (''tcmal'')the host we're on (''sontaran''), and the path we're at (''~''which is an alias for your home directory ''/home/<username>'').
-This may not be desirable to you, especially if you use your shell account for idling on IRC all day, and have a few private messages you might not want everyone to be able to see (howevernote, do not have an expectation of privacy on insecure, multi-user systems).+
  
-In the shelltype **chmod -R o-rwx ~**+We can enter commands after the prompthit enter, and once they're done executing we'll get another prompt back. 
  
-The tilde '~' is shorthand for your home directory, /home/USERNAME/. In our default, Bash (and most other shells), this is also referenced by the variable $HOME (variables are accessible in shell scripts and the shell more generally by appending $ to the start of them).+<code> 
 +tcmal@sontaran:~$ whoami 
 +tcmal 
 +tcmal@sontaran:~$ hostname -f 
 +sontaran.tardisproject.uk 
 +tcmal@sontaran:~$ pwd 
 +/home/tcmal 
 +</code>
  
  
-===== Running programs =====+While you're at your prompt, you can also use the arrow keys to scroll back up your command history, and Ctrl+R to search through it.
  
 +===== Moving around =====
  
-To run a programyou need to know the name of that program. All you need to do is type that name, or if it is a specific script or program in a non-standard place, you will need to type the path of it as well (more on this later). A lot of the non-interactive Linux programs take command-line options which are additional words or filenames you type after the name of the program. For example, the Linux program **cat** (short for "concatenate") can display the contents of text files. Try typing: +As mentioned aboveour current directory is shown at the start of our prompt. You'll almost always start off in ''~''.
-**<code>cat /etc/motd</code>** +
-This simply dumps the content of the file "motd" in the directory "/etc" to screen. Command-line options also often have one-letter flags which tell the program what mode to run in, or how to display the output. For instance, **ls** lists the content of a directory, much like "dir" in a windows command prompt. +
-Try typing **ls /** +
-Now try typing **ls / -l** +
-This simply lists the files and directories in the root of the filesystem (referred to simply as "/"), but the second one gives the "long" output. You can usually get a quick listing of the command-line flags available for a program by typing **[program name] --help** +
-It is worth mentioning that the Linux command-line is case sensitive, unlike windows - this means that **cat** is different from **CAT** and from **Cat** etc. - only one will work, because there is no program called "CAT", but if there were you could call it by using the upper case. This applies to all filenames and command-line options etc.+
  
 +We can use ''cd'' to change the directory we're in. It takes the name of the directory we want to change to, with two special cases - ''.'' always means stay in the same directory and ''..'' means go one level up.
  
-===== Learning how to use programs =====+<code> 
 +tcmal@sontaran:~$ cd .. 
 +tcmal@sontaran:/home$ cd tcmal 
 +tcmal@sontaran:~$ 
 +</code>
  
 +This is more interesting if we make some directories, so let's make a few test ones using ''mkdir'':
  
-The best way to learn how a program works and how to use it is by reading the man page (manual page) built into the system about that programTo find out what a program does and how to use it, simply type man [program name]Try it now - type+<code> 
-**<code>man cat</code>** +tcmal@sontaran:~$ mkdir test 
-You can scroll up and down using up and down arrows, page up and page down, and space and return. Don't worry if you don't understand everything written there, you're not expected to memorize all of a program's options - that's why they're so easily accessible in man pages! Press q to quit the man page reader.+tcmal@sontaran:~$ cd test 
 +tcmal@sontaran:~/test$ mkdir test2 
 +tcmal@sontaran:~/test$ cd test2/ 
 +tcmal@sontaran:~/test/test2$ pwd 
 +/home/tcmal/test/test2 
 +tcmal@sontaran:~/test/test2$ cd .. 
 +tcmal@sontaran:~/test$ 
 +</code>
  
-I should also mention the other manual command, info which (most of the time) shows the same info as man, but uses Emacs to display it. However, certain programs (such as tar) have much more useful information in the info page than the man page, because certain GNU programmers are very awkward about these thingsYou don't really need to worry about this though.+''pwd'' outputs the full path to whatever directory we're currently in. This path is absolute because it starts with a ''/'' - the filesystem root.
  
 +We can use ''ls'' to see what's in our directory.
  
-===== Finding a program for your purpose =====+<code> 
 +tcmal@sontaran:~/test$ mkdir test3 
 +tcmal@sontaran:~/test$ ls 
 +test2  test3 
 +tcmal@sontaran:~/test$ ls -al 
 +total 16 
 +drwxr-xr-x 4 tcmal 1004 4096 Oct 26 22:49 . 
 +drwxr-xr-x 7 tcmal 1004 4096 Oct 26 22:49 .. 
 +drwxr-xr-x 2 tcmal 1004 4096 Oct 26 22:49 test2 
 +drwxr-xr-x 2 tcmal 1004 4096 Oct 26 22:49 test3 
 +</code>
  
 +As well as //positional// arguments (like ''test'' in ''cd test''), most Linux commands will also accept flags - starting with either a single ''-'' or two.
 +In ''ls -al'' we've passed 2 //short flags// - ''a'' and ''l''. These correspond to 'all' and 'long', and give us the long printout with the 'hidden' directories ''.'' and ''..''. Because they're short flags, we only type one ''-'', although we could type ''ls -a -l''
  
-If you know what sort of program you want to runbut aren't sure of the name (or if such a program exists), you can use **apropos** to search for a program by functionFor instance, say we want to find an IRC client but we don't know the names of any. We type: +Now that we know how to make directorieslets clean up after ourselvesReplace ''<username>'' with whatever your username is.
-**<code>apropos irc</code>** +
-But we get quite a lot of matches, most of which are no use to us. This is because the search has turned up a load of results where "irc" was part of another word, such as "circular". If we have a quick look at man apropos we find out that the -e flag searches for exact matches: +
-**<code>apropos -e irc</code>** +
-Yay, we've narrowed our matches down to what we wanted! Alternatively we could have tried **apropos "irc client"** which would have yielded the same results. However, typing **apropos irc client** without the quotes would have returned twice the unwanted results, as it would have searched for both "irc" and "client" and given you results for either. The quotes tell Bash to treat what you put in them as one continuous string.+
  
 +<code>
 +tcmal@sontaran:~/test$ cd /home/<username>
 +tcmal@sontaran:~$ rm --recursive test 
 +tcmal@sontaran:~$
 +</code>
  
-===== Navigating the filesystem =====+Note that we gave ''cd'' an absolute path - this is totally fine. We also could have said ''cd ~'' - bash would have replaced the ''~'' with the same thing before it called cd.
  
 +When we call ''rm'', this time we pass it the //long flag// ''recursive''. Unlike short flags, you need to pass each long flag with a seperate '--'.
 +The recursive flag is needed when you're deleting directories - like most long flags there is a corresponding short flag, so we could have said ''rm -r test''
  
-Just like in windows, you can use **cd** to change directory. Why don't we use it in combination with ls to have a browse around the filesystem right now! To change to a directory, simply type **cd** and that directory's name. You'll notice the directory you're in is shown on your prompt, and can also be shown by typing **pwd**. Now let's take a spin around the filesystem and see what we can see! Type ls between each step to get a feel for where you are. +===== Learning how to use programs =====
-To go up a step in the directory tree type cd .. +
-Notice you are now in the /home/ directory with all the users' home directories. To return to your specific home directory, type **cd [your username]** +
-To go to the root of the filesystem type **cd /** +
-You can also use tab completion - this is where you press tab partway through typing the name of the directory (or filename) and the shell completes it for you (if there is only one possible match). Otherwise you will hear a beep. If you press tab a second time, it will list all the possible completions for what you've typed already. Type **cd ~** to return to your home directory.  +
-You can navigate faster by using absolute paths. The current directory can be printed to the shell by typing **pwd** - this is useful to remember for scripts that you write for the shell. If you wanted to look at the source of my web page, you could type:+
  
-**<code> +The best way to learn how a program works and how to use it is by reading the man page (manual page) built into the system about that programTo find out what a program does and how to use it, simply type man [program name]. Try it now -''man cat''.
-cd .. +
-cd .. +
-cd var +
-cd autofs +
-cd www +
-cd users +
-cd rain +
-cd pages +
-</code>** +
-But it is infinitely more efficient to type **cd /var/autofs/www/users/rain/pages/** all in one go. You could also type that without the first "/" but only if you were in the root directory - the first "/" means "from root"The last "/" is optional - Bash is intelligent enough to know something is a directory even if you don'use it, but tab completion puts it in. From here we can now go back to our homepage using absolute paths type **cd /home/[your username]**+
  
-All this takes a little getting used to, and it's useful to have command reference nearby I recommend printing this handy [[tardis_beginner_tutorials:cheatsheet|Tardis command-line cheatsheet]].+This takes us away from our shell and into **pager**. You can scroll up and down using up and down arrowspage up and page down, and space and return. Don't worry if you don't understand everything written there, you're not expected to memorize all of program's options that's why they're so easily accessible in man pages! Press q to quit the man page reader.
  
 +===== Finding a program for your purpose =====
  
-===== Changing your shell ===== +If you know what sort of program you want to run, but aren't sure of the name (or if such a program exists), you can use **apropos** to search for a program by function. For instance, say we want to find an IRC client but we don't know the names of any. We type: ''apropos irc'' 
 +But we get quite a lot of matches, most of which are no use to us. This is because the search has turned up a load of results where "irc" was part of another word, such as "circular". If we have a quick look at man apropos we find out that the -e flag searches for exact matches: ''apropos -e irc''
  
-If you decide you want to try another shell, you can type the name of it, e.g. **zsh** (Z Shell) to start it. However, to change it so that it is your login shell, the traditional **chsh** will not workThis is due to LDAP (the way that Tardis manages accounts). Please ask on our IRC channel if you want to change it.+Yay, we've narrowed our matches down to what we wanted! Alternatively we could have tried ''apropos "irc client"'' which would have yielded the same results. However, typing ''apropos irc client'' without the quotes would have returned twice the unwanted results, as it would have searched for both "irc" and "client" and given you results for eitherThe quotes tell Bash to treat what you put in them as one continuous string.
  
-**Next: [[tardis_beginner_tutorials:3|Mail]]**