2019年8月28日
2019年8月9日
Linux 學習
Other users' home directories
Bash will expand a lone ~ to point to your home directory, but you can also use it to point to
other users' home directories. For example, if we wanted to refer to a file called fredsfile.txt in
Fred's home directory, we could type:
$ ./myprog ~fred/fredsfile.txt
my home directories
example: $ ./myprog ~/myfile.txt
Looking at directories
Sometimes, you'll want to look at a directory, rather than inside it. For these situations, you
can specify the -d option, which will tell ls to look at any directories that it would normally
look inside:
$ 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
$ ls -dl /usr/local
drwxr-xr-x 8 root root 240 Dec 22 20:57 /usr/local
If we take a look at the second column from the left, we see that the directory /usr/local
(inode 5120) is referenced eight times. On my system, here are the various paths that
reference this inode:
/usr/local
/usr/local/.
/usr/local/bin/..
/usr/local/games/..
/usr/local/lib/..
/usr/local/sbin/..
/usr/local/share/..
/usr/local/src/..
mkdir -p
However, mkdir has a handy -p option that tells mkdir to create any missing parent
directories, as you can see here:
$ mkdir -p easy/as/pie
After typing this command, myfile.txt will be moved to /home/drobbins/myfile.txt. And if
/home/drobbins is on a different filesystem than /var/tmp, the mv command will handle the
copying of myfile.txt to the new filesystem and erasing it from the old filesystem. As you might guess, when myfile.txt is moved between filesystems, the myfile.txt at the new location
will have a new inode number. This is because every filesystem has its own independent set
of inode numbers.
A given inode can have any number of hard links, and
the inode will persist on the filesystem until the all the hard links disappear. When the last
hard link disappears and no program is holding the file open, Linux will delete the file
automatically.
you can only make hard links to files,not directories.
The second limitation
of hard links is that they can't span filesystems. This means that you can't create a link from
/usr/bin/bash to /bin/bash if your / and /usr directories exist on separate filesystems.
Note that double quotes will work similarly to single quotes, but will still allow bash to do
some limited expansion. Therefore, single quotes are your best bet when you are truly
interested in passing literal text to a command.
https://www.linuxjournal.com/content/bash-extended-globbing.
http://www.globtester.com/ globbing debuger
If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched.
wayne.su@rd1-4:~/configdec$ ls
myfile1 myfile2 myfile3 myfile4
wayne.su@rd1-4:~/configdec$ ls myfile[!2]
myfile1 myfile3 myfile4
wayne.su@rd1-4:~/configdec$ ls myfile[^2]
myfile1 myfile3 myfile4
You can reverse the meaning of the square brackets by putting a ^ immediately after the [.
In this case, the brackets will match any character that is not listed inside the brackets.
Again, note that we use [^] with regular expressions
$ grep dev.hda[^12] /etc/fstab
^ and $ can be combined to match an entire line. For example, the following regex will match
a line that starts with the # character and ends with the . character, with any number of other
characters in between:
$ grep '^#.*\.$' /etc/fstab
# /etc/fstab: static file system information
When bash starts it opens the three standard file descriptors: stdin (file descriptor 0), stdout (file descriptor 1), and stderr (file descriptor 2). You can open more file descriptors (such as 3, 4, 5, ...), and you can close them
~ # ls -l a
ls: a: No such file or directory
~ # ls -l a 2>abc
~ # cat abc
ls: a: No such file or directory
~ # ls -l a 1>abc
ls: a: No such file or directory
~ # cat abc
~ # ls -l a &>abc
~ # cat abc
ls: a: No such file or directory
https://catonmat.net/bash-one-liners-explained-part-three
bash exit code
http://www.tldp.org/LDP/abs/html/exitcodes.html
makefile .PHONY的作用
讓cc變成一個偽目標,即使沒有cc這個檔案也可以執行。
Bash will expand a lone ~ to point to your home directory, but you can also use it to point to
other users' home directories. For example, if we wanted to refer to a file called fredsfile.txt in
Fred's home directory, we could type:
$ ./myprog ~fred/fredsfile.txt
my home directories
example: $ ./myprog ~/myfile.txt
Looking at directories
Sometimes, you'll want to look at a directory, rather than inside it. For these situations, you
can specify the -d option, which will tell ls to look at any directories that it would normally
look inside:
$ 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
$ ls -dl /usr/local
drwxr-xr-x 8 root root 240 Dec 22 20:57 /usr/local
If we take a look at the second column from the left, we see that the directory /usr/local
(inode 5120) is referenced eight times. On my system, here are the various paths that
reference this inode:
/usr/local
/usr/local/.
/usr/local/bin/..
/usr/local/games/..
/usr/local/lib/..
/usr/local/sbin/..
/usr/local/share/..
/usr/local/src/..
mkdir -p
However, mkdir has a handy -p option that tells mkdir to create any missing parent
directories, as you can see here:
$ mkdir -p easy/as/pie
After typing this command, myfile.txt will be moved to /home/drobbins/myfile.txt. And if
/home/drobbins is on a different filesystem than /var/tmp, the mv command will handle the
copying of myfile.txt to the new filesystem and erasing it from the old filesystem. As you might guess, when myfile.txt is moved between filesystems, the myfile.txt at the new location
will have a new inode number. This is because every filesystem has its own independent set
of inode numbers.
A given inode can have any number of hard links, and
the inode will persist on the filesystem until the all the hard links disappear. When the last
hard link disappears and no program is holding the file open, Linux will delete the file
automatically.
you can only make hard links to files,not directories.
The second limitation
of hard links is that they can't span filesystems. This means that you can't create a link from
/usr/bin/bash to /bin/bash if your / and /usr directories exist on separate filesystems.
Note that double quotes will work similarly to single quotes, but will still allow bash to do
some limited expansion. Therefore, single quotes are your best bet when you are truly
interested in passing literal text to a command.
wildcards=globbing
https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.htmlhttps://www.linuxjournal.com/content/bash-extended-globbing.
http://www.globtester.com/ globbing debuger
If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched.
wayne.su@rd1-4:~/configdec$ ls
myfile1 myfile2 myfile3 myfile4
wayne.su@rd1-4:~/configdec$ ls myfile[!2]
myfile1 myfile3 myfile4
wayne.su@rd1-4:~/configdec$ ls myfile[^2]
myfile1 myfile3 myfile4
regular expression
grep regex operator | Meaning | Example |
. | Matches any single character. | grep '.' file grep 'foo.' input |
? | The preceding item(k) is optional and will be matched zero or at most, once. | grep 'vivek?' /etc/passwd |
* | The preceding item(k) will be matched zero or more times. | grep 'vivek*' /etc/passwd |
+ | The preceding item will be matched one or more times. | ls /var/log/ | grep -E "^[a-z]+\.log." |
{N} | The preceding item is matched exactly N times. | egrep '[0-9]{2} input |
{N,} | The preceding item is matched N or more times. | egrep '[0-9]{2,} input |
{N,M} | The preceding item is matched at least N times, but not more than M times. | egrep '[0-9]{2,4} input |
– | Represents the range if it’s not first or last in a list or the ending point of a range in a list. | grep ':/bin/[a-z]*' /etc/passwd |
^ | Matches the empty string at the beginning of a line; also represents the characters not in the range of a list. | grep '^vivek' /etc/passwd grep '[^0-9]*' /etc/passwd |
$ | Matches the empty string at the end of a line. | grep '^$' /etc/passwd |
\b | Matches the empty string at the edge of a word. | vivek '\bvivek' /etc/passwd |
\B | Matches the empty string provided it’s not at the edge of a word. | grep '\B/bin/bash /etc/passwd |
\< | Match the empty string at the beginning of word. | grep '\<="" kbd="" style="box-sizing: inherit;"> |
\> | Match the empty string at the end of word. | grep 'bash\>' /etc/passwd grep '\' /etc/passwd |
ref:
- https://www.digitalocean.com/community/tutorials/using-grep-regular-expressions-to-search-for-text-patterns-in-linux
- https://www.cyberciti.biz/faq/grep-regular-expressions/
- https://www.debuggex.com/r/tMQ8TzNdVe3jzS5y 好用的Debug regular
You can reverse the meaning of the square brackets by putting a ^ immediately after the [.
In this case, the brackets will match any character that is not listed inside the brackets.
Again, note that we use [^] with regular expressions
$ grep dev.hda[^12] /etc/fstab
/dev/hda3 / reiserfs noatime,ro 1 1
#/dev/hda4 /mnt/extra reiserfs noatime,rw 1 1
a line that starts with the # character and ends with the . character, with any number of other
characters in between:
$ grep '^#.*\.$' /etc/fstab
# /etc/fstab: static file system information
When bash starts it opens the three standard file descriptors: stdin (file descriptor 0), stdout (file descriptor 1), and stderr (file descriptor 2). You can open more file descriptors (such as 3, 4, 5, ...), and you can close them
~ # ls -l a
ls: a: No such file or directory
~ # ls -l a 2>abc
~ # cat abc
ls: a: No such file or directory
~ # ls -l a 1>abc
ls: a: No such file or directory
~ # cat abc
~ # ls -l a &>abc
~ # cat abc
ls: a: No such file or directory
https://catonmat.net/bash-one-liners-explained-part-three
bash exit code
http://www.tldp.org/LDP/abs/html/exitcodes.html
makefile .PHONY的作用
讓cc變成一個偽目標,即使沒有cc這個檔案也可以執行。
fakeroot vs sudo
Why not just use sudo?
First of all, you don't need root privileges to build software and you don't need root privileges to compress them. So if you don't need it, you'd have to really be a Windows user to even think of getting that permission. But sarcasm aside, you may not even have root password.
Besides, let's say you do have root permissions. And let's say you want to pretend that a file should have read access only to the root. So you sudo, actually change the file owner and permissions to root, you get out of root shell and try to package everything. You fail because now you cannot read the file anymore since you don't have root access. So you have to sudo and compress and build the package as root. Effectively, you have to do everything as root.
This is BadTM.
As a packager, you don't need root permissions and you shouldn't get it. When you install a package, you may need to install some file (A) as root and that's where you need root permissions. All fakeroot does is to make this possible. It lets the packager list A as owned by root for the archiver, so that when the package is decompressed by the user, the archiver demands root permission and creates A as owned by root.