Commands using perl (369)

  • This is from perldoc -q random.*line, which says: This has a significant advantage in space over reading the whole file in. You can find a proof of this method in The Art of Computer Programming, Volume 2, Section 3.4.2, by Donald E. Knuth. Who am I to argue with Don Knuth?


    1
    perl -e 'rand($.) < 1 && ($line = $_) while <>;'
    unixmonkey20842 · 2011-04-25 21:28:26 3
  • Can also just use the debug mode like this. Show Sample Output


    1
    perl -dwe 1
    bashrc · 2011-05-05 20:28:03 6
  • Replace PACKAGE with desired package name. Found here: http://mikebeach.org/2011/04/undo-apt-get-build-dep/


    1
    sudo aptitude markauto $(apt-cache showsrc PACKAGE | grep Build-Depends | perl -p -e 's/(?:[\[(].+?[\])]|Build-Depends:|,|\|)//g')
    Lexton · 2011-05-08 14:18:52 6
  • Recursively delete empty directories. Use with care.


    1
    perl -MFile::Find -e"finddepth(sub{rmdir},'.')"
    igorfu · 2011-05-23 08:45:34 7

  • 1
    arp-scan -I eth0 -l | perl -ne '/((\d{1,3}\.){3}\d{1,3})/ and $ip=$1 and $_=`nmblookup -A $ip` and /([[:alnum:]-]+)\s+<00>[^<]+<ACTIVE>/m and printf "%15s %s\n",$ip,$1'
    bandie91 · 2011-07-08 07:41:41 3

  • 1
    perl -mText::Highlight -E 'say Text::Highlight->new(ansi => 1)->highlight(Perl => do { local (@ARGV,$/) = shift; <> }) ' path/to/perl-file.pl
    dbr · 2011-07-12 04:55:12 5
  • this command example converts to 25 fps subtitles that were originally created for 24 fps movie


    1
    cat subtitles.txt | perl -pe 's/} /}/g; s/{(\d+)}/=1=/; $f1=(24/25*$1); s/{(\d+)}/=2=/; $f2=(24/25*$1); $f1=~s/\..*//; $f2=~s/\..*//; s/=1=/{$f1}/; s/=2=/{$f2}/; ' > subtitles_newfps.txt
    ciekawy · 2011-07-30 15:34:43 18
  • Calls the POSIX strerror() function to look up the meaning of integer ERRNOs set by some functions.


    1
    perl -MPOSIX -e 'print strerror($ARGV[0])."\n";' ERRNO
    sethjust · 2011-09-21 18:16:19 5

  • 1
    perl -i -pe 'BEGIN{undef $/;} s/START.*?STOP/replace_string/smg' file_to_change
    hute37 · 2011-10-05 11:22:36 10
  • the output of svn log is annoying to grep, since it spreads the useful info over multiple lines. This compacts the output down to one line so eg you can grep for a comment and see the rev, date & committer straight away. Updated: MUCH shorter, easier to remember. Now it just replaces newlines with spaces, except on '---' lines. Show Sample Output


    1
    svn log | perl -l40pe 's/^-+/\n/'
    bazzargh · 2011-10-14 16:02:22 6

  • 1
    perl -ane 'END{printf(" %d %d %d\n", $x, $y, $z)} $x+=1; $y+=@F; $z+=length' file.txt
    kev · 2011-10-24 06:14:02 10
  • The crypt function takes a password, key, as a string, and a salt character array which is described below, and returns a printable ASCII string which starts with another salt. It is believed that, given the output of the function, the best way to find a key that will produce that output is to guess values of key until the original value of key is found. from http://en.wikipedia.org/wiki/Crypt_(Unix)


    1
    useradd -m -p $(perl -e'print crypt("passwordscelta", "stigghiola")') user
    0disse0 · 2012-02-06 19:53:01 7
  • This one line Perl script will display the smallest to the largest files sizes in all directories on a server. Show Sample Output


    1
    du -k | sort -n | perl -ne 'if ( /^(\d+)\s+(.*$)/){$l=log($1+.1);$m=int($l/log(1024)); printf ("%6.1f\t%s\t%25s %s\n",($1/(2**(10*$m))),(("K","M","G","T","P")[$m]),"*"x (1.5*$l),$2);}' | more
    Q_Element · 2012-02-07 15:49:19 10
  • Requires perl 5.14 or greater


    1
    perl -e 'rename $_, s/ /-/gr for <*.mp3>'
    ironcamel · 2012-02-21 17:07:18 4
  • This prints file access rights in octal - useful when "stat" is unavailable. Show Sample Output


    1
    perl -e 'printf "%04o\n", (stat shift)[2] & 0777;' file
    zlemini · 2012-03-22 15:05:04 9
  • extract data in multiline blocks of data with perl pattern matching loop Show Sample Output


    1
    cat z.log | perl -ne 'BEGIN{ print "DATE;RATE\n"; } /\[(\d.*)\]/ && print $1; /CURRENT RATE: +(\S+) msg.*/ && print ";" .$1 . "\n"; '
    hute37 · 2012-04-03 13:52:20 3

  • 1
    perl -e 'printf "00:16:3E:%02X:%02X:%02X\n", rand 0xFF, rand 0xFF, rand 0xFF'
    Gular · 2012-07-02 09:59:43 5
  • Really helpfull when play with files having spaces an other bad name. Easy to store and access names and path in just a field while saving it in a file. This format (URL) is directly supported by nautilus and firefox (and other browsers) Show Sample Output


    1
    convert_path2uri () { echo -n 'file://'; echo -n "$1" | perl -pe 's/([^a-zA-Z0-9_\/.])/sprintf("%%%.2x", ord($1))/eg' ;} #convert2uri '/tmp/a b' ### convert file path to URI
    totti · 2013-07-01 08:54:45 7
  • Sets the @ A record for your domain hosted by namecheap to your current internet-facing IP address, logs success or failure with syslog, and logs the data returned to /root/dnsupdate. Change the XXX's as appropriate. More info at: http://www.namecheap.com/support/knowledgebase/article.aspx/29/ Show Sample Output


    1
    logger -tdnsupdate $(curl -s 'https://dynamicdns.park-your-domain.com/update?host=@&domain=xxx&password=xxx'|tee -a /root/dnsupdate|perl -pe'/Count>(\d+)<\/Err/;$_=$1eq"0"?"Update Sucessful":"Update failed"'&&date>>/root/dnsupdate)
    MagisterQuis · 2013-08-11 16:27:39 7
  • Use it to send raw data to a networked device. Used to interact with relay controller board whose documentation is lost, so use wireshark to sniff the sent data and replayed using the command.


    1
    echo -n 023135 | perl -pe 's/([0-9a-f]{2})/chr hex $1/gie' | nc -4u -q1 -p5001 192.168.0.100 2000
    sucotronic · 2013-09-18 14:31:47 7
  • Converts control codes and spaces (ASCII code ≤ 32) to visible Unicode Control Pictures, U+2400 ? U+2420. Skips \n characters, which is probably a good thing. Show Sample Output


    1
    /bin/echo -e '\002Hello, Folks\t!\r' | perl -pwle 'use v5.14; s/([\N{U+0000}-\N{U+0020}])/chr(9216+ord($1))/ge;'
    scruss · 2014-06-30 01:45:40 11
  • An advantage is that this doesn't modify remained string at all. One can change {0,1} with {0,n} to drop several columns


    1
    perl -pE's/(\S+\s*){0,1}//'
    pung96 · 2015-05-09 15:14:58 12
  • Better than the others, and actually works unlike some of them. Show Sample Output


    1
    objdump -d $1 | grep -Po '\s\K[a-f0-9]{2}(?=\s)' | sed 's/^/\\x/g' | perl -pe 's/\r?\n//' | sed 's/$/\n/'
    Daytona · 2015-05-23 01:21:58 10

  • 1
    echo "quit" | openssl s_client -connect facebook.com:443 | openssl x509 -noout -text | grep "DNS:" | perl -pe "s/(, )?DNS:/\n/g"
    lgarron · 2015-08-13 22:50:45 9

  • 1
    find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u
    wuseman1 · 2018-11-11 01:09:42 530
  • ‹ First  < 5 6 7 8 9 >  Last ›

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

