Penguin
Blame: BashOneLiners
EditPageHistoryDiffInfoLikePages
Annotated edit history of BashOneLiners version 26, including all changes. View license author blame.
Rev Author # Line
19 AristotlePagaltzis 1 !!! Sort a directory of [MP3]s into directories like <tt>Artist/Album Name/</tt>
17 AristotlePagaltzis 2
3 <verbatim>
4 for file in *.mp3; do
5 eval $( id3tool "$file" | sed 's/^[^:]* //; s/:[\t ]*/="/; s/[[:cntrl:]]//g; /./!d; s/ *$/"/' )
6 { [ "$Artist" -o "$Album" ] && dir="$Artist/$Album/" ; } || { [ "$Artist" ] && dir="$Artist/" ; } || continue
7 mkdir -p "$dir" ; mv "$file" "$dir/"
8 done
9 </verbatim>
10
11 It uses [id3tool | http://nekohako.xware.cx/id3tool/] to read the [ID3] tag, instructs [SED] to massage the information into something that looks like [Shell] script, executes that to put values into variables, checks which variables are set, and if at least an artist name (optionally also album name) is given, moves the file into that directory. (It's rather a mouthful for a oneliner, but hey.)
12
13 ----
14
19 AristotlePagaltzis 15 !!! Refresh the all [GPG] keys from the KeyServer without flooding it
17 AristotlePagaltzis 16
17 <verbatim>
18 for uid in $( gpg --with-colons --list-keys | grep ^pub | awk -F: '{print $5}' ) ; do gpg --refresh $uid ; sleep 5 ; done
19 </verbatim>
20
21 Alter the number after sleep(1) to change the speed of the refresh. This is equalivent to the slightly more understandable:
22
23 <verbatim>
24 for uid in $( gpg --with-colons --list-keys | grep ^pub | awk -F: '{print $5}' ) ; do
25 gpg --refresh $uid
26 sleep 5
27 done
28 </verbatim>
29
30 __Notes by AristotlePagaltzis:__
31
32 This is a useless use of grep(1). Better written as follows:
33
34 <verbatim>
35 for uid in $( gpg --with-colons --list-keys | awk -F: '/^pub/{ print $5 }' ) ; do gpg --refresh $uid ; sleep 5 ; done
36 </verbatim>
37
38 Correcting self: this is still a useless use of backticks. As with most <tt>for f in $( ... )</tt> loops it is better written with a <tt>while</tt> loop:
39
40 <verbatim>
41 gpg --with-colons --list-keys | awk -F: '/^pub/{ print $5 }' | while read uid ; do gpg --refresh $uid ; sleep 5 ; done
42 </verbatim>
43
44 ''This applies to seq(1)-based loops too -- <tt>seq 1 10 | while read i</tt> is better than <tt>for i in $( seq 1 10 )</tt>.''
45
46 This could be abbreviated as
47
48 <verbatim>
49 gpg --with-colons --list-keys | awk -F: '/^pub/{ print $5 }' | xargs -ri sh -c 'gpg --refresh {} ; sleep 5'
50 </verbatim>
51
52 but since xargs(1) doesn't offer any speed controls itself, it requires an ugly roundabout via [Shell].
53
54 ----
55
56 !!! Getting a random string
57
58 From the ChoosingPasswords page (see that page for an explanation):
59
60 <verbatim>
61 tr -dc ' -~' < /dev/urandom | head -c 20
24 AlastairPorter 62 </verbatim>
63
64 ----
65
66 !!! Getting a random number
67 There is a built in function called <tt>$RANDOM</tt>
68
69 <verbatim>
70 echo $(($RANDOM % 100))
17 AristotlePagaltzis 71 </verbatim>
20 MattBrown 72
73 ----
74
75 !!! Extracting a stanza from a file
76
77 <verbatim>
78 awk '/^iface bar/,/^$/ {print}'
79 </verbatim>
80
81 This prints everything from the line starting with 'iface bar' to the next blank line to stdout.
21 AlastairPorter 82
83 ----
84
85 !!! Counting disk usage from find output
86
87 Thanks to [#wlug], you can choose from a multitude of options
88
89 <verbatim>
90 find /foo | xargs du -s
91 find /foo -print0 | xargs -0 du -sh
92 find /foo | xargs ls -l | awk '{sum += $5} END {print sum}'
93 find /foo -printf '%s\n' | awk '{sum+=$1} END {print sum}'
94 find /foo -printf '%k\n' | awk '{sum+=$1} END {print sum,"kbytes"}'
95 </verbatim>
22 BenStaz 96
23 BenStaz 97 ----
22 BenStaz 98
99 !!! Find a random debian package that is not installed.
100 <verbatim>
101 aptitude search ~g | while read ; do echo "$RANDOM $REPLY" ; done | sort -n | head -1 | cut -d' ' -f2-
102 </verbatim>
103 —AristotlePagaltzis
25 DanielLawson 104
105 ----
106
107 !!! Rename a series of files using a regular expressions
108
26 DanielLawson 109 There's a number of ways of doing this, all with various problems. I finally settled on the following (Thanks [#wlug], and Isomer and Phil in particular):
25 DanielLawson 110
111 <verbatim>
112 find . -iname '*:*' -exec rename s/:/-/ '{}' ';'
113 </verbatim>
114
26 DanielLawson 115 This was needed in particular because the filenames had double spaces in them and trailing spaces, which ultimately defeated my rather rusty attempts at using bash. I'm replacing ':' with '-' because Macs allow you to create a file or a directory with a '/' in the name, but represent this on the filesystem as a ':'. This causes problems over CIFS shares however. -DanielLawson

PHP Warning

lib/blame.php:177: Warning: Invalid argument supplied for foreach()