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 there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Command line tips & tricks

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


    "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


«1

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>


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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,754 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


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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!


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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, Registered Users 2 Posts: 36,499 ✭✭✭✭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 :)

    In Cavan there was a great fire / Judge McCarthy was sent to inquire / It would be a shame / If the nuns were to blame / So it had to be caused by a wire.



  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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, Registered Users 2 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,966 ✭✭✭✭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, Registered Users 2 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,966 ✭✭✭✭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?


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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, Registered Users 2 Posts: 36,499 ✭✭✭✭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'
    

    In Cavan there was a great fire / Judge McCarthy was sent to inquire / It would be a shame / If the nuns were to blame / So it had to be caused by a wire.



  • Closed Accounts Posts: 18,966 ✭✭✭✭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 ✭✭✭h57xiucj2z946q


    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,966 ✭✭✭✭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, Registered Users 2 Posts: 453 ✭✭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, Registered Users 2 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, Registered Users 2 Posts: 453 ✭✭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. :(


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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, Registered Users 2 Posts: 36,499 ✭✭✭✭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!

    In Cavan there was a great fire / Judge McCarthy was sent to inquire / It would be a shame / If the nuns were to blame / So it had to be caused by a wire.



  • Registered Users, Registered Users 2 Posts: 1,093 ✭✭✭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)


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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,966 ✭✭✭✭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, Registered Users 2 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
  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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> {} \;


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


    Khannie wrote: »
    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> {} \;

    I was just about to post that


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    cd /home/somedir
    python -m SimpleHTTPServer

    presto! "somedir" is served up on port 8000. Very handy way of moving files between machines where you don't want to give (or aren't arsed setting up) SSH login for example.


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


    Khannie wrote: »
    cd /home/somedir
    python -m SimpleHTTPServer

    presto! "somedir" is served up on port 8000. Very handy way of moving files between machines where you don't want to give (or aren't arsed setting up) SSH login for example.

    I regularly use that. Its dead handy.


  • Registered Users, Registered Users 2 Posts: 10 The_Old_Grey Whistle_Thief


    You can use Ctrl-l instead of clear to clear the screen.
    Probably my most used command. I just can't seem to work on a full screen.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Putting a space at the start of a command means it doesn't get added to bash history.

    " mplayer omgfilthyporn.mkv" :pac:

    or more usefully:

    " sudo mount hidden_encrypted_volume" (note, sudo'd commands go into syslog by default, but you can disable that too on a per-command basis).


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


    Khannie wrote: »
    Putting a space at the start of a command means it doesn't get added to bash history.

    " mplayer omgfilthyporn.mkv" :pac:

    or more usefully:

    " sudo mount hidden_encrypted_volume" (note, sudo'd commands go into syslog by default, but you can disable that too on a per-command basis).

    Love this!


  • Banned (with Prison Access) Posts: 1,151 ✭✭✭rovoagho


    This thread is like porn for nerds, thanks to all the contributors. I just have to make myself read it once a week now so I'll remember it all!


  • Hosted Moderators Posts: 3,331 ✭✭✭Splinter


    only installed Mint properly at the weekend and was talking to Syklops last night and said "ah "Alt + ." is great but i can't say i can see myself using it... needless to say i have very much eaten my own hat. fantastic thread folks :)


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


    Splinter wrote: »
    only installed Mint properly at the weekend and was talking to Syklops last night and said "ah "Alt + ." is great but i can't say i can see myself using it... needless to say i have very much eaten my own hat. fantastic thread folks :)

    Yeah you didn't sound very interested in it last night, but maybe it was how I was explaining it. You have to do it to under stand. I find it great for networking tools. So you ping 192.168.253.1, and then you traceroute [alt + .] and then you nmap [alt + .] and then you nc -vv [alt + .] 22.


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


    This opens nautilus with current directory (kinda obvious...)
    nautilus .
    
    same can be used for image viewer eog:
    eog .
    
    and the amazing command for removing duplicate files - fdupes
    most common usage (run in current dir, dive into sub dirs and ask what files to preserve if duplicates are found):
    fdupes -R -d .
    


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


    Cross post from Development.

    Vundle
    Syntastic
    my .vimrc:
    syntax on
    se nu
    set autoindent
    set cindent
    
    set rtp+=~/.vim/bundle/vundle/
    call vundle#rc()
    
    " let Vundle manage Vundle
    " required!
    Bundle 'gmarik/vundle'
    
    
    Bundle 'scrooloose/syntastic'
    let g:syntastic_enable_signs=1
    let g:syntastic_auto_loc_list=1
    let g:syntastic_mode_map = { 'mode': 'active', 'active_filetypes': ['c'], 'pa
    ssive_filetypes': ['html', 'css', 'slim'] }
    
    Bundle 'scrooloose/nerdtree'
    Bundle 'scrooloose/nerdcommenter'
    nmap <leader># :call NERDComment(0, "invert")<cr>
    vmap <leader># :call NERDComment(0, "invert")<cr>
    Bundle 'majutsushi/tagbar'
    Bundle 'gregsexton/gitv'
    Bundle 'sjl/splice.vim'
    Bundle 'tpope/vim-fugitive'
    

    See the attached image for syntastic in action


  • Registered Users, Registered Users 2 Posts: 28 adrianm68


    These are great, thanks for posting. You all probably know this one :

    sudo !!

    Runs the previous command as super user in case to you forgot.


  • Banned (with Prison Access) Posts: 1,151 ✭✭✭rovoagho


    See the very first post. :)


  • Registered Users, Registered Users 2 Posts: 28 adrianm68


    rovoagho wrote: »
    See the very first post. :)

    Omg! Have I really become one of 'those' people. OK so I got caught out. I admit to having not read the thread before posting how much I liked it. People are strange. Especially me!

    Apologies if I woke anyone up.

    Zzzzz zzzzz


    Adrian


  • Registered Users, Registered Users 2 Posts: 6,393 ✭✭✭AnCatDubh


    Somewhat related is an account on twitter - https://twitter.com/climagic

    on the go since 2009 and with about 7k tweets, it should keep your interest going on, and on, and on, and on, and......................

    :)


  • Registered Users, Registered Users 2 Posts: 570 ✭✭✭hooplah


    Speaking of resources, if you haven't seen it already http://explainshell.com is really handy. Especially for when you're following tutorials or problem solutions from something like stackexchange


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    For ubuntu, if you need to build something from source, this command installs all build dependencies prior to the build:
    aptitude build-dep <package>
    

    Winner, winner, chicken dinner.


  • Registered Users, Registered Users 2 Posts: 116 ✭✭partyboy690


    The single most used command on my job

    find . -iname "*.cpp" -exec grep -Hn "Something in code I need to find" {} \;

    Ctrl+R and Ctrl+L are vital as well, couldn't get on without them.

    Another really handy one is this, we use it extensively at work to kill jobs on remote machines through jenkins

    kill -9 `ps -e | grep <Whatever the name of the job is> | cut -d' ' -f1`


  • Registered Users, Registered Users 2 Posts: 1,093 ✭✭✭KAGY



    Another really handy one is this, we use it extensively at work to kill jobs


    kill -9 `ps -e | grep <Whatever the name of the job is> | cut -d' ' -f1`

    correct me if I'm wrong but would killall processname do the same job


  • Registered Users, Registered Users 2 Posts: 116 ✭✭partyboy690


    KAGY wrote: »
    correct me if I'm wrong but would killall processname do the same job
    You're correct it would but this command is for specialised scripts we run and it doesn't have a nice processname like firefox or something like that, so we need to just know the name of the properties file and we can kill the jobs that way. Our Jenkins setup is very convoluted :D we have to implement hacks just to get things working, I'm the guy who maintains it and I'm slowly improving it over time to make it so we don't need ridiculous commands like that.

    EDIT: Will say the setup was there before I joined otherwise it'd be much better ;)


  • Advertisement
Advertisement