Commands by dopeman (11)

  • The command (above) will remove any duplicate rows based on the FIRST column of data in an un-sorted file. The '$1' represents a positional parameter. You can change both instances of '$1' in the command to remove duplicates based on a different column, for instance, the third: awk '{ if ($3 in stored_lines) x=1; else print; stored_lines[$3]=1 }' infile.txt > outfile.txt Or you can change it to '$0' to base the removal on the whole row: awk '{ if ($0 in stored_lines) x=1; else print; stored_lines[$0]=1 }' infile.txt > outfile.txt ** Note: I wouldn't use this on a MASSIVE file, unless you're RAM-rich ;) **

    awk '{ if ($1 in stored_lines) x=1; else print; stored_lines[$1]=1 }' infile.txt > outfile.txt
    dopeman · 2010-12-15 17:08:47 0
  • buf myfile.txt This is useful when you are making small but frequent changes to a file. It keeps things organised and clear for another administrator to see what changed and at what time. An overview of changes can be deduced using a simple: ls -ltr

    buf () { filename=$1; filetime=$(date +%Y%m%d_%H%M%S); cp ${filename} ${filename}_${filetime}; }
    dopeman · 2010-12-14 13:19:52 1
  • Essentially the same as funky's alias, but will not traverse filesystems and has nicer formatting. Show Sample Output

    alias dush="du -xsm * | sort -n | awk '{ printf(\"%4s MB ./\",\$1) ; for (i=1;i<=NF;i++) { if (i>1) printf(\"%s \",\$i) } ; printf(\"\n\") }' | tail"
    dopeman · 2010-07-15 10:38:27 1
  • I have come across a situation in the past where someone has unlinked a file by running an 'rm' command against it while it was still being written to by a running process. The problem manifested itself when a 'df' command showed a filesystem at 100%, but this did not match the total value of a 'du -sk *'. When this happens, the process continues to write to the file but you can no longer see the file on the filesystem. Stopping and starting the process will, more often than not, get rid of the unlinked file, however this is not always possible on a live server. When you are in this situation you can use the 'lsof' command above to get the PID of the process that owns the file (in the sample output this is 23521). Run the following command to see a sym-link to the file (marked as deleted): cd /proc/23521/fd && ls -l Truncate the sym-link to regain your disk space: > /proc/23521/fd/3 I should point out that this is pretty brutal and *could* potentially destabilise your system depending on what process the file belongs to that you are truncating. Show Sample Output

    lsof +L1
    dopeman · 2010-07-14 17:21:01 2
  • This is a handy way to circumvent the "Maximum line length of 2048 exceeded" grep error. Once you have run the above command (or put it in your .bashrc), files can be searched using: lgrep search-string /file/to/search

    lgrep() { string=$1; file=$2; awk -v String=${string} '$0 ~ String' ${file}; }
    dopeman · 2010-01-19 09:42:19 0
  • This does the same thing as many of the 'grep' based alternatives but allows a more finite control over the output. For example if you only wanted the process ID you could change the command: ps -ef | awk '/mingetty/ && !/awk/ {print $2}' If you wanted to kill the returned PID's: ps -ef | awk '/mingetty/ && !/awk/ {print $2}' | xargs -i kill {} Show Sample Output

    ps -ef | awk '/process-name/ && !/awk/ {print}'
    dopeman · 2009-08-19 11:22:09 0
  • This command will copy files and directories from a remote machine to the local one. Ensure you are in the local directory you want to populate with the remote files before running the command. To copy a directory and it's contents, you could: ssh [email protected] "(cd /path/to/a/directory ; tar cvf - ./targetdir)" | tar xvf - This is especially useful on *nix'es that don't have 'scp' installed by default.

    ssh [email protected] "(cd /path/to/remote/top/dir ; tar cvf - ./*)" | tar xvf -
    dopeman · 2009-03-31 13:08:45 3
  • This command will show the 20 processes using the most CPU time (hungriest at the bottom). You can see the 20 most memory intensive processes (hungriest at the bottom) by running: ps aux | sort +3n | tail -20 Or, run both: echo "CPU:" && ps aux | sort +2n | tail -20 && echo "Memory:" && ps aux | sort +3n | tail -20

    ps aux | sort +2n | tail -20
    dopeman · 2009-03-31 12:03:34 1
  • This command will tell you the 20 biggest directories starting from your working directory and skips directories on other filesystems. Useful for resolving disk space issues.

    du -xk | sort -n | tail -20
    dopeman · 2009-03-30 11:37:43 1
  • This command will replace all instances of 'foo' with 'bar' in all files in the current working directory and any sub-directories.

    perl -pi -e 's/foo/bar/g' $(grep -rl foo ./*)
    dopeman · 2009-03-27 17:21:35 2
  • This command will replace all instances of 'foo' with 'bar' in all files in the current working directory.

    perl -pi -e 's/foo/bar/g' $(grep -l foo ./*)
    dopeman · 2009-03-27 17:18:08 1

What's this? 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

get stdout to variable and stdout at sametime
Sometimes you want to write the script output to stdout but you need to send it to email. If you use: $ var="$( ls / )"; $ echo -e "$var"; works but, you need to wait the script terminate to bufferize then print the output var; With this way, you can use/work/print the output before the variable receive all the output content, then after it you can use the variable for anything else, like send email.

List and delete files older than one year

Simplest port scanner
Very handy way to perform a host scan if you don't have nmap,ncat,nc ...or other tools installed locally. When executing a command on a /dev/tcp/$host/$port pseudo-device file, Bash opens a TCP connection to the associated socket and UDP connection when using /dev/udp/$host/$port.A simlpe way to get servers banner is to run this command "cat < /dev/tcp/localhost/25" , here you will get mail server's banner. NOTE: Bash, as packaged for Debian, does not support using the /dev/tcp and /dev/udp pseudo-device it's not enabled by default Because bash in Debian is compiled with ?disable-net-redirections.

convert wav files to ogg
cd to the folder containing the wav files and convert them all to ogg format. in my sample output i use the -a and -l flags to set the author and album title. to get the oggenc program in ubuntu linux run: sudo apt-get install oggenc

Delete leading whitespace from the start of each line

Get the IP address

Disable sleep mode via cli and systemd (Centos, Debian Ubuntu?)
This causes computer to never power off or go into sleep mode. Disables sleep mode linux.

OpenDns IP update via curl
Your IP is resolved by OpenDns Server (like a caller ID telephone, every server knows who is calling ;-) Change user:password by yours Be Happy

Stream YouTube URL directly to mplayer.
Streams youtube video with v=ID directly into the mplayer. If exists, it uses the HD-quality stream. If you don't want to watch it in HD-quality, you can use the shorter form: $ID=52DnUo6wJto; mplayer -fs $(echo "$ID$(wget -qO - ''$ID | perl -ne 'print $1."&asv=" if /^.*(&t=.*?)&.*$/')")

bash shortcut: !$ !^ !* !:3 !:h and !:t
When expanding, bash output the command, so don't be affraid if you type the command. Here is the details: First examples: $echo foo bar foobar barfoo First argument: $echo !$ echo barfoo barfoo (Note that typing echo foo bar foobar barfoo && echo !$, bash substitute !$ with $:1) Last argument: $echo foo bar foobar barfoo && echo !^ echo foo bar foobar barfoo && echo barfoo foo bar foobar barfoo barfoo All the arguments: $echo !* echo foo bar foobar barfoo foo bar foobar barfoo The third argument: $echo foo bar foobar barfoo && echo !:3 echo foo bar foobar barfoo && echo foobar foo bar foobar barfoo foobar You may want to add {} for large numbers: echo !:{11} for example Now with path: $echo /usr/bin/foobar /usr/bin/foobar For the head: $echo !$:h echo /usr/bin /usr/bin And the tail: $echo !$:t echo foobar foobar You also may want to try !:h and !:t or !!3-4 for the third and the fourth (so !!:* == !!:1-$)

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.


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: