Monday, 17 June 2013

ome random, super-cool commands in Linux

  • A stopwatch on the terminal
    $ time read
    This will start a stopwatch on your terminal. Simply press Ctrl+D to stop the timer and see the elapsed time.
  • How to know how many CPU cores are there on the computer ?
    $ sudo cat /proc/cpuinfo | grep processor | wc -l
    Infact, you can see a lot of info about each of these cores with the command
    $ sudo cat /proc/cpuinfo
  • Finding the name of the linux distro running on the computer
    $ sudo cat /etc/issue
  • How to know whether you have a 32bit or 64bit OS running on your computer ?
    $ sudo getconf LONG_BIT
  • Killing a process that has locked a particular file, when you know the file name that is locked, but don’t know which process is locking it.
    $ sudo fuser -k <file_name>
    This is useful when you get an error saying this particular file is locked by another process. This happens many times when you are updating your linux installation and the process got terminated leaving the /var/lib/dpkg/cache file locked. You get an error when you try to re-start the system update again. You can use this command in such situations.
  • Easiest way to re-run the previous command with superuser permissions
    $ sudo !!
This saves you from pressing up arrow and then home key and then typing sudo.
  • Easily doing a reverse search for a command you entered previously. In bash, simply press Ctrl+R and then start typing the part of the comamnd you remember. Hit enter when you find the command you were looking for. This command gives rise to another neat trick. Suppose you use a lengthy command very frequently during the session. The first time you run the long command, run it as follows
    $ <command> #my_label
    The next time you want to run the same command, all you have to do is press Ctrl+R to start reverse-search and then enter “my_label” followed by enter key… How cool is that !!!.
  • How to get vi stlye editing commands working in bash
    $ echo "set editing-mode vi" > ~/.inputrc
  • some super useful bash command editing keys
    - Press Ctrl+W to erase a single word before the current cursor position.
    - Press Ctrl+U to erase the entire line before the current cursor position.
    - Press Ctrl+K to erase the entire line after the current cursor position.
    - Press Ctrl+A to go to the beginning of the command.
    - Press Ctrl+E to go the the end of the command.
  • How to display a popup notification when a command completes ( requires libnotify to be installed )
    $ wget <URL> ; notify-send "wget" "your download is complete"
    The above command displays a popup notification once wget finishes downloading the file. wget can be replaced by any command actually. The first argument to the notify-send command is the “title” and the second argument is the “body” of the popup notification. You can change it to whatever you like.
  • Turning of the monitor to save power when there is no hardware key available to do so(say, on a laptop)
    $ xset dpms force off
  • How to copy the output of any command directly to the system clipboard
    $ <command> | xsel --clipboard
  • How to open an file from the command line using the default application for that file
    $ xdg-open <file_name>
  • How to save the output of any command as an image file
    $ <command> | convert label:@- <image_name.png>
  • How to convert an entire man page into pdf format for later viewing
    $ man -t <command_name> | ps2pdf - <command_name.pdf>
  • Installing the same packages and software you already have on a fully configured linux system, on another freshly installed linux system in a single command
    First, run this command on the fully configured linux box
    $ sudo dpkg --get-selections > my_linux_software
    Then, transfer this file to the freshly installed linux box an enter the following command
    $ cat may_linux_software|sudo dpkg --set-selections && sudo dselect install
    Ofcourse, you will need network connection on your freshly installed linux box, but you will be saved from laboriously selecting all your favorite software from the software management tool.
  • Deleting a particular line number from a given file without opening it in any editor
    $ sed -i 8d <file_name>
    This command deletes the 8th line from the specified file.
  • Running a command at a specified time
    $ echo "command you want to run | at 01:00
    Note that the time is in 24hr format.
  • How to create a pencil sketch out of any image file
    $ convert <input_image> -colorspace gray \( +clone -blur 0x2 \) \
      +swap -compose divide -composite -linear-stretch 5%x0% <output_image>
    You can ofcourse add an alias or better still, a bash function for such long commands in your ~/.bashrc to make your life easier.
  • How to check unread mail from your gmail inbox from the command line
    $ curl -u your_email@gmail.com:your_password --silent \
      "https://mail.google.com/mail/feed/atom" | tr -d '\n' | \
      awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | \
      sed -n "s/<title>\(.*\)<\/title><summary>\(.*\)<\/summary.*name>\
      \(.*\)<\/name><email>\(.*\)<\/email>.*/\n\3\(\4\) - \1 - \2\n/p"
    This command might look like too much to handle, but it is really extremely simple. All it is doing is reading from your gmail account’s atom feed and formatting the output using awk and sed. As before, you are better of creating an alias(or a bash function) in your ~/.bashrc for this command.

No comments:

Post a Comment