And now, we quickly go through the ls command. Most likely you are already familiar with this command, and you know that typing ls will get a list of the contents of the current 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 By specifying the -a option, you can see the complete list, including hidden files and directories starting with ".". As you can see in the following example, ls -a also displays special binding directories "." and "..":
$ 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 You can also specify one or more directories or files on the command line with ls. If you specify a file, ls will show you only this file. And if you specify a directory, ls will display its contents. The -l option is very handy when you need to see the permissions, owner, last modification time, and size in the directory listing.
In the following example, we used the -l option to display the contents of my / 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/tmp The first column shows the permission information for each item. I'll explain how to interpret it a little later. The next column contains the number of links to each element of the file system, we will return to this later. The third and fourth columns are a list of owners and groups, respectively. The fifth column is the size of the object. The sixth is the time of the last change (mtime) of the object. And finally, the last column with the names of the objects. If the files are symbolic links, you will see an arrow -> and the path to which this symbolic link points.
Sometimes you want to look at the directory, not inside it. In this case, you can specify the -d option, which tells ls to treat any directory as internal:
$ 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 So you can use -d to look at the directory, but you can also use -R for the opposite: not only look inside the directory, but also recursively look at all the directories with the files inside it! We will not include any sample output for this option in the manual (since it is usually very voluminous), but you may want to try several ls -R and ls -Rl commands to get a feel for how it works.
Finally, the -i option can be used to display the number of inodes for objects in the file system list:
$ 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-x86 1585 lib 5132 portage.old 784 tmp Each file system object is assigned a unique index, called an inode number. This may seem trivial, but the concept of inodes is very important for understanding most of the operations in the file system. Consider for example the links "." and ".." that appear in each directory. To fully understand what the ".." directory really is, we first look at the inode number of / usr / local :
$ ls -id /usr/local
5120 /usr/localIn the / usr / local directory, the inode number is 5120. And now let's see the inode number of / usr / local / bin / .. :
$ ls -id /usr/local/bin/..
5120 /usr/local/bin/..As you can see, the / usr / local / bin / .. directory has the same number as / usr / local ! Let's see how to deal with this shocking revelation. In the past, we assumed that / usr / local is itself a directory. Now, we have found that in fact the directory is the inode with the number 5120, and we have found at least two elements (called “links”) that point to this inode. Both / usr / local and / usr / local / bin / .. are references to the 5120th inode. Although this inode exists only in one place on the disk, nevertheless there can be many references to it.
In fact, we can even see the total number of links leading to this, 5120 inode, using the ls -dl command:
$ ls -dl /usr/local
drwxr-xr-x 8 root root 240 Dec 22 20:57 /usr/local If you look at the second column on the left, you can see that the / usr / local directory (inode 5120) is referred to eight times. On my system, the following paths lead to this inode:
/usr/local
/usr/local/.
/usr/local/bin/..
/usr/local/games/..
/usr/local/lib/..
/usr/local/sbin/..
/usr/local/share/..
/usr/local/src/..Let's quickly go through the command mkdir, which is used to create new directories. The following example creates three new directories, tic , tac , and toe , all inside / tmp :
$ cd /tmp
$ mkdir tic tac toeBy default, the mkdir command does not create parent directories for you; all the way down to the last (created) element must exist. So, if you want to create the won / der / ful subdirectories , you have to execute three separate mkdir commands:
$ mkdir won/der/ful
mkdir: cannot create directory `won/der/ful': No such file or directory
$ mkdir won
$ mkdir won/der
$ mkdir won/der/fulHowever, mkdir has a very convenient option -p, which tells mkdir to create any missing parent directories, as you can see here:
$ mkdir -p easy/as/pieIn general, very simple. To learn more about the mkdir command, type man mkdir and read the instructions. The same applies to almost all the commands discussed here (for example, man ls), excluding cd, which is built into bash.
Now we are going to take a look at the cp and mv commands used to copy, rename and move files and directories. But let's start the review using the touch command to create a file in / tmp :
$ cd /tmp
$ touch copymeThe touch command updates the “mtime” (last modification time - approx. Lane) file if it exists (remember the sixth column in the ls -l output). If the file does not exist, a new, empty file will be created. You should now have a zero-size / tmp / copyme file.
Now that the file exists, let's add some data to it. You can do this with the echo command, which takes arguments and prints them to standard output. First, the echo command itself:
$ echo "firstfile"
firstfileAnd now, the same echo command, but with output redirection:
$ echo "firstfile" > copymeThe “more” sign tells the shell to write echo output to a file called copyme . This file will be created if it did not exist, or overwritten if it exists. Typing ls -l, we see that the copyme file is 10 bytes in size, since it contains the word firstfile and a newline character:
$ ls -l copyme
-rw-r--r-- 1 root root 10 Dec 28 14:13 copyme To output the contents of the file to the terminal, use the cat command:
$ cat copyme
firstfileNow, we can use the main call to the cp command to create the copiedme file from the original copyme :
$ cp copyme copiedmeBelow we check that these are really different files; they have different inode numbers:
$ ls -i copyme copiedme
648284 copiedme 650704 copyme Now let's use the mv command to rename the copiedme to movedme . The inode number will remain the same; however, the file name indicating the inode will change.
$ mv copiedme movedme
$ ls -i movedme
648284 movedme The inode number of the moved file remains as long as the destination file is on the same file system as the source file. We will take a closer look at the file system in the third part of our guide.
While we are talking about mv, let's see how else you can use this command. mv, in addition to the ability to rename files, allows you to move one or more files to another location in the directory hierarchy. For example, to move /var/tmp/myfile.txt to the / home / drobbins directory (which is my homepage ), I will type (and could use ~ - approx. Lane):
$ mv /var/tmp/myfile.txt /home/drobbinsAfter that, myfile.txt will be moved to /home/drobbins/myfile.txt . And if / home / drobbins resides in a different file system than / var / tmp , the mv command will copy myfile.txt to the new file system and remove it from the old one. As you might have guessed when myfile.txt moves between file systems, then myfile.txt at the new location gets a new inode number. This is all because each file system has its own independent set of inode numbers.
We can also use mv to move multiple files into one directory. For example, to move myfile1.txt and myarticle3.txt to / home / drobbins , you need to type:
$ mv /var/tmp/myfile1.txt /var/tmp/myarticle3.txt /home/drobbinsDaniel Robbins is the founder of the Gentoo community and the creator of the Gentoo Linux operating system. Daniel lives in New Mexico with his wife, Mary, and two energetic daughters. He is also the founder and head of Funtoo , has written many technical articles for IBM developerWorks , Intel Developer Services and the C / C ++ Users Journal.
Chris Hauser was a UNIX supporter since 1994 when he joined the team of administrators at Taylor University (Indiana, USA), where he received a bachelor's degree in computer science and mathematics. After that, he worked in many areas, including web applications, video editing, drivers for UNIX, and cryptographic protection. Currently working in Sentry Data Systems. Chris also contributed to many free projects, such as Gentoo Linux and Clojure, co-authored The Joy of Clojure .
Airon Griffis lives in Boston, where he spent the last decade working with Hewlett-Packard on projects such as UNIX network drivers for Tru64, Linux security certification, Xen and KVM virtualization, and most recently, the HP ePrint platform. In his spare time, Airon prefers to ponder over the problems of programming while riding his bike, juggling bits, or cheering on the Boston Red Baseball team.
Source: https://habr.com/ru/post/99291/
All Articles