Commands by atoponce (55)

  • Looks best in an 80x24 256-color terminal emulator.


    12
    while true; do printf "\e[38;5;$(($(od -d -N 2 -A n /dev/urandom)%$(tput colors)))m.\e[0m"; done
    atoponce · 2015-11-24 15:21:27 2
  • This command is similar to the alternate, except with head(1), you can pick as many passwords as you wish to generate by changing the number of lines you wish to preview. Show Sample Output


    2
    strings /dev/urandom | tr -cd '[:alnum:]' | fold -w 30 | head -n 1
    atoponce · 2014-12-11 06:21:51 0
  • The "proportional set size" is probably the closest representation of how much active memory a process is using in the Linux virtual memory stack. This number should also closely represent the %mem found in ps(1), htop(1), and other utilities. Show Sample Output


    5
    echo 0$(awk '/Pss/ {printf "+"$2}' /proc/$PID/smaps)|bc
    atoponce · 2013-09-26 18:20:22 0
  • Why use many different utilities all piped together, when you only need two? Show Sample Output


    1
    ip -o -4 addr show | awk -F '[ /]+' '/global/ {print $4}'
    atoponce · 2013-04-09 16:34:39 3
  • With the "--resolve" switch, you can avoid doing DNS lookups or edit the /etc/hosts file, by providing the IP address for a domain directly. Useful if you have many servers with different IP addresses behind a load balancer. Of course, you would loop it: for IP in 10.11.0.{1..10}; do curl --resolve subdomain.example.com:80:$IP subdomain.example.com -I -s; done


    3
    curl --resolve subdomain.example.com:80:10.100.0.1 subdomain.example.com -I -s
    atoponce · 2013-01-24 19:50:26 0
  • This relies on a public API from http://longurl.org. So, this has the weakness that if the service disappears, the function will break. However, it has the advantage that the shortened URL service will not be tracking your IP address and other metrics, but instead will track longurl.org. Thus, you can remain anonymous from the shortened URL services (although not anonymous from longurl.org). It does no sanity checking that you have provided an argument. If you do not provide one, "message" is displayed to STDOUT. Show Sample Output


    2
    expandurl() { curl -s "http://api.longurl.org/v2/expand?url=${1}&format=php" | awk -F '"' '{print $4}' }
    atoponce · 2013-01-19 10:40:46 0
  • /dev/urandom is cryptographically secure, and indistinguishable from true random, as it gathers data from external sources, influenced by human timing interactions with computers, to fill the entropy pool, and hashes the input with SHA-1. As such, this is a quick way to do a "true random" fair-6 dice roll. Using this method, you could easily create passphrases with Diceware http://diceware.com. Change the head(1) count to something other than 5 for more or less numbers.


    9
    tr -cd '1-6' < /dev/urandom | head -c 1; echo
    atoponce · 2012-09-21 02:16:42 3
  • Quick shortcut if you know the hostname and want to save yourself one step for looking up the IP address separately.


    2
    ssh-keygen -R $(dig +short host.domain.tld)
    atoponce · 2012-01-19 15:08:50 0
  • Save some CPU, and some PIDs. :)


    5
    awk -F ':' '{print $1 | "sort";}' /etc/passwd
    atoponce · 2011-12-20 12:46:52 0
  • You could do the following, however, brace expansion with {} is not defined in POSIX, and therefore not guaranteed to work in all shells. But, if it does, it's more convenient (although it's certainly not less typing): cp -r {*,.??*} /dest Sometimes there are times when I need to cp(1), mv(1) or rm(1) files recursively, but don't want to traverse the previous directory by following ../../../../ etc out of the current directory. This command prevents that. The secret sauce is ".??*". The file globbing ensures that it must start with a dot, and be followed by at least two characters. So, three characters must exist in the filename, which eliminates "." and "..".


    6
    cp -r * .??* /dest
    atoponce · 2011-12-16 23:41:03 6
  • The command uses ssh(1) to get to a remote host, uses tar(1) to archive a remote directory, prints the result to STDOUT, which is piped to gzip(1) to compress to a local file. In other words, we are archiving and compressing a remote directory to our local box.


    7
    ssh user@host "tar -cf - /path/to/dir" | gzip > dir.tar.gz
    atoponce · 2011-12-14 15:54:57 7
  • This command uses awk(1) to print all lines between two known line numbers in a file. Useful for seeing output in a log file, where the line numbers are known. The above command will print all lines between, and including, lines 3 and 6.


    7
    awk 'NR >= 3 && NR <= 6' /path/to/file
    atoponce · 2011-12-14 14:28:56 3
  • You can use "decode()" in a similar manner: python -c 'print "68656c6c6f".decode("hex")' Show Sample Output


    2
    python -c 'print "hello".encode("hex")'
    atoponce · 2011-12-13 23:05:17 0
  • Python is always such much more readable than most shell scripting. Show Sample Output


    0
    python -c 'print hex(1337)'
    atoponce · 2011-12-13 22:03:10 0
  • Git uses secure hash sums for its revision numbers. I'm sure this is fine and dandy for ultra-secure computing, but it's less than optimal for humans. Thus, this will give you sequential revision numbers in Git all the way from the first commit.


    -1
    git rev-list --reverse HEAD | awk "/$(git log -n 1 --pretty="format:%h")/ {print NR}"
    atoponce · 2011-11-15 21:49:32 0
  • curl(1) is more portable than wget(1) across Unices, so here is an alternative doing the same thing with greater portability. This shell function uses curl(1) to show what site a shortened URL is pointing to, even if there are many nested shortened URLs. This is a great way to test whether or not the shortened URL is sending you to a malicious site, or somewhere nasty that you don't want to visit. The sample output is from: expandurl http://t.co/LDWqmtDM Show Sample Output


    5
    expandurl() { curl -sIL $1 | grep ^Location; }
    atoponce · 2011-10-19 00:56:53 1
  • This shell function uses wget(1) to show what site a shortened URL is pointing to, even if there are many nested shortened URLs. This is a great way to test whether or not the shortened URL is sending you to a malicious site, or somewhere nasty that you don't want to visit. The sample output is from: expandurl http://t.co/LDWqmtDM Show Sample Output


    0
    expandurl() { wget -S $1 2>&1 | grep ^Location; }
    atoponce · 2011-10-18 18:50:54 7
  • Yes, rsync(1) supports local directories. And, should anything change, it's trivial to run the command again, and grab only the changes, instead of the full directory.


    8
    rsync -a /etc /destination
    atoponce · 2011-10-18 13:07:55 1
  • A common programming question for interviewers to ask potential job candidates is to code "FizzBuzz". That is, if a number is divisible by 3, then it should display "Fizz". If a number is divisible by 5, it should display "Buzz". If it is divisible by both, then it should display "FizzBuzz". Otherwise, display the current number between 1 and 100. Show Sample Output


    5
    python -c'for i in range(1,101):print"FizzBuzz"[i*i%3*4:8--i**4%5]or i'
    atoponce · 2011-10-12 21:15:35 4
  • Quick and dirty one-liner to get the average ping(1) time from a server. Show Sample Output


    -4
    ping -qc 10 server.tld | awk -F/ '/^rtt/ {print $5}'
    atoponce · 2011-10-12 21:07:06 2
  • it is generally advised to avoid using which(1) whenever possible. which(1) is usually a csh(1) script, or sometimes a compiled binary. It's output is highly variable from operating system to operating system, so platform independent scripts could become quite complicated with the logic. On HP-UX 10.20, for example, it prints "no bash in /path /path /path ..."; on OpenBSD 4.1, it prints "bash: Command not found."; on Debian (3.1 through 5.0 at least) and SuSE, it prints nothing at all; on Red Hat 5.2, it prints "which: no bash in (/path:/path:...)"; on Red Hat 6.2, it writes the same message, but on standard error instead of standard output; and on Gentoo, it writes something on stderr. And given all these differences, it's still variable based on your shell. This is why POSIX is king. See http://mywiki.wooledge.org/BashFAQ/081 for more ways on avoiding which(1). Show Sample Output


    4
    command -v bash
    atoponce · 2011-09-26 10:17:41 4
  • As odd as this may be, I know of servers where the man(1) command is not installed, and there is not enough room on / to install it. However, zcat(1), nroff(1) and less(1) are. This is a way to read those documents without the proper tool to do so, as sad as this may seem. :)


    0
    zcat /usr/share/man/man1/man.1.gz | nroff -man | less
    atoponce · 2011-09-07 01:13:57 0
  • According to the gpg(1) manual: --gen-random 0|1|2 count Emit count random bytes of the given quality level 0, 1 or 2. If count is not given or zero, an endless sequence of random bytes will be emitted. If used with --armor the output will be base64 encoded. PLEASE, don't use this command unless you know what you are doing; it may remove precious entropy from the system! If your entropy pool is critical for various operations on your system, then using this command is not recommended to generate a secure password. With that said, regenerating entropy is as simple as: du -s / This is a quick way to generate a strong, base64 encoded, secure password of arbitrary length, using your entropy pool (example above shows a 30-character long password). Show Sample Output


    10
    gpg --gen-random --armor 1 30
    atoponce · 2011-07-20 15:32:49 2
  • Will return the SSH server key information for each host you have in your ~/.ssh/known_hosts file, including key size, key fingerprint, key IP address or domain name, and key type. Show Sample Output


    11
    ssh-keygen -l -f ~/.ssh/known_hosts
    atoponce · 2010-12-05 04:03:07 1
  • This is assuming that you're editing some file that has not been wrapped at 80 columns, and you want it to be wrapped. While in Vim, enter ex mode, and set the textwidth to 80 columns: :set textwidth=80 Then, press: gg to get to the top of the file, and: gqG to wrap every line from the top to the bottom of the file at 80 characters. Of course, this will lose any indentation blocks you've setup if typing up some source code, or doing type setting. You can make modifications to this command as needed, as 'gq' is the formatting command you want, then you could send the formatting to a specific line in the file, rather than to the end of the file. gq49G Will apply the format from your current cursor location to the 49th row. And so on.


    2
    gqG
    atoponce · 2010-11-08 04:05:24 0
  •  1 2 3 > 

