Difference between revisions of "One-liners"
(adds "Delete old stuff" section; better intro to X.org) |
|||
(8 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
Sometimes one-liners are so cool, you just want to remember them. And good one-liners can also teach you the intricacies and features of the [[Bash]] shell. Although there are better sites on the Internet for [http://www.bashoneliners.com/ finding one-liners], [http://www.catonmat.net/series/bash-one-liners-explained understanding one-liners] or [http://uni.xkcd.com/ playing on the command line], we'd still like to illustrate a few here. | Sometimes one-liners are so cool, you just want to remember them. And good one-liners can also teach you the intricacies and features of the [[Bash]] shell. Although there are better sites on the Internet for [http://www.bashoneliners.com/ finding one-liners], [http://www.catonmat.net/series/bash-one-liners-explained understanding one-liners] or [http://uni.xkcd.com/ playing on the command line], we'd still like to illustrate a few here. | ||
− | + | == Free Memory == | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | ==Free Memory== | ||
Use <code>echo</code> to output the result of a sub-shell, and a few extra characters (' - + p'), which is then piped to the (reverse-polish) desk calculator. Con<code>cat</code>enate the /proc/meminfo file, printing it on STDOUT. Using extended-regex <code>grep</code>, we search for lines of output that begin with "MemFree", "Cached" or "Writeback" followed by the colon character. Piping to <code>awk</code>, we can print out the string in position 2 of each line. Those values are ultimately processed in the calculator by popping the last two numbers off the stack (Writeback and Cached), and adding that result to the first number (MemFree).<ref>[http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained Cache explained]</ref> | Use <code>echo</code> to output the result of a sub-shell, and a few extra characters (' - + p'), which is then piped to the (reverse-polish) desk calculator. Con<code>cat</code>enate the /proc/meminfo file, printing it on STDOUT. Using extended-regex <code>grep</code>, we search for lines of output that begin with "MemFree", "Cached" or "Writeback" followed by the colon character. Piping to <code>awk</code>, we can print out the string in position 2 of each line. Those values are ultimately processed in the calculator by popping the last two numbers off the stack (Writeback and Cached), and adding that result to the first number (MemFree).<ref>[http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained Cache explained]</ref> | ||
<source lang="bash"> | <source lang="bash"> | ||
Line 33: | Line 11: | ||
</pre> | </pre> | ||
− | ==Size of Graphical Desktop (X Window System)== | + | == Size of Graphical Desktop (X Window System) == |
So you think your graphical desktop is slowing things down compared to using a pure console based system. Short of logging in single user mode, how much memory does the graphical desktop consume? Since everything is a file, we can look in the folder for processes (/proc), and specifically the folder created for the process id of "X" ([http://x.org X.org]). <code>grep</code>ping for the line starting with 'VmSize', we can see the Virtual Memory size of our graphical desktop. | So you think your graphical desktop is slowing things down compared to using a pure console based system. Short of logging in single user mode, how much memory does the graphical desktop consume? Since everything is a file, we can look in the folder for processes (/proc), and specifically the folder created for the process id of "X" ([http://x.org X.org]). <code>grep</code>ping for the line starting with 'VmSize', we can see the Virtual Memory size of our graphical desktop. | ||
<source lang="bash">grep ^VmSize /proc/$(pidof X)/status</source> | <source lang="bash">grep ^VmSize /proc/$(pidof X)/status</source> | ||
Line 41: | Line 19: | ||
</pre> | </pre> | ||
− | ==Delete old stuff== | + | == Delete old stuff == |
You stumble upon a directory full of backups, which is great. But you also realize that nobody setup <code>logrotate</code> or other command to prune old content. Maybe that's because these backups are produced manually, say during upgrades, and so they are also deleted manually. What's a quick one-liner to remove old files? Use the <code>mtime</code> (modification time) option to <code>find</code> combined with the <code>exec</code> option to execute <code>rm</code> (remove) said files. | You stumble upon a directory full of backups, which is great. But you also realize that nobody setup <code>logrotate</code> or other command to prune old content. Maybe that's because these backups are produced manually, say during upgrades, and so they are also deleted manually. What's a quick one-liner to remove old files? Use the <code>mtime</code> (modification time) option to <code>find</code> combined with the <code>exec</code> option to execute <code>rm</code> (remove) said files. | ||
<source lang="bash"> | <source lang="bash"> | ||
Line 52: | Line 30: | ||
</source> | </source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{{References}} | {{References}} | ||
[[Category:Bash]] | [[Category:Bash]] | ||
[[Category:System Administration]] | [[Category:System Administration]] | ||
− |
Revision as of 08:48, 8 October 2015
Sometimes one-liners are so cool, you just want to remember them. And good one-liners can also teach you the intricacies and features of the Bash shell. Although there are better sites on the Internet for finding one-liners, understanding one-liners or playing on the command line, we'd still like to illustrate a few here.
Contents
Free Memory[edit | edit source]
Use echo
to output the result of a sub-shell, and a few extra characters (' - + p'), which is then piped to the (reverse-polish) desk calculator. Concat
enate the /proc/meminfo file, printing it on STDOUT. Using extended-regex grep
, we search for lines of output that begin with "MemFree", "Cached" or "Writeback" followed by the colon character. Piping to awk
, we can print out the string in position 2 of each line. Those values are ultimately processed in the calculator by popping the last two numbers off the stack (Writeback and Cached), and adding that result to the first number (MemFree).[1]
echo $(cat /proc/meminfo | egrep '^(MemFree|Cached|Writeback):' | awk '{print $2}') - + p | dc
Result:
3033240
Size of Graphical Desktop (X Window System)[edit | edit source]
So you think your graphical desktop is slowing things down compared to using a pure console based system. Short of logging in single user mode, how much memory does the graphical desktop consume? Since everything is a file, we can look in the folder for processes (/proc), and specifically the folder created for the process id of "X" (X.org). grep
ping for the line starting with 'VmSize', we can see the Virtual Memory size of our graphical desktop.
grep ^VmSize /proc/$(pidof X)/status
Result:
VmSize: 158212 kB
Delete old stuff[edit | edit source]
You stumble upon a directory full of backups, which is great. But you also realize that nobody setup logrotate
or other command to prune old content. Maybe that's because these backups are produced manually, say during upgrades, and so they are also deleted manually. What's a quick one-liner to remove old files? Use the mtime
(modification time) option to find
combined with the exec
option to execute rm
(remove) said files.
# Make sure we've got backups; look for recent files
sudo ls -al /backups
# list everything in the backups folder that's older than 30 days
sudo find /backups -mtime +30 -ls
# OK, delete those files
sudo find /backups -mtime +30 -exec rm {} \;