Hardware and software setup

Linux Basics: Introduction to bash. Linux Basics: Introduction to bash Examples using relative paths

All files in Linux have a specific address in the file system, with which we can access them using the file manager or console utilities. It's pretty simple theme, but many beginners have difficulty with this.

In today's short note, we will look at what a Linux file path is, what it can be, how to write it correctly, and much more. If earlier you had difficulties with this, then after reading the article everything will become completely clear.

File paths in Linux

The Linux file system is very different from Windows. We will not consider its structure, this was done earlier. We will focus on working with files.

The most important difference is that the file address does not start from a drive, for example, C:\ or D:\, as it happens in Windows, but from the root, the root system directory to which all others are connected. His address - /. And here it is necessary to tell about addresses. Linux file paths use a forward slash "/" to separate directories in the address, and this is different from what you're used to seeing on Windows - \.

For example, if on Windows the full path to a file on the desktop looked like C:\Users\Sergiy\Desktop\, then the file path on linux would simply be /home/sergiy/desktop/. With this, everything is simple and clear. But problems continue to arise.

In the operating room Linux system There can be several types of file paths. Let's look at what paths are in linux:

  • Full, absolute linux path from filesystem root- you have already seen this path in the example above, it starts from the root "/" and describes the entire path to the file;
  • relative linux path is the path to the file relative to the current folder, such paths are often confusing.
  • Path relative to the current user's home folder.- path in the file system, but not from the root, but from the folder of the current user.

Now let's take a closer look at how these paths look in linux, and also analyze a few examples to make it completely clear. To demonstrate, we will use the ls utility, which is designed to view the contents of directories.

For example, we have a directory like this in our home folder with four files in it:

This is how the full linux path to one of the files will look like:

ls /home/sergiy/tmp/file1

This is already a relative linux path that starts from the home folder, it is denoted by ~/. Note that not ~, but ~/. Then you can already specify subfolders, in our case tmp:

Well, or the path of the file in linux, relative to the current folder:

The first link points to the current folder (.), the second one (..) points to the folder one level up. This opens up even more possibilities for navigating through directories. For example, to refer to a file in the current folder, you can use the construction:

It's useless when viewing the contents of a file. But it is very important when running the program. Since the program will first be searched in the PATH environment, and only then in this folder. And therefore, if you need to run a program that is located in the current folder and it is called exactly the same as the one in the / bin directory, then without an explicit link that the file needs to be searched in the current folder, nothing will work.

Such constructions can be encountered quite often when compiling programs. You can use all these symbols and linux file paths not only in the terminal, but also in any file manager which can be very convenient.

But the Linux terminal provides even more options. You can use simple replacement characters directly in file or directory addresses. For example, you can list all files that start with f:

Or you can even search not only in the tmp folder, but in any subfolder of your home folder:

And all this will work, perhaps it is not always necessary and practical. But in certain situations it can be very helpful. These functions are implemented at the Bash shell level, so you can use them in any command. The shell looks at how many files were found and calls a command for each of them.

conclusions

That's all. Now you know everything you need to not only correctly write the path to a linux file, but also perform more complex actions, such as searching for files or navigating directories using the cd command. If you have any questions, ask in the comments!

Related posts:


After reading this article, you will know what is bash(standard linux shell), learn how to handle standard commands: ls, cp, mv… understand the purpose of inodes, hard and symbolic links and much more.

This tutorial is intended for those who are new to Linux and who want to review or improve their understanding of basic Linux concepts such as copying and moving files, creating links, using standard Linux commands along with redirects and pipes. In this article you will find many examples explaining the material presented. For beginners, most of the information will be new, but for more advanced users, this material can be an excellent tool for summarizing existing knowledge and skills.

Introduction to bash

Shell

If you are using Linux, then you know that after logging in, you will be greeted by a command interpreter prompt. For example this:

\$

If a graphical shell is loaded after login, then to get to the command interpreter you need to launch a terminal emulator (gnome-terminal, xfce4-terminal, konsole, xterm, rxvt ...) or switch to one of the virtual terminals by pressing ctrlaltF1 or ctrlaltF2 etc.

The shell prompt on your computer may differ from what is shown in the example. It may contain the username, computer name, and the name of the current working directory. But despite all these differences, the program that prints this prompt is called " shell” (shell), and most likely your shell is a program called bash.

Are you running bash?

You can check if bash is running with the following command:

\$ echo \$SHELL/bin/bash

If you get an error as a result of executing this command or its output differs from that in the example, then it is possible that bash is not used as a command shell on your system. Despite this, most of the material will be relevant, but we still recommend that you switch to bash. This can be done (if bash is installed on the system) with the command:

\$ bash

What is bash

Bash (acronym for " B ourne- a gain SH ell") is the standard command interpreter on most Linux systems. His responsibilities include processing and executing commands with which the user controls the computer. After you've completed your work, you can end the shell process. After pressing the keys ctrlD, commands exit or logout the shell process will be terminated and the screen will again prompt you for a username and password.

Using "cd"

Let's start using bash to navigate the file system. To get started, type the following command:

$ cd /

With this command, we told bash that we want to move to the root directory - / . All directories in the system are organized in a tree structure and / this is its beginning (or root). Command cd is used to change the current working directory.

Ways

To find out where in the file system this moment you are in (current working directory) type:

\$ pwd /

In the example above / - command argument cd- called way. This is the file system location where we want to move. In this case / - absolute path, which means that the path is relative to the root directory.

Absolute paths

Here are some examples of absolute paths

/dev /usr /usr/bin /usr/local/bin

As you may have noticed, all these paths are united by the fact that they start with / . Specifying the path /usr/local/bin as an argument to the command cd we tell it to go to the root directory / , then to the usr directory, then to local and bin. Absolute paths always start with /

Relative paths

The second kind of paths is called relative. bash, command cd and other commands count these paths relative to the current directory. Relative paths never start with / . For example, if we are in /usr

\$ cd /usr

We can then navigate to /usr/local/bin using a relative path

\$ cd local/bin \$ pwd/usr/local/bin

Usage ".."

Relative paths can contain one or more directories «..» . ".." indicates the parent directory of our working directory. Example:

\$ pwd/usr/local/bin\$ cd.. \$ pwd/usr/local

As you can see the team cd..‘takes us to a higher level’.

Can add .. to relative path. This will move to a directory that is on the same level as the one we are in. Example:

\$ pwd/usr/local\$ cd ../share \$ pwd/usr/share

Examples using relative paths

Relative paths can be quite tricky. Here are some examples. The result of executing the commands is not shown, try to determine in which directory you will find yourself using bash.

\$ cd /bin \$ cd ../usr/share/zoneinfo \$ cd /usr/X11R6/bin \$ cd ../lib/X11 \$ cd /usr/bin \$ cd ../bin/../bin

Working directory "."

Before finishing talking about the team cd, a few more things should be mentioned. First, there is another special directory «.» , which points to the current directory. This directory is used to run executable files located in the current directory.

\$ ./myprog

In the last example myprog is executable file located in the current directory, which will be launched for execution.

cd and user's home directory

To change to your home directory, type

\$ cd

Without an argument, cd will move you to your home directory. For the superuser, the home directory is usually /root, and for ordinary users- /home/username/. But what if we want to specify a specific file located in the home directory. For example, as an argument to the program 'myprog'? You can write:

\$ ./myprog /home/user/myfile.txt

However, using absolute file paths is not always convenient. The same operation can be done with ~ –tildes:

\$ ./myprog ~/myfile.txt

~ is a special name that points to the user's home directory in bash.

Home directories of other users

But what if we need to point to a file in another user's home directory? To do this, after the tilde, you need to specify the name of this user. For example, to point to the file fredsfile.txt located in the user's home directory fred:

\$ ./myprog ~fred/fredsfile.txt

Linux commands

Introduction to ls

You probably already know the team ls, which, called with no arguments, displays a list of files stored in the working directory:

\$ cd /usr \$ ls X11R6 doc i686-pc-linux-gnu lib man sbin ssl bin gentoo-x86 include libexec portage share tmp distfiles i686-linux info local portage.old src

If you specify the option -a, you will be able to see all files, including hidden ones (whose names start with a dot).

\$ ls-a. bin gentoo-x86 include libexec portage share tmp .. distfiles i686-linux info local portage.old src X11R6 doc i686-pc-linux-gnu lib man sbin ssl

Detailed list of directories

After the command ls one or more files or directories can be specified as its argument. If you specify a file name, then the command ls will display information about that file only. And if you specify the name of the directory, ls will show all its contents. Option ‘-l’ commands ls can be very useful if you want to know more than filenames detailed information about them (file permissions, owner name, time last change file and its size).

The following example shows the use of the option ‘-l’ to display information about files stored in the /usr directory

\$ ls -l /usr drwxr-xr-x 7 root root 168 Nov 24 14:02 X11R6 drwxr-xr-x 2 root root 14576 Dec 27 08:56 bin drwxr-xr-x 2 root root 8856 Dec 26 12:47 distfiles lrwxrwxrwx 1 root root 9 Dec 22 20:57 doc -> share/doc drwxr-xr-x 62 root root 1856 Dec 27 15:54 gentoo-x86 drwxr-xr-x 4 root root 152 Dec 12 23:10 i686-linux drwxr-xr-x 4 root root 96 Nov 24 13:17 i686-pc-linux-gnu drwxr-xr-x 54 root root 5992 Dec 24 22:30 include lrwxrwxrwx 1 root root 10 Dec 22 20:57 info -> share/info drwxr-xr -x 28 root root 13552 Dec 26 00:31 lib drwxr-xr-x 3 root root 72 Nov 25 00:34 libexec drwxr-xr-x 8 root root 240 Dec 22 20:57 local lrwxrwxrwx 1 root root 9 Dec 22 20 :57 man -> share/man lrwxrwxrwx 1 root root 11 Dec 8 07:59 portage -> gentoo-x86/ drwxr-xr-x 60 root root 1864 Dec 8 07:55 portage.old drwxr-xr-x 3 root root 3096 Dec 22 20:57 sbin drwxr-xr-x 46 root root 1144 Dec 24 15:32 share drwxr-xr-x 8 root root 25 ssl lrwxrwxrwx 1 root root 10 Dec 22 20:57 tmp -> ../var/tmp

The first column shows information about the permissions for each file in the list. (I'll explain which letter stands for what in a bit.) The next column shows the number of links to each element of the list. The third and fourth columns are the owner and group of the file, respectively. The fifth column is the size. The sixth is the time the file was last modified ('last modified time' or mtime). The last column is the name of the file or directory (If this is a link, then after the ‘ –> ‘ is the name of the object it refers to).

How to view only directories

Sometimes there is a need to view information only about directories, and not about all their contents. The option will help with this task. ‘-d’, which tells the command to display information about directories only. Example:

\$ ls -dl /usr /usr/bin /usr/X11R6/bin ../share drwxr-xr-x 4 root root 96 Dec 18 18:17 ../share drwxr-xr-x 17 root root 576 Dec 24 09:03 /usr /X11R6/bin drwxr-xr-x 2 root root 14576 Dec 27 08:56 /usr/bin

Recursive list and inode information

Option action ‘-R’ opposite action ‘-d’. It allows you to display information about the files in the directory recursively. First, the contents of the top-level directory are shown, then the contents of all subdirectories in turn, and so on. The output of this command can be quite large, so we don't give an example of it, but you can try it yourself by typing command linels -R' or ' ls-RL‘.

And finally the option ‘-i’ used to output the inodes of each filesystem object.

\$ ls -i /usr 1409 314258 X11R6 i686-linux libexec 43090 13394 sbin 1417 1513 bin i686-pc-linux-gnu 5120 local share 13408 8316 distfiles 1517 include 776 man src 23779 43 1386 doc portage info 93892 36737 70744 ssl gentoo-x86 1585 lib 5132 portage.old 784 tmp

What are inodes?

Each object of the file system (file, directory ...) has its own unique number, called inode(inode number). This information may seem insignificant, but understanding the function of inodes will help you understand many file system operations. For example, let's look at «.» And «..» like the links present in each directory. To understand what a directory is «..» , find out the inode of the /use/local directory

\$ ls -id /usr/local 5120 /usr/local

As we can see, the inode of the /usr/local directory is 5120. Now let's see what inode the /usr/local/bin/.. directory has:

\$ ls -id /usr/local/bin/.. 5120 /usr/local/bin/..

It turns out that the inodes of the /usr/local and /usr/local/bin/.. directories are the same! This means that two names refer to inode 5120: /usr/local and /usr/local/bin/.. That is, these are two different names for the same directory. Each inode points to a specific location on the disk.

Each inode can have multiple file system object names associated with it. The number of file 'synonyms' (file system objects referring to one inode) shows the number in the second column of the output of the command ' ls-l‘.

\$ ls -dl /usr/local drwxr-xr-x 8 root root 240 Dec 22 20:57 /usr/local

In this example, you can see (second column) that the /usr/local directory is referenced by 8 different file system objects. Here are their names:

/usr/local /usr/local/. /usr/local/bin/.. /usr/local/games/.. /usr/local/lib/.. /usr/local/sbin/.. /usr/local/share/.. /usr/local/ src/..

mkdir

Let's take a look at the command mkdir. It is used to create new directories. The following example demonstrates the creation of three new directories (tic, tac, toe) in the /tmp directory

\$ cd /tmp $ mkdir tic tac toe

Default command mkdir cannot create a nested directory structure. Therefore, if you need to create several nested directories ( won/der/ful), then you will have to call this command three times in turn:

\$ mkdir won/der/ful mkdir: cannot create directory "won/der/ful": No such file or directory \$ mkdir won \$ mkdir won/der \$ mkdir won/der/ful

You can simplify this operation by adding the option ‘-p’ to the mkdir command. This option allows you to create a nested directory structure:

\$ mkdir -p easy/as/pie

To learn more about the capabilities of this utility, read the help that is called by the command man mkdir. Help is available for almost all commands in this manual (for example, man ls), except cd, because it is built into bash (for such commands, help is called like this: help cd)

touch

Let's move on to learning commands cp And mv, used to copy, rename and move files and directories. But before that, let's create an empty file in the /tmp directory using the command touch:

\$ cd /tmp \$ touch copyme

Command touch updates the time of the last access to the file (sixth column of the output of the command ls-l) if it already exists, or creates a new empty file if it doesn't already exist. After this operation, we should have an empty file /tmp/copyme.

echo

Now that we have an empty file, let's write a text string to it using the command echo, which prints the argument passed to it to standard output (a text terminal in our case).

\$ echo "firstfile" firstfile

To write a string to our file, we redirect the output of the command to it echo:

\$ echo "firstfile" > copyme

Sign > (greater) tells the shell to redirect the output of the command on the left to the file name on the right. If a file with the same name does not exist, it will be created automatically. And if such a file already exists, then it will be overwritten (all its contents will be erased before writing our line). Command 'ls -l' will show that the size of our file is now 10 bytes - nine bytes are occupied by the word 'firstfile' and one byte is the line feed character.

\$ ls -l copyme-rw-r--r-- 1 root root 10 Dec 28 14:13 copyme

cat and cp

To display the contents of a file on the terminal, use the command cat:

\$ cat copyme firstfile

Now we can start parsing the basic functionality of the command cp. This command takes two arguments. The first is the name of an already existing file ('copyme'), the second is the name of the new copy we want to make ('copyme').

\$ cp copyme copiedme

We can make sure that new copy file has a different inode number (this means that we got a really new separate file, not just a link to the old one)

\$ ls -i copyme copiedme 648284 copyme

mv

Now let's apply the command mv to rename the file ("copiedme" -> "movedme"). The inode number after this operation does not change, but only the file name changes.

\$ mv copied-me-moved-me \$ ls -i moved me 648284 movedme

The inode number does not change unless the renamed file remains within the same file system as the original file. We'll take a closer look at how filesystems are arranged in a later part of this tutorial.

Command mv allows you to not only rename files, but also move them. For example, to move a file /var/tmp/myfile.txt to the directory /home/user you need to give the command:

\$ mv /var/tmp/myfile.txt /home/user

The file will be moved to the user's home directory user even if it is on a different file system (in this case, the file will be copied to a new location, after which the original will be deleted). As you might have guessed by now, moving a file to a different file system changes its inode. This happens because each file system has its own set of inodes.

It should be noted that there is a possibility that the new assigned inode number may be the same as the old one, but it is extremely small.

To move several files at the same time into one directory, you need to write:

\$ mv /var/tmp/myfile1.txt /var/tmp/myfile2.txt /home/user \$ mv -t /home/user /var/tmp/myfile1.txt /var/tmp/myfile2.txt

If you add the option ‘-v’, a report on the operation performed will be displayed on the screen:

\$ mv -vt /home/user /var/tmp/myfile1.txt /var/tmp/myfile2.txt"/var/tmp/myfile1.txt" -> "/home/user/myfile1.txt" "/var/tmp/myfile2.txt" -> "/home/user/myfile2.txt"

Recently, I described commands for moving through directories in the terminal Linux. So I decided to continue the story about the commands in the console. So the movements seem to be sorted out. Now I want to consider in a little more detail what paths to directories are in the console.

Let's first look at the most well-known for everyone, the paths to our files and directories, namely the absolute paths. Open a terminal and issue a command that prints the full path to your home directory. I talked about this team recently, here. Enter pwd:


:~$ pwd
/home/dante


As you can see this console command displayed the full path starting from the root directory. I just described such movement through directories recently, this movement using an absolute path. But these are not all possibilities to move through the categories of our operating system. IN operating system Linux, there is still the possibility of using relative paths.

Relative paths in the console count categories relative to the category you are in. They can be used with ".." two dots before the folder we want to go to. Let's go somewhere in our operating system. For example in a folder usr and inspect its contents with the command ls.

[email protected]:~$ cd /usr
[email protected]:/usr$ls

[email protected]:/usr$

Here we see a list of available directories. Let's now go to the directory " share».

[email protected]:~$ cd /usr/share


So, now we are in the category /usr/share as we see. Let's move now to a category that's on the same level as the category we're in. For example, in the category local". This can be done with an absolute path like this.

[email protected]:~$ cd /usr/local

[email protected]:/usr/share$pwd
/usr/share
[email protected]:/usr/share$


As you can see, I have displayed for you in the console, the full path to the place where you are. We know that the directory " local' is on the same level as the directory ' share” and we can use a command like this.

[email protected]:/usr/share$ cd ../local
[email protected]:/usr/local$


As you can see, the query command has become much simpler. But this is only if you remember directories that are on the same level as your working directory, and I don’t always remember other directories in the console.

In order to go to the next directory, it is enough to enter a command in the console relative to your working directory. For this purpose, use instead ".." two points "." one point and you will move relative to the directory in the console where you are at the moment. Let's go back to the directory /usr and take another look at what's in there.

[email protected]:/usr/local$ cd /usr
[email protected]:/usr$ls
bin games include lib local sbin share src
[email protected]:/usr$


Let's now go to the directory " share» relative to the directory in which we are now. To do this, enter in the console

[email protected]:/usr$ cd ./local
[email protected]:/usr/local$


As you can see, using relative paths can greatly reduce the commands you enter in the console. Linux.

Liked the article? Share with friends!
Was this article helpful?
Yes
Not
Thanks for your feedback!
Something went wrong and your vote was not counted.
Thanks. Your message has been sent
Did you find an error in the text?
Select it, click Ctrl+Enter and we'll fix it!