This guide covers the basics of handling the Linux command line in a terminal.
After logging in to your user account the command prompt in a terminal will look like this: [username@HOSTNAME ~]$
username is your user name.@ at (what computer/hostname)HOSTNAME the computer you are logged in to.~ the current path, ~ is your home directory.$ your rank, normal user.Notice that terminal input is CasE SensItiVE.
To scroll up and down in the terminal window, use the key bindings shift-PgUp and shift-PgDn
To repeat previous terminal input, use the up and down arrow keys to choose from the latest commands.
Execute commands with the enter key.
When needed clean up the terminal window with the command $ clear
Use $ su (and root password when asked) to become the super user (root). The command prompt prefix will change from $ to # and indicates your new rank.
Only work as root when required. Being logged in as root always possess a security risk. You can make mistakes with administrator permissions, or someone can capture the root account.
# su username to swap user to any other username on the system. root can become any user without having to type in the user password.
Another approach to administration is to execute only the specific command with root permissions. This is made possible with sudo (Sper User DO).
Prefix any command like this: $ sudo command and enter a password when asked. After typing your password once, you can use sudo for some time before having to retype it.
$ cd /media will change path from your home directory to /media. Without any path specified $ cd will take you back to your home dir.
To specify a directory name containing a blank space, like "Broken arrow", use a back slash after the first word like this: Broken\ arrow
To navigate to a path higher up in the directory hierarchy use cd ..
To move up two directories at once use cd ../.. and so on.
$ ls will list the content of the current directory.
Specify a path to list this specific directory, for example ls / to list the content of the topmost directory in the file system.
Use ls -a to list all content of a dir, including hidden files (dot-files).
Commands often use the same basic syntax. First command, then the path.
The path can be relative: ./ will target the current directory, ./Music will target the dir Music inside the current dir and so on.
Or it can be absolute like: /home/user
cd or ls commands can never target files, but only directories.
Most commands and apps that target directories will use the current dir if no other is specified. For example the file manager, XFE, will start in your home dir if you start it with $ xfe . Use $ xfe path to start in any other directory.
There are also commands to target files. For example the text editor nano.
$ nano without specifying a file will start nano with a new blank text file. (use key binding C-x to exit nano). If you specify a path like $ nano ~/mytextfile it will open "mytextfile" in your home directory.
All installed applications have a terminal command to launch them, normally the application name. $ opera will launch Opera web browser for example.
Starting an app (even GUI apps) from a terminal have the advantage that the terminal will print messages from this app. Good when investigating why something is not working as expected.
To start a GUI app from the terminal without locking up the terminal with messages from this app, add & as a command suffix.
$ firefox & to launch firefox while still being able to work in the terminal. Firefox is still tied to the terminal though. If you close it firefox will close as well.
To avoid this, detach the last started app completely from the terminal with $ disown . Or $ firefox & disown to start firefox directly without any connection to the terminal.
It is possible to input several commands on the same line as long as you separate them with && . For example $ cd / && ls will first navigate to the topmost directory in the file system and then list this directory.
nano and vi are two CLI text editors. They are installed on most Linux systems and since they might be all you got to fix a system when the GUI refuse to start, it is a good idea to have some basic skills with one or both of them.
Start editing a file with $ nano filename
Edit text and move the marker using the keyboard and arrow keys.
Below the text, at the bottom of the window, the nano key bindings are listed. "^" means Ctrl key. To save (write-out) use C-o and press enter if you are satisfied with the file name. To close the file use C-x
Start editing a file with $ vi filename
Some basic vi-commands are:
i to enter insert mode. This is where you edit text in the file.
esc to exit insert mode. Now you can navigate in the text with h,j,k,l instead of using arrow keys in case the do not work as expected.
When not in edit mode you can also give commands like:
Del -key will erase the marked character.dd will cut the entire text line:help for manual:w write or save:w filename save as filename:q quit:q! quit without saving:set mouse=a enable mouse input if posible.So far the following has been explained; How to navigate in the file system, list the content of directories, launch apps and edit files in nano or VI editors. Next follows a command reference list, with useful commands for Linux.
The following aliases will be used in the syntax of this command reference:
path is any path, e.g. /usr/share/iconscommand is any commandfilename is any filename$ help List allot of helpful things. Exit with q as in quit.
$ command --help Print out basic usage help for this command.
$ man command Show the manual for the command. Exit with q
$ help command Might also help out if there is a dedicated help topic for this command.
$ info Fire up a help library. Exit with q
$ info command Search the help library for sections related to the command.
$ mkdir path Create a directory.
$ touch path Create an empty file or blank the existing one.
$ less path or $ more path View specified text file in terminal. It is a contradict but Less has a richer number of functions compared to More.
$ file path Show description of specified file.
$ path/file Execute a file, if it is executable.
$ chmod Change permissions of files and directories. Read more about chmod or consult its manual for further documentation. Only use chmod after gaining understanding about permissions.
$ cp path path Copy the first specified to the latter path.
$ mv path path Move or rename the first specified to the latter path. This is also the standard way of renaming a file from the terminal.
$ rm path Delete the specified path.
$ rm path -r Delete the specified path and its content in rage mode, without any questions. This command can delete directories and all their content. Use with care!
$ mc Midnight Commander is a powerful two panel file manager that can run in the terminal. It is not installed by default, so install it prior to use. Read more here about Midnight Commander.
$ whoami Display your user name.
$ w Show who is logged on and what they are doing.
$ uptime Print out system uptime.
$ free -m Display memory usage in megabytes.
$ top Complete system monitor to view CPU load, RAM and SWAP info as well as most resource consuming running processes in realtime, q to exit.
$ top -u username to run top showing only the processes of a specified user.
$ htop Complete ncurses interface system monitor application. $ htop -u username to run htop showing only processes of a specified user.
It is not installed by default, but the package htop is in the Arch repos.
$ pstree Show processes and parent-child relations.
$ df -h -T Show file systems free-used space in (-h) human readable format and (-T) list type of file system.
$ less /proc/cpuinfo List info about CPU(s).
$ cat /proc/version Print out the running kernel version.
$ cat /proc/modules Print out all loaded modules.
$ /sbin/runlevel Show the current system runlevel.
$ glxinfo Show OpenGL functions provided by the GPU.
$ glxinfo | grep direct will filter out if direct 3D rendering is enabled.
$ uname -r Show your current kernel version.
$ uname -m Show your hardware architecture.
$ uname -p Show the processor type.
$ uname -i Show the hardware platform.
$ uname -a Show all information: kernel name, node name, kernel release, kernel version, machine hardware name, processor type, hardware platform, operating system.
# shutdown -r now or # reboot Securely reboot computer.
# shutdown -h now or # halt Securely shutdown computer.
$ logout End the current user session from the (real) terminal.
$ kill -9 -1 End the current user session from a virtual terminal.
$ exit End the current session at a virtual terminal. This will only close the virtual terminal, but not perform a logout.
$ killall xxx Kill specified running process (xxx).
$ alsamixer A sound volume mixer that can run in the terminal. It is part of alsa (Linux Advanced Sound Architecture).
# alsactl store Store current alsamixer settings.
$ alsactl restore will reload the stored alsamixer settings.
$ echo "any text" Print out "any text" in the terminal. Very useful when using a script to output some information.
$ sort filename Sort lines of input.
$ grep -option string filename Print lines matching a pattern in the string.
Very powerful command to filter information.
$ xterm -hold -e "command" Launch a new instance of xterm. The terminal will execute the command and hold (not close terminal when command has finished).
$ find path -iname "string" Search for files at the specified path and filter out files containing the string. For example: $ find ~/Music/Music_library -iname "*.mp3" to filter out any files of type .mp3 in dir Music_library.
-iname Make the search non case sensitive, compared to case sensitive -name
* A wild card character that will match any pattern.
The standard output of a command can be fed to a text file instead of being displayed at the command line. This is done with the > character. Two >> characters will append the output to a new line in an existing text file rather than creating a new file.
$ whoami > username.txt Output your username to the text file username.txt .
$ echo is_my_username >> username.txt Add a new line with the text is_my_username to username.txt .
In the same way you can use the < character to feed the content of a text file to the terminal. Many commands accept this way of providing Standard Input, abbreviated as stin. If you created the text file as described above, now try $ sort < username.txt and the content of this file will be fed to the sort command.
You can also use stout and stin together. $ sort < username.txt > username_sorted.txt Output the sorted list to a new text file.
With pipes, the standard output of one command is fed into the standard input of another command. It is done with the | character. Try $ ls | grep s to filter out all names with s in a directory. This command will use ls to list the directory content and pipe the result to grep s In the same way try $ glxinfo | grep direct to see if you have direct 3D rendering on your computer. Very powerful.
Home | Contact | Debian OS | OpenStudio arkitekter