We use absolute and relative paths in the console. Linux Basics: An Introduction to Bash Recursive List and Inode Information

I recently 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 start by looking at the most well-known paths to our files and directories, namely 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. Introduce pwd:


: ~ $ pwd
/ home / dante


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

Relative paths in the console, counting categories relative to the category you are in. They can be used with ".." two points indicated in front of the folder we want to go to. Let's go somewhere in our operating system. For example in the folder usr and examine 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 can see. Let's now move into a category that is on a par with the category we are 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 with 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 easier. But this is only if you remember the directories that are on the same level with your working directory, and I do not always remember other directories in the console.

In order to go to the next directory, just enter the command in the console relative to your working directory. For these purposes, use instead ".." two points, "." one point and you will move relative to the directory in the console in which you are at the moment. Let's go back to the directory / usr and look again what is 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, the use of relative paths can greatly shorten the commands that you enter in the console. Linux.

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. This is a fairly simple topic, but many newbies have difficulty with it.

In today's short note, we will look at what the path to a Linux file 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, it was done earlier. We will focus on working with files.

The most important difference is that the file address begins not from the 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 say about the addresses. Linux file paths use a forward slash "/" to separate directories in an address, and this is different from what you are used to seeing on Windows - \.

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

The Linux operating system can have several types of file paths. Let's look at what paths are in linux:

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

Now let's take a closer look at how these paths look on linux, and also analyze a few examples to make it completely clear. For demonstration, 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 ~ /. Notice, not ~, but ~ /. Then you can already specify subfolders, in our case tmp:

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

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

It is useless when viewing the contents of a file. But it is very important when executing the program. Since the program will be searched first in the PATH environment, and only then in this folder. Therefore, if you need to run a program that is in the current folder and it is named exactly the same as the one in the / bin directory, then nothing will work without an explicit reference that the file needs to be searched for in the current folder.

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

But the Linux terminal is even more powerful. You can use simple substitution characters directly in file or directory addresses. For example, you can list all files starting 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 level of the Bash shell, so you can use them in any command. The shell looks at how many files were found and invokes a command for each one.

conclusions

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

Related entries:


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

This tutorial is intended for those new to Linux and for those who want to repeat or improve their understanding of basic Linux principles 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 to clarify the material presented. For beginners, most of the information will be new, and for more advanced users, this material can be an excellent guide to summarize existing knowledge and skills.

Introduction to bash

Shell

If you are using Linux, then you know that after login you are greeted by a shell prompt. For example this:

\$

If the graphical shell is loaded after login, then to get to the command interpreter, you need to start the terminal emulator (gnome-terminal, xfce4-terminal, konsole, xterm, rxvt ...) or switch to one of the virtual terminals by clicking CtrlAltF1 or CtrlAltF2 etc.

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

Are you running bash?

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

\$ echo \ $ SHELL/ bin / bash

If as a result of executing this command you receive an error or its output differs from that in the example, then it is possible that your system does not use bash as a command shell. 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. Once you have completed your work, you can end the shell process. After pressing the keys CtrlD, teams exit or logout the shell process ends and the screen again prompts you for your username and password.

Using "cd"

Let's start using bash to navigate the filesystem. First, 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 serves to change the current working directory.

Paths

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

\$ pwd /

In the above example / - command argument cd- called way... This is where we want to move to in the filesystem. In this case / - an absolute path, which means that the path is specified 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 begin with / ... By specifying the path / usr / local / bin as an argument to the command cd we tell her 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 are called relative paths. 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 the relative path

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

Usage ".."

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

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

As you can see, the command cd .."Takes us one level up."

Can add .. to a relative path. This will allow us to move to a directory that is at the same level as the one in which we are. 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 ending the conversation about the team cd there are a few more things to mention. 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 an executable file located in the current directory that will be launched for execution.

cd and user's home directory

In order to go to your home directory, you need to type

\$ cd

Without an argument, cd will take you to your home directory. The home directory is usually / root for the superuser, and / home / username / for normal users. But what if we want to point to 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 using ~ –Thylds:

\$ ./myprog ~ / myfile.txt

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

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 fredsfile.txt file in the user's home directory fred:

\$ ./myprog ~ fred / fredsfile.txt

Linux commands

Introduction to ls

You are probably already familiar with the command ls which, invoked 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 can 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 itself ls one or more files or directories can be specified as an argument. If you specify a file name, then the command ls will display information only about this file. And if you specify the name of the directory, ls will show all of its contents. Option ‘-L’ commands ls can be very useful if you want to know more detailed information about them in addition to file names (file permissions, owner name, time of the last file modification 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 3096 Dec 22 20:57 sbin drwxr-xr-x 46 root root 1144 Dec 24 15:32 share drwxr-xr-x 8 root root 328 Dec 26 00:07 src drwxr-xr-x 6 root root 176 Nov 24 14: 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 a little later which letter stands for what) The next column shows the number of links for each element of the list. The third and fourth columns are the owner and group of the file, respectively. The fifth column is 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 it is a link, then after the ‘ –> 'Stands for the name of the object it refers to).

How to see only directories

Sometimes there is a need to look at information only about directories, and not about all of their contents. The option will help to cope 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 drwxr-xr-x 2 root root 3192 Dec 26 12:52 / 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 to action ‘-D’... It allows you to display information about files in a directory recursively. First, the contents of the top-level directory are shown, then the contents of all subdirectories are shown in turn, and so on. The output of this command can be quite lengthy, so we do not provide an example of it, but you can try it yourself by typing ‘ ls -R' or ' ls -Rl‘.

And finally, the option ‘-I’ used to display the inodes of each file system object.

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

What are inodes?

Each file system object (file, directory ...) has its own unique number, called inode(inode number). This information may sound trivial, but understanding the function of inodes will help you understand many operations on the filesystem. For example, let's look at «.» and «..» as 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 you 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 inode 5120 is referenced by two names: / usr / local and / usr / local / bin / .. That is, they are two different names of 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 ‘synonyms’ of a file (filesystem objects referring to the same inode) shows the number in the second column of the ‘ 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 filesystem 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 serves 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

By default, the 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 one by one:

\$ 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, which is invoked by the command man mkdir... Help is available for almost all of the commands in this manual (for example man ls), except cd since it is built into bash (for such commands, the help is invoked 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, 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 command output ls -l) if it already exists or creates a new empty file if it does not already exist. After this operation, we should have an empty file / tmp / copyme.

echo

Now that we have an empty file, 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 line to our file, redirect the output of the command to it echo:

\$ echo "firstfile"> copyme

Sign > (more) tells the shell to redirect the output of the command on the left to the file on the right. If a file with this name does not exist, it will be created automatically. And if such a file already exists, then it will be overwritten (all of 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 a line feed character.

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

cat and cp

To output the contents of a file to the terminal, use the command cat:

\$ cat copyme firstfile

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

\$ cp copyme copiedme

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

\$ ls -i copyme copiedme 648284 copiedme 650704 copyme

mv

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

\$ mv copiedme movedme \$ ls -i movedme 648284 movedme

The inode number does not change only if the renamed file remains within the file system where the original file was located. We'll take a closer look at file system design 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 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 located in 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, moving a file to another file system changes its inode. This is because each filesystem has its own separate set of inodes.

It should be noted that there is a possibility that the newly assigned inode number may coincide with 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 performed operation 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"

Top