Command Line Basics

Table of Contents

Command Line Basics

Below, dollar sign “$” is the normal user command prompt, user does not write that. After a command, hash mark “#” is comment character, rest of the line is ignored.


Moving and looking around

List files in working directory (commands affect these files by default)

$ ls

Print working directory (this is where you are)

$ pwd

Change directory (to “terosdir”, under working directory)

$ cd terosdir/

Change directory up. Path printed by pwd becomes shorter. Note space between command “cd” and directory “..”.

$ cd ..

View text file tero.txt (Space ” ” shows next page, “b” shows previous page, slash “/” searches, “q” exits)

$ less tero.txt

Any command output can be read one screenfull at a time by piping the output to less. On Finnish keyboard, pipe character is created by pressing AltGr (right of space) and “<>|” (between left shift and Z):

$ ls /etc |less


Relative and Absolute Path

Path relative to working directory does not start with a slash.

Example. If ‘ls’ shows “terosdir”, you can use a relative path. To change directory to “terosdir”, under current directory:

$ cd terosdir

Absolute path starts with a slash “/”. The first slash in path refers to topmost directory, the root directory, whose name is simply “/”. You can go up to root directory “/” by commanding ‘cd ..’ many times, then finally checking path with ‘pwd’.

Change directory to etc, that is under the root dir “/”. With absolute path, working directory does not matter.

$ cd /etc

Final slash after path does not mean anything, and these two commands mean exactly the same:

$ cd terosdir/
$ cd terosdir


File Manipulation

Easiest text editors are pico and nano.

$ nano FOO.TXT

Use CTRL-X y <enter> to save and exit. You can see all commands at the bottom of nano screen. There the hat character “^” means control, for example “^X” is the same as ctrl-X.

Filenames don’t have to be ALL CAPS, capitalization is used just for readability. If FOO.TXT does not exist, it is created. Files can have any suffix, and text file names don’t often end with “.txt”.

Make a new directory (ie. folder):

$ mkdir NEWFOLDER

Move or rename directory or file OLDNAME to target NEWNAME. If NEWNAME does not exist, OLDNAME is renamed to NEWNAME.

$ mv OLDNAME NEWNAME

If both parameters are files, NEWNAME is overwritten. Usually no warnings or questions are shown.

If target is a directory, OLDNAME is moved there.

$ mv SOMEFILE NEWDIR/

Copy ORIGINAL to COPY. “-r” means recursive, ORIGINAL is copied with contents if it is a directory.

$ cp -r ORIGINAL COPY

Remove a file called JUNK.

$ rm JUNK

Remove FOLDEROFJUNK/ and its contents. Usually no warnings or questions are shown.

$ rm -r FOLDEROFJUNK

There is no trash with rm. Think before you type.


SSH Remote Control

Open a remote command shell in a very secure way. Here, username is tero and server is example.com.

$ ssh tero@example.com

A command line just like yours opens. Anything you type is sent to remote machine and the answer is sent back to you. You can use ‘w’ to see who else is on the same machine.

Exit back to your own machine with ‘exit’

remotecomputer$ exit

Securely copy FOLDER to a folder in remote machine. Scp is run on your own machine. If you are connected to remote computer with ssh, ‘exit’ first.

$ scp -r FOLDER tero@example.com:public_html/

Syntax of remote path is login@server:path_from_your_home_dir. Note that there are no spaces between login name and path_from_your_home_dir. Server and path are separated with a colon “:”.


Help

Show manual page of a command

$ man ls

Manual page is shown in ‘less’. Use space ” ” for next page, “q” to quit. “b” Moves backwards, slash “/” searches.

Many commands have a short builtin help

$ ls --help
$ wget -h


History and Guessing

Tab (above caps lock) twice shows what you can type next

$ ls /etc/re[tab][tab]
reportbug.conf  resolvconf/     resolv.conf

Tab also fills commands if it is unambiguous

$ ls /etc/resolv.[tab]
$ ls /etc/resolv.conf

Always use tab when entering filenames and paths to avoid typos.

Arrow keys (between numpad and alphabeth characters, below del, end, PgDown) move in current command line and command history.

Up arrow shows previous command.

Left and right arrows allow you to modify current command line.

History commands you have ran

$ history

This history can be searched with ctrl-R (^R).


Useful commands

$ wget   # -r -np --convert-links
$ links   # or one of lynx w3m glinks elinks w3
$ w
$ finger
$ ping   # ctrl-C


Important directories

To find files in Linux, you need to remember just a few directories. They are defined in Filesystem Hierarchy Standard, so they are the same in all Linux systems.

Does it have your name in it? If some file is related to a specific user, the path has the name of that user. For example, tero’s desktop is “/home/tero/Desktop/”. Settings for web server called “apache2” are in “/etc/apache2/”.

/           Root directory, the top of the filesystem. There are no drive letters
            in Linux, everything is under /.
/home/      Contains home directories for all users.
/home/tero/ Home directory of user "tero". The only place where user "tero" can
            permanently store data.
/etc/       All system wide settings. They are in human readable, plain text files.
/media/     Removable media, such as /media/cdrom/ or /media/usbdisk/
/var/log/   System wide logs, such as /var/log/syslog or /var/log/apache2/error.log


What Next?

After you have learned these important commands by heart, have a look at commands for admin.

Edit: Fixed link.Fixed Typo.


Posted in Old Site | Tagged , , , | 39 Comments

39 Responses to Command Line Basics

  1. Pingback: Aikataulu – Linux palvelimet ict4tn021 4-ti ja 5-to – alkusyksy 2017 – 5 op | Tero Karvinen

  2. Pingback: Aikataulu – Palvelinten hallinta ict4tn022-2 – 5 op, uusi OPS – loppukevät 2017 p2 | Tero Karvinen

  3. Pingback: Aikataulu – Linuxin keskitetty hallinta – ict4tn011-11 – loppukevät 2017 p2 | Tero Karvinen

  4. Pingback: H2 SSH – Linux palvelimet kurssin raportit

  5. Pingback: Aikataulu – Linux palvelimet ict4tn021 2-ti ja 3-ke – Alkukevät 2017 – 5 op | Tero Karvinen

  6. Pingback: Automatically Install a List of Software to Windows – Chocolatey Puppet Provider | Tero Karvinen

  7. Pingback: Aikataulu – Linux palvelimet ict4tn021-1 – 5 op – alkusyksy 2016 | Tero Karvinen

  8. Pingback: Aikataulu – Linuxin keskitetty hallinta – ict4tn011-10 – loppusyksy 2016 | Tero Karvinen

  9. Pingback: Aikataulu – Linux palvelimena ict4tn003 22 ja 23 – alkusyksy 2016 | Tero Karvinen

  10. Pingback: Aikataulu – Linuxin keskitetty hallinta – ict4tn011-9 – loppukevät 2016 | Tero Karvinen

  11. Pingback: Aikataulu – Linux palvelimena ict4tn003-19 – loppukevät 2016 | Tero Karvinen

  12. Pingback: Playing around with some Linux command lines – Linux Booklet

  13. Pingback: New Default Website with Apache2 – Show your homepage at top of example.com, no tilde | Tero Karvinen

  14. I Just realised that I didn’t post anything in moodle, all done now.

  15. Pingback: Aikataulu – Linux palvelimena ict4tn003-20 ilta – keväällä 2016 | Tero Karvinen

  16. Pingback: Implementing Package-File-Service Pattern as a Salt Stack PyObjects Function | Tero Karvinen

  17. Pingback: Linux Basics – Week 4 | girlinux

  18. Pingback: Command Line Basics: | tatianalaneva

  19. Pingback: Linux command line | Elizaveta Kantina's blog

  20. Pingback: Aikataulu – Linux palvelimena ict4tn003-17 ilta ja -18 päivä syksyllä 2015 | Tero Karvinen

  21. Pingback: Ratkaise valitsemasi Työasemat ja tietoverkot koe. Raportoi. | kirriii

  22. Pingback: Työasemat ja Tietoverkot: Uusia komentoja | mikaelsalmela

  23. Pingback: Linux Palvelimena kotitehtävä 1 | Esan yrityksiä

  24. Pingback: Aikataulu – Linux palvelimena ict4tn003-11 ja -12 keväällä 2014 | Tero Karvinen

  25. Pingback: Aikataulu – Linuxin keskitetty hallinta – ict4tn011-4 syksyllä 2013 | Tero Karvinen

  26. Pingback: Aikataulu – Linux palvelimena ict4tn003-9 ja -10 syksyllä 2013 | Tero Karvinen

  27. Pingback: Hello Puppet revisited – on Ubuntu 12.04 LTS | Tero Karvinen

  28. Pingback: Etäkäyttö « kennetk

  29. Pingback: Aikataulu – Linuxin keskitetty hallinta – ict4tn011-3 | Tero Karvinen

  30. Pingback: Aikataulu – Linux palvelimena ict4tn003-7 keväällä 2013 | Tero Karvinen

  31. Pingback: English in Finland on Ubuntu | Tero Karvinen

  32. Pingback: Git from Offline to Network | Tero Karvinen

  33. Pingback: Aikataulu – Linuxin keskitetty hallinta – ict4tn011-2 | Tero Karvinen

  34. Pingback: Aikataulu – Linux palvelimena ict4tn003-4 ja ict4tn003-6 syksyllä 2012 | Tero Karvinen

  35. Pingback: Hello Puppet – on Ubuntu 12.04 LTS | Tero Karvinen

  36. Pingback: Aikataulu – Linuxin keskitetty hallinta – ict4tn011-1 | Tero Karvinen

  37. Pingback: Ideas for Automation with Centralized Configuration Tool | Tero Karvinen

  38. Pingback: Update All Your Computers with a .DEB Repository | Tero Karvinen

  39. Pingback: Shared Folder with chmod SetGID | Tero Karvinen

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Picks

  • Boxing Clock for AndroidOcton8 Diving T-ShirtsShaking Tower Panda Android GameLearn Chinese with Android
  • Student projects