Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Command line tips & tricks

  • 16-04-2013 10:41am
    #1
    Registered Users Posts: 1,931 ✭✭✭PrzemoF


    "1. Use "!"

    If you use the command line a lot, it is very likely that you will run several different commands in a short span of time. And if you want to re-run a command that was just recently used, using "!" is much faster than using the arrow keys to find the command. With "!", you only have to remember the name of the command. For example, if you want to re run the last "vim" command, you just need to typ "!vim" and vim will open the last file without you typing the whole command.


    2. Use "!!"

    Using "!!" is the best way to re-call the previous command. It is actually faster than using the arrow keys. For example, when you forget to type "sudo" before the previous command, to re-run it with sudo, you just need to type "sudo !!"."


    More here: http://www.linuxandlife.com/2013/04/some-tips-to-use-command-line-faster.html


«134

Comments

  • Closed Accounts Posts: 350 ✭✭mickgotsick


    1. Use "!"

    You can use "!" with the history command as well. For example, you can search your history for a command of interest: history | grep <command>

    Each of the results will be numbered and to re-execute the one you want, you can just do !<number>


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    My favourite is ctrl+r. It searches back through your history for commands containing whatever string you type.

    e.g. ctrl+r, then type vim will bring up your last command containing vim, ctrl+r again and it'll find the one before that, etc.


  • Moderators, Science, Health & Environment Moderators Posts: 4,677 Mod ✭✭✭✭Tree


    Ctrl+r is probably the most useful way to root through my history.
    The other thing I do is alias the crap out of things I do regularly, using capital letters for the beginning of them helps narrow down the tab-complete a lot


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    Oh, the other thing I use all the time is !$ which means "the last argument in the last command line"

    So, for example:
    vim /long/path/to/file.txt
    mv !$ /tmp

    will mv file.txt to /tmp

    or even better....

    mv !$ !$.bak also works

    OOOH! Tasty!


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    I'm spamming now, but these are things that I'm just coming across during a day....

    If you have done key exchange on a remote server, tab completion does work.

    e.g.

    scp file.txt user@remote_server:~/dir<tab> will fill in the rest of the directory name for you. Obviously only works well on a LAN, but can be OK over the internet in a pinch.


  • Advertisement
  • Registered Users Posts: 33,822 ✭✭✭✭Hotblack Desiato


    Khannie wrote: »
    Oh, the other thing I use all the time is !$ which means "the last argument in the last command line"

    Or alt-dot which pastes the last argument in, and it can be edited.
    mv !$ !$.bak also works

    OOOH! Tasty!

    That's a good one alright :)

    Life ain't always empty.



  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    ninja900 wrote: »
    Or alt-dot which pastes the last argument in, and it can be edited.

    Beauty! That's a good one. Cheers.


  • Registered Users Posts: 1,931 ✭✭✭PrzemoF


    ^abc­^­def Run previous command, replacing abc with def

    Ctrl-XX toggles between current position and the beginning of the line

    Ctrl-A moves cursor to the beginning of the line

    Some more tips here: http://ss64.com/bash/syntax-keyboard.html


  • Closed Accounts Posts: 350 ✭✭mickgotsick


    PrzemoF wrote: »
    ^abc­^­def Run previous command, replacing abc with def

    Ctrl-XX toggles between current position and the beginning of the line

    Ctrl-A moves cursor to the beginning of the line

    Some more tips here: http://ss64.com/bash/syntax-keyboard.html

    Ctrl-E moves the cursor to the end of the line.


  • Closed Accounts Posts: 18,969 ✭✭✭✭syklops


    There are some real gems here.

    I just used !$ for adding an account, e.g.:

    useradd username01
    passwd !$

    Very handy. I will be using this alot!


  • Advertisement
  • Closed Accounts Posts: 350 ✭✭mickgotsick


    One thing that took me a while to discover was how to match a tab character using the standard grep command. It's easy to do with a perl one liner (or with one of the grep variants?) but if you want to use the standard grep you can use :space: to match a tab (also matches spaces though).


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    Can we please sticky this thread? It's very useful.

    I love brace expansion:
    vi /etc/httpd.conf
    
    cp !${,.bak}
    
    diff /etc/httpd.conf{.bak,}
    


  • Registered Users Posts: 1,731 ✭✭✭GreenWolfe


    Some really handy cd tricks here:

    http://www.thegeekstuff.com/2008/10/6-awesome-linux-cd-command-hacks-productivity-tip3-for-geeks/

    I really like this one for going up multiple directory levels with cd:
    alias cd..="cd .."
    alias cd...="cd ../.."
    alias cd....="cd ../../.."
    alias cd.....="cd ../../../.."
    alias cd......="cd ../../../../.."

    # cd /tmp/very/long/directory/structure/that/is/too/deep
    # cd.....
    [Note: use cd..... to go up 4 directory level]
    # pwd
    /tmp/very/long/directory/structure


  • Closed Accounts Posts: 18,969 ✭✭✭✭syklops


    I was going to ask this question:
    Does anyone know a way to move the cursor back one position, aside from using the arrow key?

    I ask, because in the brace expansion example, I would type that as cp !$ {}, then left arrow so I am between the braces and then type ,.bak. The reason being that I can do the {} in one movement and keep my hands in the same position, but I have to move my right hand off the keyboard to hit the arrow key, which is annoying and slows me down(albeit by less than a second, but time is money people!).

    But, with a little trial and error I found the answer myself. The answer is CTRL + b.

    Now to type the above example, I type !$ {} CTRL + b ,.bak ENTER

    Confused yet?


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    [-0-] wrote: »
    I love brace expansion

    Oh yes. Love that!

    for i in {0..9}; do <something 10 times>; done

    for i in {0..9}{0..9}; do <something 100 times, starting with 00, 01, etc.>; done

    [-0-] wrote: »
    Can we please sticky this thread?

    There is no mod of this forum. We're all to peaceful to require moderation. :D


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    Khannie wrote: »
    Oh yes. Love that!

    for i in {0..9}; do <something 10 times>; done

    for i in {0..9}{0..9}; do <something 100 times, starting with 00, 01, etc.>; done

    These always save me from having to fire up python. :) Love it!
    There is no mod of this forum. We're all to peaceful to require moderation. :D

    Shame on us then! :P It might be worth dropping the cat mod a PM.


  • Registered Users Posts: 33,822 ✭✭✭✭Hotblack Desiato


    Some really handy cd tricks here:
    alias cd...="cd ../.."
    alias cd....="cd ../../.."

    Or - openSUSE has these built in:
    alias ..='cd ..'
    alias ...='cd ../..'
    

    Also a good built-in one is
    alias ll='ls -l'
    

    I've added one to get rid of Flash cookies.
    alias killflash='rm -rf ~/.adobe ~/.macromedia'
    

    Life ain't always empty.



  • Closed Accounts Posts: 18,969 ✭✭✭✭syklops


    ninja900 wrote: »
    Or - openSUSE has these built in:
    alias ..='cd ..'
    alias ...='cd ../..'
    

    Also a good built-in one is
    alias ll='ls -l'
    

    I've added one to get rid of Flash cookies.
    alias killflash='rm -rf ~/.adobe ~/.macromedia'
    

    I used to use ll alot, and then I was on a system(can't remember which one) and it didn't have it, and I got out of the habit of using it. Just tried it now and Fedora has it.


  • Closed Accounts Posts: 2,267 ✭✭✭ Shiloh Echoing Tigress


    Prevent disasters, asks for a prompt to delete
    alias rm='rm -i'
    

    Not many know this one.. Override the alias!
    \rm
    

    This will ignore the alias above!


  • Closed Accounts Posts: 18,969 ✭✭✭✭syklops


    Prevent disasters, asks for a prompt to delete
    alias rm='rm -i'
    

    Not many know this one.. Override the alias!
    \rm
    

    This will ignore the alias above!

    On mission critical systems I go one further and I do:
    alias rm='mv --target-directory /tmp/garbage
    
    with a cronjob which periodically deletes the contents of the garbage directory.

    Saved my ass a few times.

    There was a bug in bash a while back where tab complete didnt append the '/', instead completed the word and added a space. I don't need to tell you what happened when I meant to do ' rm /bin/somedir/*' and instead did 'rm /bin/somedir *'


  • Advertisement
  • Registered Users Posts: 446 ✭✭Ant


    Many of the bash shell's best features have already been mentioned in this thread but one of my favourites is:
    set -o vi
    
    I used to be reasonably nifty with the default Emacs key bindings but, as a vim user, my command line editing has been much faster since I started using the vi key bindings.


    More recently, I've also added
    shopt -s histreedit
    
    to my bashrc. This provides the opportunity to re-edit bash history substitutions that didn't work the first time (which happens often enough for me).

    It's worth having a read of the bash manual every so often and experimenting with features you didn't know about or haven't previously tried.


  • Registered Users Posts: 1,931 ✭✭✭PrzemoF


    Rename all htm files to html:
    rename .htm .html *.htm
    

    or

    rename all files containing strange_file_name to image:
    rename strange_file_name image strange_file_name*
    

    i.e.
    strange_file_name_001.jpg -> image_001.jpg
    strange_file_name_002.jpg -> image_002.jpg

    I noticed that it's quite common to rename files using some one line bash scripts, but that's the easiest way.


  • Registered Users Posts: 446 ✭✭Ant


    PrzemoF wrote: »
    Rename all htm files to html:
    rename .htm .html *.htm
    

    This is indeed a very useful command - and one I've used many times. It's worth mentioning that the rename program described by PrzemoF is the one from the util-linux package which usually forms part of the stock install of Red Hat derived distributions.

    However, in Debian-based distributions (including Ubuntu and Mint) rename is sym-linked to a Perl script povided by the perl package. This script similarly allows the user to rename multiple files but it works differently: it's basically a wrapper to modify multiple file names using the same Perl expression. e.g. the above use case would be implemented as:
    rename s/htm/html/ *.htm
    

    An advantage with the Perl version is that it's more flexible. Also, it doesn't silently over-write files if a file with the same destination name already exists - which once happened to me. :(


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    Got a good one today - let's say you've kicked off a long running process and now you want to shift it to a screen session instead of killing it.

    (as root)
    echo 0 > /proc/sys/kernel/yama/ptrace_scope

    (as yourself)
    screen
    reptyr <pid>

    bingo, bango!


  • Registered Users Posts: 33,822 ✭✭✭✭Hotblack Desiato


    Hmm was stumped there for a minute until I did a search, Yama only came in with 3.4 kernel and I'm still on 3.1 :p will have to upgrade soon anyway though!

    Life ain't always empty.



  • Registered Users Posts: 1,089 ✭✭✭KAGY


    Been using the find command a lot recently. Esp with the exec option

    find ./ -type f -exec sudo chmod -c 640 {} \;
    For when I need to change perms for files only recursively
    Or using it with mplayer over ssh from my phone to the htpc to play music without turning the screen on . (Scared the skit out of the OH from work accidently running that)


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    ninja900 wrote: »
    Hmm was stumped there for a minute until I did a search, Yama only came in with 3.4 kernel and I'm still on 3.1 :p will have to upgrade soon anyway though!

    It may just work without that config change on older kernels. Worth a whirl anyway.


  • Closed Accounts Posts: 18,969 ✭✭✭✭syklops


    KAGY wrote: »
    Or using it with mplayer over ssh from my phone to the htpc to play music without turning the screen on . (Scared the skit out of the OH from work accidently running that)

    Sh!t like this is why I love Linux.


  • Registered Users Posts: 1,931 ✭✭✭PrzemoF


    Copy/move all files (filtered) from subdirectories to a new directory:

    Real life example - I had to copy all gnome icons containing word 'symbolic' to a new directory. The directory symbolic was already existing.
    find . -type f -name '*symbolic*' | xargs -I {} cp {} symbolic
    
    And more generic:
    find [any-find-options] | xargs -I {} cp {} /any/directory/you/want
    find [any-find-options] | xargs -I {} mv {} /any/directory/you/want
    


  • Advertisement
  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    Handy. FYI - find has an exec option, so:

    find <stuff> -exec cp {} <destination> \;

    I regularly use it to grep stuff in a large set of files / directory tree:

    find <stuff> -exec grep --with-filename <pattern> {} \;


Advertisement