Commands by splante (8)

  • This is an alternative to #9131. ffmpeg didn't work on my .au files, though it did on the .wav ones. Also useful if you don't have ffmpeg but do have sox. Handily, sox already reports in seconds (decimal). Show Sample Output


    3
    get_duration() { durline=$(sox "$1" -n stat 2>&1|grep "Length (seconds):");echo ${durline#*\: }; }
    splante · 2011-09-02 15:22:43 5
  • Shorter, easier to remember version of cmd#7636 NTP is better, but there are situations where it can't be used. In those cases, you can do this to sync the local time to a server. Show Sample Output


    39
    date --set="$(ssh user@server date)"
    splante · 2011-08-30 20:03:06 62
  • This is an alternative to another command using two xargs. If it's a command you know there's only one of, you can just use: ls -l /proc/$(pgrep COMMAND)/cwd Show Sample Output


    3
    eval ls -l /proc/{$(pgrep -d, COMMAND)}/cwd
    splante · 2011-04-14 13:41:58 7
  • This is regarding the command 8263 using an alias to fill in command line options for psql. You can actually just type 'psql'. In order for that to work, you want to set environment variables PGDATABASE, PGHOST, PGUSER, and (except you're using the default) PGPORT. Also, you can add a line "host:port:dbname:user:password" (asterisk ok in some columns) to your ~/.pgpass file. Finally, if you don't like the aligned columns, you can add the line "\pset format unaligned" to your ~/.psqlrc file.


    2
    psql
    splante · 2011-04-08 18:35:20 5
  • Admittedly, I'd never have thought of this without the earlier examples, but here's one that you can execute from your workstation to just display the image from another, without separately doing a file transfer, etc. By the way, I hear a loud beep coming from the other room, so I guess it's not too stealthy :-D


    12
    ssh user@remote-host "DISPLAY=:0.0 import -window root -format png -"|display -format png -
    splante · 2011-04-08 01:07:41 5
  • The "type" builtin command is handy to find out what executable will be used if you issue a command. But on some distros, particularly when using /etc/alternatives, certain executables get buried under layers and layers of symbolic links and it becomes hard to find which one. If you put the above command in your .bashrc, it adds a "-c" option to the type command that will weed through the symbolic links and prints the actual file that will be executed. Show Sample Output


    0
    type () { if [ "$1" = "-c" ]; then shift; for f in "$@"; do ff=$(builtin type -p "$f"); readlink -f "$ff"; done; else builtin type $typeopts "$@"; fi; }
    splante · 2011-04-07 18:57:51 5
  • Credit goes to brun65i but he posted it as a comment instead as an alternative. I hadn't noticed the -h option on sort before and this seems like the cleanest alternative. Thanks Brun65i! Show Sample Output


    0
    du -h --max-depth=1 | sort -hr
    splante · 2011-04-07 18:01:18 3
  • ksh's version of cd has an optional syntax where you can type "cd old new" and it will replace "old" with "new" in your current directory and take you there. This is very handy when you have a parallel directory structure, like source and object directories. As suggested, you can just type cd ${PWD/old/new} to get this in bash, but this function in your .bashrc will let you type the ksh cd syntax and avoid typing the special characters while preserving other cd functionality. Show Sample Output


    0
    cd () { cdop=""; while [ "$1" != "${1#-}" ]; do cdop="${cdop} ${1}"; shift; done; if [ $# -eq 2 ]; then newdir="${PWD/$1/$2}"; [ -d "${newdir}" ] || { echo "no ${newdir}"; return 1; }; builtin cd $cdop "${newdir}"; else builtin cd $cdop "$@"; fi }
    splante · 2011-04-07 14:36:26 3

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

Print all the lines between 10 and 20 of a file
Subtly different to the -n+p method... and probably wrong in so many ways....... But it's shorter. Just.

Get your current Public IP

Create a file server, listening in port 7000
At client side: tar c myfile | nc localhost 7000 ##Send file myfile to server tar c mydir | nc localhost 7000 ## Send directory mydir to server

Find non-ASCII and UTF-8 files in the current directory

Poor man's ntpdate
If you don't have netcat, you can use curl.

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

du with colored bar graph
i'm using gawk, you may get varying mileage with other varieties. You might want to change the / after du to say, /home/ or /var or something, otherwise this command might take quite some time to complete. Sorry it's so obsfucated, I had to turn a script into a one-liner under 255 characters for commandlinefu. Note: the bar ratio is relative, so the highest ratio of the total disk, "anchors" the rest of the graph. EDIT: the math was slightly wrong, fixed it. Also, made it compliant with older versions of df.

Generate soothing noise
Substitute 'brown' with 'pink' or 'white' according to your taste. I put this on my headphones when I'm working in an "open concept" office, where there are always three to five conversations going in earshot, or if I'm working somewhere it is "rude" of me to tell a person to turn off their cubicle radio.

Create a large test file (taking no space).

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"


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: