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)
Print working directory (this is where you are)
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
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 email@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’
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 firstname.lastname@example.org: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 “:”.
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
This history can be searched with ctrl-R (^R).
$ wget # -r -np --convert-links $ links # or one of lynx w3m glinks elinks w3 $ w $ finger $ ping # ctrl-C
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
After you have learned these important commands by heart, have a look at commands for admin.
Edit: Fixed link.