Commands by mpb (44)

  • On Mageia (and Mandriva) Linux, this command will clear your existing urpmi configuration and configure it to use only network sources. It can be useful after you have installed from CD/DVD and don't want to continually be prompted to insert CD/DVD each time you install a package. acknowledgement: blino


    2
    urpmi.removemedia -a && urpmi.addmedia --distrib --mirrorlist
    mpb · 2011-03-27 15:29:42 6
  • For Linux distributions using rpm (eg Mandriva), this command will find the rpm package name that provides a file. Show Sample Output


    2
    rpm -q --whatprovides $filename
    mpb · 2011-02-09 23:28:15 2
  • "sort_csn" is a function to sort a comma separated list of numbers. Define the the function with this: sort_csn () { echo "${1}" | sed -e "s/,/\n/g"| sort -nu | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; } Use the function like this: sort_csn 443,22,80,8200,1533,21,1723,1352,25 21,22,25,80,443,1352,1533,1723,8200 One example where this is useful is when port scanning with nmap and getting a list of open ports in random order. If you use Nessus, you may need to create a scan policy for that set of specific ports and it is clearer to read with the port numbers in ascending order (left to right). Caveat: no spaces in the comma separated list (just number1,number2,number3,etc). A variation of this to sort a comma separated list of strings: sort_css () { echo "${1}" | sed -e "s/,/\n/g"| sort -u | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; } usage: sort_css apples,pears,grapes,melons,oranges apples,grapes,melons,oranges,pears Show Sample Output


    0
    sort_csn () { echo "${1}" | sed -e "s/,/\n/g"| sort -nu | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; }
    mpb · 2011-01-26 15:18:08 5
  • This command securely erases all the unused blocks on a partition. The unused blocks are the "free space" on the partition. Some of these blocks will contain data from previously deleted files. You might want to use this if you are given access to an old computer and you do not know its provenance. The command could be used while booted from a LiveCD to clear freespace space on old HD. On modern Linux LiveCDs, the "ntfs-3g" system provides ReadWrite access to NTFS partitions thus enabling this method to also be used on Wind'ohs drives. NB depending on the size of the partition, this command could take a while to complete. Show Sample Output


    8
    # cd $partition; dd if=/dev/zero of=ShredUnusedBlocks bs=512M; shred -vzu ShredUnusedBlocks
    mpb · 2009-06-21 14:17:22 12

  • 0
    shred -vzu /tmp/junk-file-to-be-shredded
    mpb · 2009-06-18 12:00:19 11
  • There are two ways to use "here documents" with bash to fill stdin: The following example shows use with the "bc" command. a) Using a delimiter at the end of data: less-than less-than eeooff bc > k=1024 > m=k*k > g=k*m > g > eeooff 1073741824 b) using the "inline" verion with three less-than symbols: less-than less-than less-than "k=1024; m=k*k; g=k*m; g" bc 1073741824 One nice advantage of using the triple less-than version is that the command can easily be recalled from command line history and re-executed. PS: in this "description", I had to use the name "less-than" to represent the less-than symbol because the commandlinefu input text box seems to eat up the real less-than symbols. Odd. Show Sample Output


    9
    <<<"k=1024; m=k*k; g=k*m; g" bc
    mpb · 2009-06-17 10:35:10 12
  • Update a Mandriva Linux system with any pending updates. This command needs to be run with root privilege. Using the "--force" option answers "yes" to any interactive prompts thus allowing the updates to be left unattended to completion. NB if there is an update for glibc and/or a new kernel then the system would need to be rebooted for these to take effect. A prerequisite for running "urpmi --auto-update" is to have correctly defined urpmi media sources (which can be done by visiting http://easyurpmi.zarb.org/). If there are no new updates the the message "Packages are up to date" is shown. Show Sample Output


    0
    urpmi --auto-update --force # apply all pending updates (Mandriva Linux)
    mpb · 2009-03-28 14:59:26 8
  • See: "man pwgen" for full details. Some Linux distros may not have pwgen included in the base distribution so you maye have to install it (eg in Mandriva Linux: "urpmi pwgen"). Show Sample Output


    6
    pwgen
    mpb · 2009-03-28 11:43:21 5
  • The vi key sequence !}command will send the file contents from the cursor to the next blank line as STDOUT to the command specified and replace that sequence of file lines with the output of the command. For example: sorting a block of data - !}sort The sequence !{command will do the same but "upwards" (from the current position towards the start of the file. Show Sample Output


    12
    !}sort
    mpb · 2009-03-28 00:18:39 11
  • Some commands (such as netcat) have a port option but how can you know which ports are unused? Show Sample Output


    4
    netstat -atn | awk ' /tcp/ {printf("%s\n",substr($4,index($4,":")+1,length($4) )) }' | sed -e "s/://g" | sort -rnu | awk '{array [$1] = $1} END {i=32768; again=1; while (again == 1) {if (array[i] == i) {i=i+1} else {print i; again=0}}}'
    mpb · 2009-03-27 20:38:43 8
  • Mandriva Linux includes a security tool called "msec" (configurable via "draksec"). One of the many things it regularily checks for is world writeable files. If any are found, it writes the list to /var/log/security/writable.today. "wc -l" simply counts the number of lines in the file. This number should be low. Browse through /var/log/security/writable.today and consider if any of those files *need* to be world-writeable (and if not, modify the permissions. eg: "chmod o-w $file"). A large number of world-writeable files may indicate that umask is not correctly set in /etc/profile (or ${HOME}/.bash_profile) but could also indicate poor security configuration or even malicious activity. Show Sample Output


    0
    # wc -l /var/log/security/writable.today
    mpb · 2009-03-19 12:25:52 7
  • This command shows if there are any locked AFS volumes. The output is a list of AFS volume IDs (or nothing if there are none locked). Show Sample Output


    0
    vos listvldb | agrep LOCKED -d RWrite | grep RWrite: | awk -F: '{print $2}' | awk '{printf("%s ",$1)} END {printf("\n")}'
    mpb · 2009-03-17 19:55:39 7
  • Find out which RPMs were installed on a particular date. These would (naturally) include update RPMs. This example shows searching for "Thu 05 Mar" (with grep). Alternatively, pipe it to less so you can search inside less (with less's neat text highlighting of the search term): rpm -qa --queryformat '%{installtime} \"%{vendor}\" %{name}-%{version}-%{release} %{installtime:date}\n' | less # (this example) search term: Thu 05 Mar Show Sample Output


    4
    rpm -qa --queryformat '%{installtime} \"%{vendor}\" %{name}-%{version}-%{release} %{installtime:date}\n' | grep "Thu 05 Mar"
    mpb · 2009-03-17 13:38:20 12
  • "play" is part of "SoX" SoX - Sound eXchange, the Swiss Army knife of audio manipulation. For details, see: man sox Show Sample Output


    -3
    play $audio_file
    mpb · 2009-03-17 11:30:02 10
  • At the start of a vi session and *before* saving any changes use ":!cp % %-" to make a backup of the current file being edited. example: vi /data/some/long/path/file :!cp% %- creates /data/some/long/path/file-


    5
    :!cp % %-
    mpb · 2009-03-17 00:34:24 18
  • #

    Using the "#" in shell is surprisingly useful. Some of the uses I found: a) As a visible copy buffer in shell history (caveat: do not use for passwords :-) b) To build complex commands until ready then hit the HOME, DEL, ENTER keys to run it c) Placing reference data into shell history (search for tags with CTRL-R TAGNAME) d) Putting aside a "work in progress" command to focus on another task (HOME # ENTER) Show Sample Output


    -4
    # indicates a comment in shell
    mpb · 2009-03-16 23:15:33 8
  • Low on disk space? Check the largest installed RPMs for delete canditates. Show Sample Output


    2
    rpm -qa --qf '%{SIZE} %{NAME}\n' | sort -nr | nl | head -6 # six largest RPMs
    mpb · 2009-03-15 22:18:17 9
  • This command converts filenames with embedded spaces in the current directory replacing spaces with the underscore ("_") character. Show Sample Output


    2
    ls -1 | grep " " | awk '{printf("mv \"%s\" ",$0); gsub(/ /,"_",$0); printf("%s\n",$0)}' | sh # rename filenames: spaces to "_"
    mpb · 2009-03-15 18:42:43 18
  • NB when you run this gcal command in your shell, holidays are highlighted but this highlighting does not show in the sample output (above). To find full details on gcal options: gcal --long-help | less Example for United States, Pennsylvania: gcal -K -q US_PA 2009 # display holidays in USA/Pennsylvania for 2009 (with week numbers) Example for Hong Kong: gcal -K -q HK 2009 # display holidays in Hong Kong for 2009 (with week numbers) Show Sample Output


    5
    gcal -K -q GB_EN 2009 # display holidays in UK/England for 2009 (with week numbers)
    mpb · 2009-03-15 10:19:52 7
  •  < 1 2

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

tail -f a log file over ssh into growl

dump database from postgresql to a file

Lists all usernames in alphabetical order

Keep a copy of the raw Youtube FLV,MP4,etc stored in /tmp/
Certain Flash video players (e.g. Youtube) write their video streams to disk in /tmp/ , but the files are unlinked. i.e. the player creates the file and then immediately deletes the filename (unlinking files in this way makes it hard to find them, and/or ensures their cleanup if the browser or plugin should crash etc.) But as long as the flash plugin's process runs, a file descriptor remains in its /proc/ hierarchy, from which we (and the player) still have access to the file. The method above worked nicely for me when I had 50 tabs open with Youtube videos and didn't want to have to re-download them all with some tool.

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }

Change the homepage of Firefox
Pros: Works in all Windows computers, most updated and compatible command. Cons: 3 liner Replace fcisolutions.com with your site name.

Copy without overwriting

Fast, built-in pipe-based data sink
This is shorter and actually much faster than >/dev/null (see sample output for timings) Plus, it looks like a disappointed face emoticon.

bash screensaver off

return the latest kernel version from a Satellite / Spacewalk server software channel


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: