One-liners: Difference between revisions
m Text replacement - "<(\/?)source" to "<$1syntaxhighlight" |
add a find example |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 7: | Line 7: | ||
<syntaxhighlight lang="bash">du -cks -- * | sort -rn | head</syntaxhighlight> | <syntaxhighlight lang="bash">du -cks -- * | sort -rn | head</syntaxhighlight> | ||
<code>du --total --block-size=1K --summarize</code> and the double dash argument means 'take the arguments from STDIN' then the asterisk is the glob character that matches 'everything in this directory', so each file and directory in the current working directory is summarized. This is the piped to <code>sort</code> with the reverse, numeric options and then piped to <code>head</code> for showing just the top 10. Adjust to taste. | <code>du --total --block-size=1K --summarize</code> and the double dash argument means 'take the arguments from STDIN' then the asterisk is the glob character that matches 'everything in this directory', so each file and directory in the current working directory is summarized. This is the piped to <code>sort</code> with the reverse, numeric options and then piped to <code>head</code> for showing just the top 10. Adjust to taste. | ||
== Truncate a file == | |||
Redirecting (null) input to the file will truncate that file. | |||
If the 'noclobber' option is set, then trying to redirect to an existing file will give an error message. You can work around this without changing your noclobber setting by simply using a pipe, as shown in the second instance. By the way, you can check all options with <code>set -o</code><syntaxhighlight lang="bash"> | |||
> /my/big/debug.log | |||
# or, add a pipe | |||
>| /my/big/debug.log | |||
</syntaxhighlight>If you need to be root to overwrite the file, then either use sudo with tee or the truncate command | |||
<code>echo -n "" | sudo tee error.log</code> | |||
The <code>-n</code> option is to avoid the automatic trailing newline - so it truly makes the file empty. | |||
<code>sudo truncate --size=0 error.log</code> | |||
<code>-s</code> is the short option for <code>--size</code> so <code>sudo truncate -s0 error.log</code> works identically. | |||
==Mount remote filesystem== | ==Mount remote filesystem== | ||
| Line 13: | Line 30: | ||
==Compare two wikis for extensions and skins== | ==Compare two wikis for extensions and skins== | ||
This one-liner invokes the API of two wikis asking for info on siteinfo, general, extensions and skins; in json format. Since that data is returned without any newlines, we use | This one-liner invokes the API of two wikis asking for info on siteinfo, general, extensions and skins; in json format. Since that data is returned without any newlines, we use <code>jq</code> to pretty-print the json output. Then it's an easy <code>meld</code> or <code>diff</code> to compare them. The <code>--silent</code> option to <code>curl</code> just suppresses the connection and retrieval metadata; while the <code>-L</code> is customary to follow redirects. | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
A='https://freephile.org/' B='https://www.mediawiki.org/' API=' | A='https://wiki.freephile.org/wiki' B='https://www.mediawiki.org/w' API='/api.php?action=query&meta=siteinfo&siprop=general%7Cextensions%7Cskins&format=json' meld <(curl --silent -L "${A}${API}" | jq '.') <(curl --silent -L "${B}${API}" | jq '.')</syntaxhighlight> | ||
The wiki farm detection has changed (@fixme) on this site making it slightly different from mediawiki.org. Still, the one-liner should work but is failing silently whereas a manually executed substitution of <code>meld <(curl --silent -L "https://wiki.freephile.org/wiki/api.php?action=query&meta=siteinfo&siprop=general%7Cextensions%7Cskins&format=json" | jq '.') <(curl --silent -L "https://www.mediawiki.org/w/api.php?action=query&meta=siteinfo&siprop=general%7Cextensions%7Cskins&format=json" | jq '.')</code> does launch meld with the proper content. | |||
Using more than a single line of code, you can do things with PHP's <code>yaml_parse</code> such as [https://github.com/freephile/meza/blob/37b9be51289b16868730cbc1070a188159323ee7/src/scripts/findDupesInLocalExtensions.php src/scripts/findDupesInLocalExtensions.php] | |||
==Perl edit== | ==Perl edit== | ||
| Line 77: | Line 99: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Find with exclusions == | |||
Trying to fix [[permissions]], I wanted to see if there were any executable files in the 'data' directory. It turned out that every file in the elasticsearch and uploads directories were +x somehow. So, I wanted to exclude those dirs and see if there was anything else. | |||
<syntaxhighlight lang="bash"> | |||
sudo find /opt/data-meza/ \( -name uploads -o -name elasticsearch \) -prune -o -type f -perm -ugo=x -ls | |||
</syntaxhighlight> | |||
{{References}} | {{References}} | ||