Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Command line tips & tricks

13

Comments

  • 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,929 ✭✭✭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,929 ✭✭✭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,929 ✭✭✭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: 13,948 ✭✭✭✭Johnboy1951


    CTRL + shift + t

    opens a new tab here (KDE)


  • Registered Users, Registered Users 2 Posts: 1,929 ✭✭✭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,110 ✭✭✭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: 13,948 ✭✭✭✭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,282 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,209 ✭✭✭✭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,929 ✭✭✭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,929 ✭✭✭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,929 ✭✭✭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,598 ✭✭✭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,080 ✭✭✭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,282 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, Paid Member Posts: 39,543 ✭✭✭✭Hotblack Desiato


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

    I'm partial to your abracadabra,

    I'm raptured by the joy of it all.



  • 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.


Advertisement