Replace spaces in filenames with underscores

Report bugs in Ubuntu
As of 10.04 LTS, you need to use this command-line to reports bugs to the launchpad.net tracking system (you need a launchpad acct for this to work). This command is preferred over using the website because it collects/sends info about your system to help developers. ubuntu-bug is a symlink to apport-bug which sees if KDE/Gnome is running and calls apport-gtk/apport-kde dialogs, otherwise apport-cli, so you can fill out a bug report. First run 'ubuntu-bug' without args to see a list of known symptoms. If there's no matching symptom, or you know which package is to blame, then run 'ubuntu-bug <package>'. If the process is still running, use 'ubuntu-bug <PID>'

Quickly get summary of sizes for files and folders
Use this as a quick and simple alternative to the slightly verbose "du -s --max-depth=1"

Get AWS temporary credentials ready to export based on a MFA virtual appliance
You might want to secure your AWS operations requiring to use a MFA token. But then to use API or tools, you need to pass credentials generated with a MFA token. This commands asks you for the MFA code and retrieves these credentials using AWS Cli. To print the exports, you can use: `awk '{ print "export AWS_ACCESS_KEY_ID=\"" $1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" $2 "\"\n" "export AWS_SESSION_TOKEN=\"" $3 "\"" }'` You must adapt the command line to include: * $MFA_IDis ARN of the virtual MFA or serial number of the physical one * TTL for the credentials

Viewing Top Processes according to cpu, mem, swap size, etc.
I've wanted this for a long time, finally just sat down and came up with it. This shows you the sorted output of ps in a pretty format perfect for cron or startup scripts. You can sort by changing the k -vsz to k -pmem for example to sort by memory instead. If you want a function, here's one from my http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html $ aa_top_ps(){ local T N=${1:-10};T=${2:-vsz}; ps wwo pid,user,group,vsize:8,size:8,sz:6,rss:6,pmem:7,pcpu:7,time:7,wchan,sched=,stat,flags,comm,args k -${T} -A|sed -u "/^ *PID/d;${N}q"; }

Batch rename extension of all files in a folder, in the example from .txt to .md
Same thing using bash built-in features instead of a sub-shell.

Create a nifty overview of the hardware in your computer
After the command is done, open the html file in a browser

Adding formatting to an xml document for easier reading
This will indent the input to be more readable. Warnings and messages are not send to STDOUT so you can just use a pipe to create the formatted outputfile, like: $ tidy -i -xml in.xml > out.xml

draw line separator (using knoppix5 idea)

Print a single route to a destination and its contents exactly as the kernel sees it
Useful to determine the source_ip of outgoing packages to a certain destination


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: