|
1 <stránka |
|
2 xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana" |
|
3 xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro"> |
|
4 |
|
5 <nadpis>Reading files metadata using relpipe-in-filesystem</nadpis> |
|
6 <perex>accessing file metadata like path, type, size or owner</perex> |
|
7 <m:pořadí-příkladu>01200</m:pořadí-příkladu> |
|
8 |
|
9 <text xmlns="http://www.w3.org/1999/xhtml"> |
|
10 |
|
11 <p> |
|
12 Our filesystems contain valuable information and using proper tools we can extract them. |
|
13 Using <code>relpipe-in-filesystem</code> we can gather metadata of our files and process them in relational way. |
|
14 This tools does not traverse our filesystem (remember the rule: <em>do one thing and do it well</em>), |
|
15 instead, it eats a list of file paths separated by <code>\0</code>. |
|
16 It is typically used together with the <code>find</code> command, but we can also create such list by hand using e.g. <code>printf</code> command or <code>tr \\n \\0</code>. |
|
17 </p> |
|
18 |
|
19 <m:pre jazyk="bash">find /etc/ssh/ -print0 | relpipe-in-filesystem | relpipe-out-tabular</m:pre> |
|
20 |
|
21 <p> |
|
22 In the basic scenario, it behaves like <code>ls -l</code>, just more modular and machine-readable: |
|
23 </p> |
|
24 |
|
25 <pre><![CDATA[filesystem: |
|
26 ╭───────────────────────────────────┬───────────────┬────────────────┬────────────────┬────────────────╮ |
|
27 │ path (string) │ type (string) │ size (integer) │ owner (string) │ group (string) │ |
|
28 ├───────────────────────────────────┼───────────────┼────────────────┼────────────────┼────────────────┤ |
|
29 │ /etc/ssh/ │ d │ 0 │ root │ root │ |
|
30 │ /etc/ssh/moduli │ f │ 553122 │ root │ root │ |
|
31 │ /etc/ssh/ssh_host_ecdsa_key │ f │ 227 │ root │ root │ |
|
32 │ /etc/ssh/sshd_config │ f │ 3262 │ root │ root │ |
|
33 │ /etc/ssh/ssh_host_ed25519_key.pub │ f │ 91 │ root │ root │ |
|
34 │ /etc/ssh/ssh_host_ecdsa_key.pub │ f │ 171 │ root │ root │ |
|
35 │ /etc/ssh/ssh_host_rsa_key │ f │ 1679 │ root │ root │ |
|
36 │ /etc/ssh/ssh_config │ f │ 1580 │ root │ root │ |
|
37 │ /etc/ssh/ssh_host_ed25519_key │ f │ 399 │ root │ root │ |
|
38 │ /etc/ssh/ssh_import_id │ f │ 338 │ root │ root │ |
|
39 │ /etc/ssh/ssh_host_rsa_key.pub │ f │ 391 │ root │ root │ |
|
40 ╰───────────────────────────────────┴───────────────┴────────────────┴────────────────┴────────────────╯ |
|
41 Record count: 11]]></pre> |
|
42 |
|
43 <p> |
|
44 We can specify desired attributes and also their aliases: |
|
45 </p> |
|
46 |
|
47 <m:pre jazyk="bash"><![CDATA[find /etc/ssh/ -print0 \ |
|
48 | relpipe-in-filesystem \ |
|
49 --file path --as artefact \ |
|
50 --file size \ |
|
51 --file owner --as dear_owner \ |
|
52 | relpipe-out-tabular]]></m:pre> |
|
53 |
|
54 <p>And we will get a subset with renamed attributes:</p> |
|
55 |
|
56 <pre><![CDATA[filesystem: |
|
57 ╭───────────────────────────────────┬────────────────┬─────────────────────╮ |
|
58 │ artefact (string) │ size (integer) │ dear_owner (string) │ |
|
59 ├───────────────────────────────────┼────────────────┼─────────────────────┤ |
|
60 │ /etc/ssh/ │ 0 │ root │ |
|
61 │ /etc/ssh/moduli │ 553122 │ root │ |
|
62 │ /etc/ssh/ssh_host_ecdsa_key │ 227 │ root │ |
|
63 │ /etc/ssh/sshd_config │ 3262 │ root │ |
|
64 │ /etc/ssh/ssh_host_ed25519_key.pub │ 91 │ root │ |
|
65 │ /etc/ssh/ssh_host_ecdsa_key.pub │ 171 │ root │ |
|
66 │ /etc/ssh/ssh_host_rsa_key │ 1679 │ root │ |
|
67 │ /etc/ssh/ssh_config │ 1580 │ root │ |
|
68 │ /etc/ssh/ssh_host_ed25519_key │ 399 │ root │ |
|
69 │ /etc/ssh/ssh_import_id │ 338 │ root │ |
|
70 │ /etc/ssh/ssh_host_rsa_key.pub │ 391 │ root │ |
|
71 ╰───────────────────────────────────┴────────────────┴─────────────────────╯ |
|
72 Record count: 11]]></pre> |
|
73 |
|
74 <p> |
|
75 We can also choose, which path format fits our needs best: |
|
76 </p> |
|
77 |
|
78 |
|
79 <m:pre jazyk="bash"><![CDATA[find ../../etc/ssh/ -print0 \ |
|
80 | relpipe-in-filesystem \ |
|
81 --file path \ |
|
82 --file path_absolute \ |
|
83 --file path_canonical \ |
|
84 --file name \ |
|
85 | relpipe-out-tabular]]></m:pre> |
|
86 |
|
87 <p>The <code>path</code> attribute contains the exact same value as was on input. Other formats are derived:</p> |
|
88 |
|
89 <pre><![CDATA[filesystem: |
|
90 ╭────────────────────────────────────────┬───────────────────────────────────────────────────┬───────────────────────────────────┬──────────────────────────╮ |
|
91 │ path (string) │ path_absolute (string) │ path_canonical (string) │ name (string) │ |
|
92 ├────────────────────────────────────────┼───────────────────────────────────────────────────┼───────────────────────────────────┼──────────────────────────┤ |
|
93 │ ../../etc/ssh/ │ /home/hack/../../etc/ssh/ │ /etc/ssh │ │ |
|
94 │ ../../etc/ssh/moduli │ /home/hack/../../etc/ssh/moduli │ /etc/ssh/moduli │ moduli │ |
|
95 │ ../../etc/ssh/ssh_host_ecdsa_key │ /home/hack/../../etc/ssh/ssh_host_ecdsa_key │ /etc/ssh/ssh_host_ecdsa_key │ ssh_host_ecdsa_key │ |
|
96 │ ../../etc/ssh/sshd_config │ /home/hack/../../etc/ssh/sshd_config │ /etc/ssh/sshd_config │ sshd_config │ |
|
97 │ ../../etc/ssh/ssh_host_ed25519_key.pub │ /home/hack/../../etc/ssh/ssh_host_ed25519_key.pub │ /etc/ssh/ssh_host_ed25519_key.pub │ ssh_host_ed25519_key.pub │ |
|
98 │ ../../etc/ssh/ssh_host_ecdsa_key.pub │ /home/hack/../../etc/ssh/ssh_host_ecdsa_key.pub │ /etc/ssh/ssh_host_ecdsa_key.pub │ ssh_host_ecdsa_key.pub │ |
|
99 │ ../../etc/ssh/ssh_host_rsa_key │ /home/hack/../../etc/ssh/ssh_host_rsa_key │ /etc/ssh/ssh_host_rsa_key │ ssh_host_rsa_key │ |
|
100 │ ../../etc/ssh/ssh_config │ /home/hack/../../etc/ssh/ssh_config │ /etc/ssh/ssh_config │ ssh_config │ |
|
101 │ ../../etc/ssh/ssh_host_ed25519_key │ /home/hack/../../etc/ssh/ssh_host_ed25519_key │ /etc/ssh/ssh_host_ed25519_key │ ssh_host_ed25519_key │ |
|
102 │ ../../etc/ssh/ssh_import_id │ /home/hack/../../etc/ssh/ssh_import_id │ /etc/ssh/ssh_import_id │ ssh_import_id │ |
|
103 │ ../../etc/ssh/ssh_host_rsa_key.pub │ /home/hack/../../etc/ssh/ssh_host_rsa_key.pub │ /etc/ssh/ssh_host_rsa_key.pub │ ssh_host_rsa_key.pub │ |
|
104 ╰────────────────────────────────────────┴───────────────────────────────────────────────────┴───────────────────────────────────┴──────────────────────────╯ |
|
105 Record count: 11]]></pre> |
|
106 |
|
107 <p> |
|
108 We can also <em>select</em> symlink targets or their types. |
|
109 If some file is missing or is inaccessible due to permissions, only <code>path</code> is printed for it. |
|
110 </p> |
|
111 |
|
112 <p> |
|
113 Tip: if we are looking for files in the current directory and want omit the „.“ we just call: <code>find -printf '%P\0'</code> instead of <code>find -print0</code>. |
|
114 </p> |
|
115 |
|
116 </text> |
|
117 |
|
118 </stránka> |