21. February 2024 By Kenneth May
Linux for developers: Navigation in the command line
Linux is an indispensable tool in the world of software development. Linux offers flexibility, performance and the freedom to customise the system according to your ideas - key skills for every developer.
Linux is an indispensable tool in the world of software development. Linux offers flexibility, performance and the freedom to customise the system according to your ideas - key skills for every developer. In each of my projects so far, it has been necessary to configure a system. An article by Roland Majchszak "The Cloud changed our software architectures" describes the differences between on-premise and cloud.
However, new services often lack the necessary expertise.
Roland Majchszak
He's right and that's why this article is aimed at anyone interested in familiarising themselves with the basics of Linux. Be it to set up your development system or to achieve a broader distribution of knowledge in your project.
The Linux file system
Basic understanding and differences to Windows
The Linux file system is fundamentally different from that of Windows.
Windows usually organises physical hard drives into drives such as C:, D:
and so on.
Linux, on the other hand, organises everything in a directory hierarchy that begins with a single root directory, the so-called root directory (/)
.
ll other drives and partitions are mounted in this directory tree. Linux even goes one step further: even the hardware and kernel configuration is available as a file.
In addition to root, there is also the home directory. This is usually located under /home/<Benutzername>
. The user-specific files and configurations are saved there.
This principle should be familiar to Windows, as it has a similar structure. And as with Windows, the authorisations are restricted. However, there is one small exception here that I would like to mention. The root account also has a home directory. To emphasise the special status of root, this is not under /home/<Benutzername>
, but directly under /root
.
In addition to Root and Home, there are also other special directories. To keep the introduction clear for you, these will be described in a later article.
Important for further content
All paths are relative to the current path unless the reference begins with a / for the root directory, in which case the path is absolute. However, if you have already worked with Powershell or the command prompt, then you already know this.
Hands-On
Especially at the beginning, it is easier if you can try out the commands yourself. That's where Docker comes in handy. If you already have a running Docker instance, for example via DockerDesktop, you can simply start a new container and select ubuntu ubuntu:latest
as the image. As soon as you connect to the container, you can experience the commands live.
For the Windows users among you - Windows already offers a shell (Bash) directly under Windows with WSL (Windows Subsystem for Linux). For Apple users, it's even easier: OS X itself uses zsh as a shell.
Here comes a new term: Shell. A shell is the command prompt under Linux. There are different shells, the most common being sh, closely followed by bash (Bourne Again Shell). The differences between the individual variants are negligible, especially at the beginning.
Important commands for navigation and file management
1. cd (Change Directory)
The cd
command is used to switch between directories.
- Example:
cd /home/benutzername
changes to the user directory.
Then there are the special folder names .
und ...
. .
for the current directory, while ..
das übergeordnete Verzeichnis meint.Dadurch ergeben sich besondere Varianten von cd. cd .
means the parent directory. This results in special variants of cd.
.
tells the system to change to the current directory. I'll leave it up to you to find the meaning behind it. This is mainly used when programmes require a path, but we are already there. An example of this is docker build .
, as the build process expects a context directory.
cd ..
the system to change to the parent directory.
If we execute this in our home directory (/home/benutzername/
) /), as in the example above, we end up in /home
.
cd (note the lack of a destination) is a short form for changing to your home directory (/home/<username>).
2. mkdir (Make Directory)
Use mkdir
to create a new directory. During generation, directories belong to the current user account and its main group. If the name begins with a .
, then it is a hidden directory.
- Example:
mkdir newDirectory
creates a directory named newDirectory in the current directory. - Example:
mkdir /.neuesVersteck
creates a hidden directory with the name.neuesVersteck
in the root directory.
Depending on how you are trying out the commands, you will receive an error message here.
The root directory belongs to the user root and his group root (creative, isn't it?) and you may not be root at the moment. Linux will kindly inform you that you do not have the authorisation.
3. rmdir (Remove Directory)
This command removes an empty directory. Empty actually means empty because hidden files or subfolders also prevent deletion.
- Example:
rmdir altesVerzeichnis
deletesaltesVerzeichnis
, provided it is empty.
4. touch
touch
is often used to quickly create empty files or to update the last modification date. It is important to note that file extensions only have a limited purpose in Linux. If you open a file via a file manager, the extension may be used, but if it is different, this is often of secondary importance. Touch is a good example here: if an empty file is created, it is binary empty. If you create a file.pdf,
you do not have a PDF with a blank page.
Digression
We can see the difference under Windows if we create an empty TXT file (for example a file.txt) and then rename it to file.nfo.
If you then open the file, Windows will complain that the file is faulty and display a very detailed hardware and software list.
- Example:
touch datei.txt
creates an empty file calleddatei.txt
. - Example:
touch .versteckt.txt
creates a hidden, empty file called.versteckt.txt
.
5. rm (remove)
Files are irretrievably deleted with rm.
Due to the nature of irretrievable deletion, Linux warns and questions whether we really want to delete files. Unless you are root. Without the explicit hint, however, rm cannot delete directories: rm: test/: is a directory.
- Example:
rm datei.txt
deletes the file created in the previous step. - Example:
rm .versteckt.txt
deletes the hidden file created in the previous step.
6. ls (List)
ls
lists the contents of a directory. ls without arguments does not list hidden directories. The -a
or -A
option is used for this. -a
lists all files and directories in the current directory, hidden or not. -A
does almost the same as -a,
but ignores the two special directories .
and ...
Another interesting parameter is -l,
which also lists metadata for the files.
- Example:
ls /home
shows the contents of the home directory.
$~> ls -lA
total 28
-rw------- 1 may admin 31 Nov 29 12:20 .bash_aliases
-rw------- 1 may admin 1872 Dec 20 17:58 .bash_history
-rw------- 1 may admin 220 Apr 23 2023 .bash_logout
-rw------- 1 may admin 3526 Apr 23 2023 .bashrc
-rw------- 1 may admin 0 Nov 26 05:21 .cloud-locale-test.skip
-rw------- 1 may admin 20 Dec 20 14:58 .lesshst
-rw------- 1 may admin 807 Apr 23 2023 .profile
drwx------ 2 may admin 4096 Nov 29 12:20 .ssh
-rw------- 1 may admin 0 Nov 29 12:28 .sudo_as_admin_successful
The command lists the current directory and also shows hidden files but ignores .
and ...
The following meta information on the files is also displayed:
- 1. type, authorisations and sticky bits. An example of a "type" is the
d
in.ssh,
which marks the entry as a directory. For the time being, only the authorisations are of interest. - 2. hard links to the file.
- 3. the uid (user-id) of the owner. UserIDs are numbers, in most distributions from 1000, which identify a user account. This is difficult to read, so
ls
supports us here by translating the user IDs into the readable name. - 4. the group. The translation of the gid (group id) here is analogous to the user id
- 5. the file size in bytes
- 6. the date of the last change (we remember touch)
- 7. the file name
As we can see in the example, I was even forced to use -a
or -A.
My home directory only has hidden files and is otherwise empty, rather bad for demonstrating ls.
7. man (Manual)
This is a very useful command to get help with other commands. As long as you use general commands, you can simply look at the "manual page" and find information about the command. This is often used when you want to search through the arguments for a command. In the help pages, there are often references to other pages with further information at the end.
- Example: man ls shows the manual for the
ls
command.
Special features of the Linux file system: Hidden files and authorisations
Authorisations
The authorisation system in Linux is an essential feature for security.
Every file and every directory has authorisations that determine who is allowed to read, write or execute the file.
These authorisations are divided into three categories:
Owner
(for a newly created file/folder, the creator)Group
(all user accounts that belong to the same group)Other
(all other users)
The authorisations are often displayed in the form rwx,
where r
stands for read, w for write and x
for execute.
You can use the ls -l
command to display these authorisations (see above).
Extended explanation of chmod: Octal numbers and symbolic notation
Octal numbers in chmod
When using chmod,
authorisations can also be defined with octal numbers (base 8). Each digit represents a set of authorisations: Read (4, the third bit), Write (2, the second bit) and Execute (1, the first bit). The sum of these values defines the authorisations for a category (owner, group, other).
- Example:
chmod 754 file.txt
sets the permissions so that the owner has all permissions (7), the group can read and execute (5) and other users can only read (4).
Symbolic notation in chmod
As an alternative to octal notation, you can also use symbolic notation, which should be more intuitive, especially for newcomers:
u
stands for owner (user),g
for group,o
for others,a
for all (all).
We can add (+),
remove (-)
or precisely define (=)
authorisations.
This includes the following authorisations:
r
stands for readw
stands for writex
stands for executableX
keeps the execute permissions for files and, what it is particularly useful for, sets them for directories only!
- Beispiele:
chmod u+w file.txt
adds write authorisation to the owner.chmod g=rX file.txt
sets the group permissions to Read and, if it is a directory or the Execute permission is already set, also to Execute.chmod g=rX subdirectory/
sets the group authorisation for the subdirectory to "read" and gives the group authorisation to enter the directory.chmod ug+rX,o= -R subdirectory/
adds read authorisation to groups and users and, if it is a directory, this may also be entered.
The authorisations are removed for all others, octal 0. And finally, with -R we recursively execute the changes for all subfiles and directories.
id
The id command displays the user ID and the group ID of the current user. This is helpful to understand which user and group authorisations you are working under. In addition, you can see what the main group (gid) is.
- Example:
id
shows the user ID, group ID and membership of other groups of the current user.
chown
The command chown
(change owner) changes the owner and/or the group of a file or directory.
This is useful for managing access rights.
- Example:
chown user file.txt
changes the owner of file.txt to user and leaves the group untouched.chown user: file.txt
changes the owner of file.txt to user and the group to the user's main group.chown user:group file.txt
changes both the owner and the group.
Summary
So far, we have learnt about the most important commands for file management. We can use cd,
mkdir
and rmdir
to change, create or delete directories. With touch, rm and ls we can open and delete files and see what the directories contain. man
helps us to find further information on commands. Finally, we have learnt about the chmod
and chown
commands and can use id
to view information about our user. Knowledge and application of these commands and concepts enable finely tuned control over file and directory authorisations in Linux. This is particularly important in development environments, where access to certain files and directories must be carefully managed. With practice, this complexity becomes manageable and a powerful tool in the hands of an experienced developer.
If you have any questions or suggestions, you can reach me at kenneth.may@adesso.de.
Outlook
Navigating through the file system via the command line is just the beginning. Further topics include the installation of new programmes and user administration.
You can find more exciting topics from the world of adesso in our previous blog posts.