Considering A New Car?

You should consider a Tesla. Get 6 Months Free Supercharging.

Free Supercharging

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

Analyze awk fields
Breaks down and numbers each line and it's fields. This is really useful when you are going to parse something with awk but aren't sure exactly where to start.

Run bash on top of a vi session (saved or not saved), run multiple commands, instead of one at a time with :!(bashcommand), type exit and [enter] to get back to where you left off in vi.
Helps when I'm editing a script and want to double check some commands without having to exit out of vi multiple times or having to use another terminal session.

Create a new file

Create a bash script from last commands
In order to write bash-scripts, I often do the task manually to see how it works. I type ### at the start of my session. The function fetches the commands from the last occurrence of '###', excluding the function call. You could prefix this with a here-document to have a proper script-header. Delete some lines, add a few variables and a loop, and you're ready to go. This function could probably be much shorter...

Compare two directories
Output of this command is the difference of recursive file lists in two directories (very quick!). To view differences in content of files too, use the command submitted by mariusbutuc (very slow!): $ diff -rq path_to_dir1 path_to_dir2

Geolocate a given IP address
GeoIP Needs to be installed. Can be done from some distro's or via MaxMind.com for free. There even is a free city database availabble. If the GeoLiteCity is downloaded and installed it will also find more information $ geoiplookup -f /var/lib/GeoIP/GeoLiteCity.dat commandlinefu.com GeoIP City Edition, Rev 1: US, NJ, Absecon, 08201, 39.420898, -74.497704, 504, 609

Get the 10 biggest files/folders for the current direcotry
This command simply outputs 10 files in human readable, that takes most space on your disk in current directory.

Number of connections to domains on a cPanel server

Oneliner to run commands on multiple servers
Oneliner to run commands on multiple servers over ssh. - First parameter "$1" is the command you want to execute remotely. ( It can be multiple commands e.g. "hostname;uptime") - Second parameter "${@:2}" represents the remote host/s you want to run the command/s on.

Remove space and/or tab characters at the end of line
The command removes all space and/or tabulation characters preceding new line


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: