Linux Basics: Introduction to bash. Using absolute and relative paths in the console Introduction to ls
All files in Linux have a specific address in the file system with which we can access them using a file manager or console utilities. It's pretty simple theme, but many beginners have difficulty with this.
In today's short article 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 you had difficulties with this before, 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; that was done earlier. We will focus on working with files.
The most important difference is that the file address does not start 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 is /. And here we need to talk about addresses. Linux file paths use the 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 full path to the file on the desktop looked like C:\Users\Sergiy\Desktop\ then the file path in Linux would be simply /home/sergiy/desktop/. With this, everything is simple and clear so far. But problems arise further.
IN operating system Linux can have several kinds of file paths. Let's look at what paths there 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;
- Linux relative path- this is the path to the file relative to the current folder; such paths often cause confusion.
- Path relative to the current user's home folder.- path in the file system, but not from the root, but from the current user’s folder.
Let’s now take a closer look at what these paths look like in Linux, and also look at a few examples to make it completely clear. For the 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 what the full Linux path to one of the files will look like:
ls /home/sergiy/tmp/file1
This is already a relative Linux path, which starts from the home folder, it is designated ~/. Note, not ~, namely ~/. Then you can 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 a higher folder. This opens up even more ample opportunities to navigate through catalogs. For example, to refer to a file in the current folder you can use the following construct:
This is of no use when viewing the contents of a file. But it is very important when executing the program. Because the program will first be searched in the PATH environment, and only then in this folder. 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 you need to look for the file in the current folder, nothing will work.
Such constructs can occur 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 wildcard 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 help a lot. 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 it. Now you know everything you need to not only correctly write the path to linux file, but also perform more complex actions, such as searching for files or navigating through directories using the cd command. If you have any questions, ask in the comments!
Related posts:
I recently described commands for navigating directories in the terminal Linux. So I decided to continue the story about the commands in the console. So the movements seem to have been sorted out. Now I want to look in a little more detail at what directory paths there are in the console.
Let's first look at the most well-known paths to our files and directories, namely absolute paths. Open a terminal and issue a command that displays 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 this kind of movement through directories recently, this movement using an absolute path. But these are not all the 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 count categories relative to the category you are in. They can be used using ".." two dots indicated in front of 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 team ls.
dante@dante-1001PXD:~$ cd /usr
dante@dante-1001PXD:/usr$ ls
dante@dante-1001PXD:/usr$
Here we see a list of available directories. Let's now go to the directory " share».
dante@dante-1001PXD:~$ cd /usr/share
So, now we are in the category /usr/share as we see. Let's now move to a category that is on the same level as the category in which we are. For example, in the category " local" This can be done using an absolute path like this.
dante@dante-1001PXD:~$ cd /usr/local
dante@dante-1001PXD:/usr/share$ pwd
/usr/share
dante@dante-1001PXD:/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.
dante@dante-1001PXD:/usr/share$ cd ../local
dante@dante-1001PXD:/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 move to the next directory, just enter the command in the console regarding 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 on at the moment. Let's go back to the directory /usr and let's look again at what's there.
dante@dante-1001PXD:/usr/local$ cd /usr
dante@dante-1001PXD:/usr$ ls
bin games include lib local sbin share src
dante@dante-1001PXD:/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
dante@dante-1001PXD:/usr$ cd ./local
dante@dante-1001PXD:/usr/local$
As you can see, using relative paths can greatly reduce the commands you enter into the console Linux.
After reading this article, you will find out what it is bash(standard Linux command interpreter), learn how to use standard commands: ls, cp, mv...understand the purpose of inodes, hard and symbolic links, and much more.
This tutorial is intended for those new to Linux and for those who want to review or improve their understanding of basic Linux principles, such as copying and moving files, creating links, using standard Linux commands along with redirections and pipes. In this article you will find many examples that explain 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 use Linux, then you know that after logging in you are greeted by a command interpreter prompt. For example this:
\$If after logging in the graphical shell is loaded, 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 clicking Ctrl–Alt–F1 or Ctrl–Alt–F2 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 invitation is called " shell"(shell), and most likely your command shell is a program called bash.
Are you running bash?
You can check if bash is running with the following command:
\$ echo \$SHELL/bin/bashIf, 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 the 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:
\$ bashWhat is bash
Bash (an 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 are done, you can exit the shell process. After pressing the keys Ctrl–D, teams exit or logout The shell process will be terminated and you will be prompted to enter your username and password again.
Using "cd"
Let's start using bash to navigate the file system. To get started, type the following command:
$ cd/With this command we indicated to 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). Team CD serves to change the current working directory.
Paths
To find out where in the file system you are currently located (current working directory), type:
\$ pwd /In the above example / - command argument CD- called path. This is the location on the file system where we want to move. In this case / - absolute path, this 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 already noticed, all these paths have in common that they start 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 type of path is called relative. Bash, team 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 /usrThen we can go to /usr/local/bin using the relative path
\$ cd local/bin \$ pwd/usr/local/binUsage ".."
Relative paths can contain one or more directories «..» . ".." indicates the parent directory of our working directory. Example:
\$ pwd/usr/local/bin\$ cd.. \$ pwd/usr/localAs you can see, the team cd..‘raises us to a higher level’.
You can add .. to the relative path. This will allow us to move to a directory that is at the same level as the one we are in. Example:
\$ pwd/usr/local\$ cd ../share \$ pwd/usr/shareExamples 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 which directory you will be in using bash.
\$ cd /bin \$ cd ../usr/share/zoneinfo \$ cd /usr/X11R6/bin \$ cd ../lib/X11 \$ cd /usr/bin \$ cd ../bin/../binWorking directory "."
Before we finish talking about the team CD, there are a few more things worth mentioning. Firstly, there is another special directory «.» , which points to the current directory. This directory is used to run executable files located in the current directory.
\$ ./myprogIn 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
To go to your home directory, you need to type
\$ CDWithout an argument, cd will move you to your home directory. For a superuser, the home directory is usually /root, and for regular 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.txtHowever, using absolute paths to files is not always convenient. The same operation can be done using ~ –tildes:
\$ ./myprog ~/myfile.txt~ - a special name that points to the user's home directory in bash.
Other users' home directories
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 indicate the name of this user. For example, to point to the file fredsfile.txt located in user fred's home directory:
\$ ./myprog ~fred/fredsfile.txtLinux commands
Introduction to ls
You are probably already familiar with the team ls, which, called without 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 srcIf you specify the option -a, you will be able to see all files, including hidden ones (whose names begin 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 sslDetailed list of directories
After the command itself ls one or more files or directories can be specified as its argument. If you specify a file name, the command ls will display information only about this file. And if you specify the directory name, ls will show all its contents. Option '-l' teams ls can be very useful if you want to know more than file names 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 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/tmpThe first column shows information about the access rights of each file in the list. (I'll explain which letter means what a little later) The next column shows the number of links to each list element. 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 this is a link, then after the ‘ –> ‘ stands for 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 you 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/binRecursive list and inode information
Option action '-R' opposite of action '-d'. It allows you to display information about files located in a 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 lengthy, so we're not providing an example, but you can try it yourself by typing command line ‘ls -R' or ' ls -Rl‘.
And finally, the option '-i' used to print 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 92 portage 36737 ssl 70744 gentoo-x86 1585 lib 5132 portage.old 784 tmpWhat are inodes?
Each file system object (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 directory /use/local
\$ ls -id /usr/local 5120 /usr/localAs we can see, the inode of the directory /usr/local is 5120. Now let’s see what inode the directory /usr/local/bin/.. 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, these are two different names for the same directory. Each inode points to a specific location on the disk.
Each inode can have multiple filesystem object names associated with it. The number of file 'synonyms' (file system objects referencing the same inode) is shown by the number in the second column of the ' command output ls -l‘.
\$ ls -dl /usr/local drwxr-xr-x 8 root root 240 Dec 22 20:57 /usr/localThis example shows (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 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 toeDefault 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/fulYou 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/pieTo learn more about the capabilities of this utility, read the help, which is called by the command man mkdir. There is help for almost all the 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 studying 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 copymeTeam touch updates the last access time of the file (sixth column of the command output ls -l) if it already exists or creates a new empty file if it does not exist yet. 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 into it using the command echo, which outputs the argument passed to it to standard device output (text terminal in our case).
\$ echo "firstfile" firstfileTo write a line to our file, let's redirect the command output to it echo:
\$ echo "firstfile" > copymeSign > (more) tells the shell to redirect the output of the command on the left to the file whose name is 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, it will be overwritten (all its contents will be erased before writing our line). Team '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 by the line feed character.
\$ ls -l copyme-rw-r--r-- 1 root root 10 Dec 28 14:13 copymecat and cp
To output the contents of a file to the terminal, use the command cat:
\$ cat copyme firstfileNow we can begin to analyze the basic functionality of the command cp. This command takes two arguments. The first one is already a name existing file('copyme'), the second is the name of the new copy we want to make ('copiedme').
\$ cp copyme copiedmeWe 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 copymemv
Now let's use 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 movedmeThe inode number does not change only if the renamed file remains within the file system where the original file was located. We will take a closer look at the structure of file systems in one of the following parts of this tutorial.
Team mv allows you not only to rename files, but also to 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/userThe file will be moved to the user's home directory user even if it is located 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, moving a file to another file system changes its inode. This happens because every file system has its own separate set of inodes.
It should be noted that there is a possibility that the new assigned inode number may coincide with the old one, but it is extremely small.
To move several files at the same time to 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.txtIf 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"