More about Linux Terminal
Authors: Lauri Sormunen, Timo Hämäläinen and Mikhail Zolotukhin
1. Introduction
In this page we cover the basics of a POSIX-like command line interface. This is used in Linux-based operating system distributions and FreeBSD, which are used in this course. To be precise, with "Linux CLI" we actually refer to a (most likely) GNU implementation of a POSIX-like interface, which runs on a Linux kernel. A similiar interface can be found on FreeBSD and many other systems with slight differences in the basics.
Linux-based operating systems are most commonly used in server and mobile while seeing less usage in Desktop computers. Linux operating systems commonly have a useful command line interface (CLI) called Terminal. While usage of CLIs may seem out-of-date, the CLIs can be used to effectively maintain heavy-duty computers without running resource-demanding graphical user interfaces (GUI) and to automate repetitive tasks by scripting. In this brief tutorial, we will familiarize ourselves with most common Linux CLI, i.e. terminal, commands and usage of file system.
File system and a couple of methods for learning basics of Linux are introduced in section 2. Section 3 lists a few useful commands that every user should know. Section 4 focuses on usage of SSH. Section 5 concludes this tutorial.
2. File system and useful commands
Entire names of files and directories in Linux file system look like this:
/path/to/your-file-or-dir
You may refer to files using either relative paths or absolute paths. File system hierarchy starts from the root directory (/). The difference between absolute and relative paths is that absolute paths start with a slash. For example, let us consider the following directory structure:
/test-dir
├── my-dir-1
│ ├── first-file.txt
│ └── second-file.txt
├── my-dir-2
│ └── third-file.txt
└── other-dir-1
└── fourth-file.log
In the commands below, symbol "$" is not part of the commands. The dollar sign in the beginning of a line is a (shortened) command prompt of the shell. Usually you see something like in a real system:
[user@host:~]$
Superuser prompt uses "#" symbol instead by convention.
We may print the content of "first-file.txt" by (at least) two ways. The first option is to use absolute path:
$ cat /test-dir/my-dir-1/first-file.txt
The second one is to use relative path from a certain working directory. First, move to the directory:
$ cd /test-dir/my-dir-1
and then use the relative path:
$ cat first-file.txt
If we are in directory "other-dir-1"
$ cd /test-dir/other-dir-1
and our file "first-file.txt" is in another directory "my-dir-1", we can access it as follows:
$ cat ../my-dir-1/first-file.txt
where ".." refers to directory one level up.
In Linux, "." refers to current directory, while ".." means the upper directory, and "~" refers to home directory of current user, usually placed in /home/my-username/. Generally, all files are kept in home directory, files and directories under root directory ("/") are for system-wide files, such as programs installed for all users.
To run programs and scripts, one must (generally) refer to program files using entire paths:
$ /path/to/my-program
To avoid typing entire path, when the program is in current working directory, you may utilize ".":
$ cd /path/to/
$ ./my-program
How do all the Linux commands work then, by just typing "ls" while no "ls" exists in current working directory? Environment variables are required for this purpose, particularly the variable "PATH" in our case.
Try the following command:
$ echo $PATH
and you will see several directories separated by colons. These directories are always checked for the program user requests to run, unless available in the current directory or an absolute path was specified.
Sometimes you do not have permissions to run a command, e.g. when doing system-wide installations or administrator tasks. Then you have to run a command with root privileges, i.e. adding "sudo" in front of command and typing password if requested. If you are an average user on a server, it is likely you do not have sudo-access at all and are limited to viewing only your home directory.
Avoid running sudo unless absolutely required! It is always preferred to run programs locally, in home directory, to avoid system-wide malfunctions.
2.1 Codecademy interactive tutorial
Codecademy.com hosts an interactive Linux terminal tutorial. If you have never used Linux and are unfamiliar with basics of command line interface, it is recommended to check it out first.
3. Useful commands
The following table is a list of some of the most common commands.
Command | Examples | Info |
---|---|---|
echo | echo Hello hello hello echo $PWD |
Print all arguments. |
ls | ls ls /some/path/to/directory/ ls -la |
List content of a directory. |
cd | cd another-directory | Change current working directory. |
pwd | pwd | Print current working directory. |
mkdir | mkdir New-directory | Create a new directory. |
cp | cp file.txt another-file.txt cp file.txt another-dir/ cp -r directory/ another-directory/ |
Copy content of a file to another file. |
rm | rm some-file.txt rm -r some-directory/ |
Remove a file permanently. |
mv | mv some-file.txt another-file.txt/ mv some-file.txt another-dir/ mv dir/ another-dir/ |
Move a file or a directory. |
grep | grep "word" file.txt | Print all lines containing a word or a pattern of a file. |
ifconfig | ifconfig ifconfig newinterfacename new-ip netmask newmask |
Check or set network interface properties. In some distributions, you have to use 'ip' instead. |
nano | nano nano new-text-file.txt nano existingscript.py |
Open a (somewhat) user-friendly text editor. |
chmod | chmod +x chmod +rw chmod -w |
Add or remove access rights to a file (+=add, -=remove, x=execute, r=read, w=write). |
cat | cat some-file.txt | Print content of a file. |
less | less some-file.txt | View content of a file in parts. |
wget | wget http://anydomainname.com/some-directory/file.zip | Download a page or a file from the Internet. |
| | ps aux | grep "ssh" | Pipeline output of a command as an input to another. |
> | echo "Hello!" > hello.txt | Redirect the output of a command to a file. Old data of the file will be gone! |
>> | echo "Hello!" >> hello.txt | Redirect the output of a command to the end of a file. |
apt | sudo apt install newprogram sudo apt update sudo apt upgrade apt list texlive* |
Install and manage packages. Apt is used in Debian and its derivatives. Plenty of others exist. (e.g. pkg in FreeBSD, yum in CentOS) |
4. SSH
Secure Shell (SSH) is a secure protocol most commonly used for accessing other computers remotely. You have probably used SSH to access halava.cc.jyu.fi or jalava.cc.jyu.fi remotely with Korppi/JYU credentials using Putty.
Old Windows operating systems may not include native SSH client applications, but using applications like Putty enable SSH connections for Windows, too. Putty does not include options for transferring files between the local Windows host and remote Linux host, but it is possible by using pscp alongside with Putty. WinSCP is a similar file transfer program with a GUI.
In Windows 10, there should be an SSH client. It is also possible to instal Windows Subsystem for Linux which includes a Bash shell for Windows. This can be enabled by first enabling Developer mode in "Settings -> For developers". After the developer package has been installed, search "Turn Windows features on or off" and tick the checkbox next to "Windows Subsystem for Linux (beta)". After reboot, just run (Win+R or in Cmd) "bash" to start running Linux terminal commands in Windows. The subsystem shows Windows files under /mnt/
as mounted drives when using bash. The Linux file system is accessible in Windows by navigating to C:\Users\%USERNAME%\AppData\Local\lxss\
.
Read this SSH tutorial to learn about features of SSH.
5. Conclusion
In this tutorial, we have got familiar with basics of a Linux CLI and learned some of the common commands required for the course's assignments. When in trouble, consult your preferred search engine, and the man pages of the system you are running.
These are the current permissions for this document; please modify if needed. You can always modify these permissions from the manage page.