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! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
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

2»

Comments

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


    I had edited my post to include this, but I'll repost it here to keep the flow of conversation:-

    btw you could add those string of commands into .bashrc as a function.
    function mykill () {kill -9 `ps -e | grep "$1" | cut -d' ' -f1`}


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


    KAGY wrote: »
    I had edited my post to include this, but I'll repost it here to keep the flow of conversation:-

    btw you could add those string of commands into .bashrc as a function.
    function mykill () {kill -9 `ps -e | grep "$1" | cut -d' ' -f1`}
    Yeah I could do that6, doesn't make too much of a difference because this command is run adhoc as a job on Jenkins.


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


    Those are not typical command line tips, but I hope they are close enough to fit into this thread.

    In vim:
    :make - This will run the regular old make in the command line for you.
    :copen - Opens the quickfix window, to show the results of the compilation
    :cw[indow] - Opens the quickfix window only if there is an error.
    :cn - Goto the next error in source code.
    :cp - Goto the previous error in source code.
    :cfirst - Goto the first error in source code.
    :clast - Goto the last error in source code.

    P.S. All credit goes to the author of [URL="In vim: :make - This will run the regular old make in the command line for you. :copen - Opens the quickfix window, to show the results of the compilation. Opens the window even if there are no errors in compilation. :cw[indow] - Opens the quickfix window only if there is an error. If quickfix window is open and there are now errors, the window is closed. :cn - Goto the next error in source code. :cp - Goto the previous error in source code. :cfirst - Goto the first error in source code. :clast - Goto the last error in source code. how come that I did not know those earlier?? P.S. All credit goes to the author of this blog"]this blog[/URL]


  • Closed Accounts Posts: 1,004 ✭✭✭Recondite49


    Generate a random number between X and 1 (in this case 10) drawing directly from the pool of random data.

    echo "$(od -An -N4 -tu4 /dev/random) % 10 + 1" | bc

    You can check available levels of entropy in your system as follows:

    cat /proc/sys/kernel/random/entropy_avail

    Be advised that checking available levels of entropy in itself uses up entropy.

    If you need lots of random numbers, a personal favourite program of mine is randomsound. It uses white noise from your sound card and pipes it straight into /dev/random.

    This comes in very handy when trying to generate gpg/SSL keys.


  • Closed Accounts Posts: 1,004 ✭✭✭Recondite49


    Another nugget I discovered the other day.

    A friend of mine had a folder with over one million jpgs inside it. He wanted to know if there was a terminal command which would allow for automatic search and deletion of all images below a certain size (in this case 250kb).

    After some judicious googling I saw that this can be done in one line:

    find . -name \*.jpg -size -250k -exec rm {} \

    This will take some time but at least it's simple.


  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    jeans-out-of-dryer-tuco-tight-tight-tight-breaking-bad.gif


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


    Dave! wrote: »
    jeans-out-of-dryer-tuco-tight-tight-tight-breaking-bad.gif

    Anyone else baffled?


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


    It means "great". Quoting pictures! I'm surprised at you. :P


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


    Khannie wrote: »
    It means "great".

    News to me.
    Quoting pictures! I'm surprised at you. :P

    Well if I didnt quote the picture, how would anyone know what I was baffled about?


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


    syklops wrote: »
    News to me.

    Have you not seen Breaking Bad??? :eek:


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


    Today I learned about a program called parallel. It does what you think it would - launches lots of programs in parallel to make use of your lovely, lovely cores. Here's an example:

    parallel -j 8 oggenc -- *.flac

    Translation:

    in parallel, using 8 cores, run oggenc. Each instance of oggenc will be given one of the files after the -- (in this case a flac file). When one finishes, a new one is spawned so 8 are always running in parallel until the jobs are exhausted.

    I had put up a script in here to achieve this before, but this is handier.

    edit: Encoded a whole album in 14 seconds. Yummy.


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


    Khannie wrote: »
    Have you not seen Breaking Bad??? :eek:

    Just the first few episodes. Its on my 'to watch' list.


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


    "Save a file as root with your normal user

    Sometimes you start to edit a file without noticing that you can’t save it for permissions problems, tee can easily save you in this case, as example:

    $ vim /etc/passwd
    [:w !sudo tee %]

    In vim this means:

    :w saves the file
    !sudo calls the sudo command
    tee redirects vim’s output
    % the current file

    After this command you’ll have saved successfully your file and can exit safely from the editor."

    Source: http://linuxaria.com/pills/linux-terminal-the-tee-command


  • Closed Accounts Posts: 1,004 ✭✭✭Recondite49


    Generate a long, random string of X bits :

    dd if=/dev/random bs=1 count=X 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev

    e.g

    dd if=/dev/random bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev

    7X50xEE3Xxjb0DxI7BDhGqig8k2PfZnJflVj0KG/14g


    Very useful if you want to generate a secure password, although admittedly difficult to remember! :)


  • Registered Users, Registered Users 2 Posts: 218 ✭✭Tillotson


    Generate a long, random string of X bits :

    dd if=/dev/random bs=1 count=X 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev

    e.g

    dd if=/dev/random bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev

    7X50xEE3Xxjb0DxI7BDhGqig8k2PfZnJflVj0KG/14g


    Very useful if you want to generate a secure password, although admittedly difficult to remember! :)
    cat /dev/random | base64 | head -c 10
    


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


    After you execute a command (or shell script) in the background using &, if you logout from the session, the command will get killed. To avoid that, you should use nohup as shown below.
    $ nohup ./my-shell-script.sh &
    
    Using at command you can schedule a job to run at a particular date and time. For example, to execute the backup script at 10 a.m tomorrow, do the following.
    $ at -f backup.sh 10 am tomorrow
    
    More here: http://www.thegeekstuff.com/2010/12/5-ways-to-execute-linux-command/


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


    bash:
    Ctrl-T - transpose 2 previous words in command line
    Alt-T - same for letters
    Ctrl+_ - undo changes


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


    PrzemoF wrote: »
    bash:
    Ctrl-T - transpose 2 previous words in command line
    Alt-T - same for letters
    Ctrl+_ - undo changes

    Im on Fedora and its the other way round.


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


    Almost universally, Ctrl + Alt + T opens a new terminal. <3


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


    Khannie wrote: »
    Almost universally, Ctrl + Alt + T opens a new terminal. <3

    In KDE its CTRL + shift + t.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 14,031 ✭✭✭✭Johnboy1951


    CTRL + shift + t

    opens a new tab here (KDE)


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


    syklops wrote: »
    Im on Fedora and its the other way round.

    My mistake. Unfortunately I cannot edit it


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


    syklops wrote: »
    In KDE its CTRL + shift + t.

    That's new tab for me.

    Stupid, awkward KDE.


  • Registered Users, Registered Users 2 Posts: 1,109 ✭✭✭Skrynesaver


    This is about terminal commands, if your Xterm application captures bash commands you should probably remap the keys as [CTRL]-T (transpose letters), is one of the most useful shortcuts out there for the thickfingered clumsy demographic.


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


    This is about terminal commands, if your Xterm application captures bash commands you should probably remap the keys as [CTRL]-T (transpose letters), is one of the most useful shortcuts out there for the thickfingered clumsy demographic.

    By that you mean, most of us.


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


    Khannie wrote: »
    That's new tab for me.

    Stupid, awkward KDE.

    KDE rocks.


  • Registered Users, Registered Users 2 Posts: 14,031 ✭✭✭✭Johnboy1951


    In KDE it is (at least here :) )

    Ctrl + shift + t .... for new tab

    and

    Ctrl + shift + n .... for New window

    They are of course easily modified to your own preferences ;)


  • Registered Users, Registered Users 2 Posts: 5,238 ✭✭✭humbert


    Khannie wrote: »
    Almost universally, Ctrl + Alt + T opens a new terminal. <3
    I think the mileage on almost universally in Linux isn't great.


  • Moderators, Computer Games Moderators Posts: 4,281 Mod ✭✭✭✭deconduo


    humbert wrote: »
    I think the mileage on almost universally in Linux isn't great.

    Those sort of shortcuts are mainly DE/WM dependant anyway.


  • Registered Users, Registered Users 2 Posts: 20,180 ✭✭✭✭jimgoose


    CTRL-insert/SHIFT-insert is copy/paste in VNC/PuTTY/ESX console on Solaris/AIX/RHEL/SuSE. :)


  • Advertisement
  • Closed Accounts Posts: 824 ✭✭✭Kinet1c


    Ctrl+Alt+t in LXDE too.


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


    Create new file or wipe out content of existing file
    > file_name
    
    That's how is works:
    [przemo@localhost test]$ ls
    [przemo@localhost test]$ > file.log
    [przemo@localhost test]$ ls -l
    total 0
    -rw-rw-r--. 1 przemo przemo 0 Dec  5 10:01 file.log
    [przemo@localhost test]$ echo "something" > file.log 
    [przemo@localhost test]$ ls -l
    total 4
    -rw-rw-r--. 1 przemo przemo 10 Dec  5 10:02 file.log
    [przemo@localhost test]$ > file.log 
    [przemo@localhost test]$ ls -l
    total 0
    -rw-rw-r--. 1 przemo przemo 0 Dec  5 10:02 file.lo
    

    more here: http://www.linuxtechi.com/standard-input-output-error-in-linux/


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


    A stack with directories useful for scripting, but also in normal day-to-day command line operations. Basic commands: pushd, popd and dirs (to show the stack)
    [przemo@localhost freecad]$ pwd
    /home/przemo/fedora/FreeCAD/freecad
    [przemo@localhost freecad]$ pushd .
    ~/fedora/FreeCAD/freecad ~/fedora/FreeCAD/freecad
    [przemo@localhost freecad]$ cd /tmp
    [przemo@localhost tmp]$ cd /bin
    [przemo@localhost bin]$ cd /usr/share/
    [przemo@localhost share]$ popd
    ~/fedora/FreeCAD/freecad
    [przemo@localhost freecad]$ pwd
    /home/przemo/fedora/FreeCAD/freecad
    


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


    File
    cat file_name_here | command arg1 arg2
    
    can be replaced with
    < file_name_here | command arg1 arg2
    

    http://www.catb.org/jargon/html/U/UUOC.html


  • Registered Users, Registered Users 2 Posts: 1,581 ✭✭✭Dante


    I use this in my bashrc to prettify my bash prompt:
    PS1='\e[0;35m\[[\u\]\e[0;37m\]@\e[0;32m\]\h]\e[0;37m\]\$\[\e[0m\] '

    It will look something like below:
    [usernameserver]:


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


    Can't remember if this was posted before. Actually I must look back over all the posts again.

    If you need to do a calculation for an option you can use $((calc))

    e.g. mount a partition from a dd disk image
    sudo mount -o ro,loop,offset=$((122880*512)) pi_2015-09.dd /media/SD_backup
    

    where the numbers came from fdisk -l
    $sudo fdisk -l pi_2015-09.dd
    Sector size (logical/physical): 512 bytes / 512 bytes
    
    Device                   Boot  Start     End Sectors Size Id Type
    pi_2015-09.dd1        8192  122879  114688  56M  c W95 FAT32 (LBA)
    pi_2015-09.dd2      122880 6399999 6277120   3G 83 Linux
    

    quick edit:
    to see progress of a dd
    watch --interval=10 "sudo kill -SIGUSR1 1234"
    
    where 1234 is the PID from
    ps -ef | grep / dd/
    
    there is a space after the last /


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


    recursive scp slow because of lots of small files?

    ssh remotebox -T -o Compression=no -x "tar cf - /remote/path" | tar xf - -C .

    or if you want to store it locally as a tarball:

    ssh remotebox -T -o Compression=no -x "tar cf - /remote/path" > tarball.tar

    or a compressed tarball:

    ssh remotebox -T -o Compression=no -x "tar cf - /remote/path" | gzip -c > tarball.tar.gz

    I'm working with a lot of routers these days and they don't have the storage or processing power to be tarring up stuff on the device so this works well. :)


  • Moderators, Computer Games Moderators Posts: 4,281 Mod ✭✭✭✭deconduo


    Khannie wrote: »
    recursive scp slow because of lots of small files?

    ssh remotebox -T -o Compression=no -x "tar cf - /remote/path" | tar xf - -C .

    or if you want to store it locally as a tarball:

    ssh remotebox -T -o Compression=no -x "tar cf - /remote/path" > tarball.tar

    or a compressed tarball:

    ssh remotebox -T -o Compression=no -x "tar cf - /remote/path" | gzip -c > tarball.tar.gz

    I'm working with a lot of routers these days and they don't have the storage or processing power to be tarring up stuff on the device so this works well. :)

    You could also use rsync :)


  • Registered Users, Registered Users 2 Posts: 35,476 ✭✭✭✭Hotblack Desiato


    You need rsync installed at the remote end though, which a router may not have.

    Scrap the cap!



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


    deconduo wrote: »
    You could also use rsync :)

    It's not on the router. :) Does it not suffer the same painfully slow multi-file copy issue that SCP does though? It's a while since I've used rsync.


  • Registered Users, Registered Users 2 Posts: 2,385 ✭✭✭RebelButtMunch


    Want to answer 'n' to lots of repetitive questions? Use the YES command
    e.g.
    "yes n | gzip -d *"


  • Registered Users, Registered Users 2 Posts: 2,385 ✭✭✭RebelButtMunch


    Want to answer 'n' to lots of repetitive questions? Use the YES command
    e.g.
    "yes n | gzip -d *"


  • Registered Users, Registered Users 2 Posts: 2,385 ✭✭✭RebelButtMunch


    cat a file backwards... use tac command


  • Registered Users, Registered Users 2 Posts: 883 ✭✭✭Keplar240B




  • Registered Users, Registered Users 2 Posts: 732 ✭✭✭Dero


    One thing that (irrationally) bugs me is seeing "grep -v grep"; e.g.
    ps -ef | grep java | grep -v grep
    

    So I use this character class trick that someone showed me years ago:
    ps -ef | grep [j]ava
    

    It'll also work anywhere else that regex can be used, like awk:
    ps -ef | awk '/[j]ava/ {print $2}'
    

    Yes, pgrep will do this, but I work a lot on AIX, which doesn't have pgrep.

    Also, for parallel execution, you can use xargs and nproc if parallel is not installed/available.
    ls -1 *.flac | xargs -P $(nproc) -n1 -I{} oggenc "{}"
    

    Using nproc will match the number of logical CPUs in your machine, but you could use more or less if you want.


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


    $ cat a
    A
    B
    C
    D
    $ cat b
    1
    2
    3
    4
    $ paste a b
    A   1
    B   2
    C   3
    D   4
    

    Nothing really special about it, but it just saved me some work :-)


  • Registered Users Posts: 83 ✭✭rayzercork


    if you have the xfce terminal installed type

    xfce4-terminal --drop-down

    and it will drop down from the top of your screen. if you create a launcher with that command and stick it into usr/share/applications you can run it from the menu or else assign a keyboard shortcut like F12 to activate it.
    i like to run cmus music player inside this terminal because i can hide it out of the way when not needed


  • Registered Users Posts: 70 ✭✭lyda


    If you want to version control your home directory, look at vcsh: https://github.com/RichiH/vcsh

    It's also available in most distros. The nice part is that you can put different parts of your home dir in different repos.



Advertisement