Command Line Basics Revisited
The command line used in Linux and BSD has survived the test of time. It existed before Google, Web, Linux, Windows and even before the Internet. It's also convenient, fast, expressive and easy to automate. I use it every day.
$ pwd /home/tero
Previous versions of this article have been popular for more than 10 years.
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
We're always in a directory. Current directory is the working directory.
'pwd' prints the working directory. Notice how I don't need to type the prompt '$'.
List files in working directory. These are the files you can directly manipulate. If shell says "No such file or directory", it's a good idea to 'ls' to see what files there 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. In 'less', 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
Easiest text editors are pico and nano.
$ nano FOO.TXT
Use CTRL-X y
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 an empty directory
$ rmdir EMPTYDIR
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. I use tab to complete almost all commands I type.
$ 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).
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, /var/log/auth.log and /var/log/apache2/error.log|
Minimum privilege principle states that we do operations with the lowest level of privileges possible. Only operations requiring high privileges are run with 'sudo'. These commands gain unlimited privileges.
Commands affecting the whole system require higher privileges. Such operations include installing and removing software, creating users and managing privileges.
Package Management – Installing Software
Package mangement is a safe and convenient way of installing software.
Update list of available packages. Run apt-get update before giving other apt commands.
$ sudo apt-get update
Search for software with keywords, no sudo needed.
$ apt-cache search dungeon adventure
Package name is the part before dash ‘-’. It is needed when installing.
nethack-console - Text-based overhead view D&D-style adventure game
$ sudo apt-get -y install nethack-console
Many programs are shown in the applications menu. For most command line applications, you have to find out the right command.
$ dpkg --listfiles nethack-console
Usually, the command is the file in one of the bin/ or game/ directories
So we run it with
To avoid addiction, only try that nethack runs. Don’t start playing the game.
In fact is best to remove the game. We'll use purge, so that also it's system wide settings are destroyed.
$ sudo apt-get purge nethack
Notes on Nethack
Maybe you shouldn’t start playing Nethack. But if you insist, game is easier to play with numpad enabled. echo "OPTIONS=number_pad:1">$HOME/.nethackrc
Same apt-get command can install a lot of software.
$ sudo apt-get -y install krita blender vlc tree httpie curl tmux python3-py
Now, go practice!