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

Command line tips & tricks

13

Comments

  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭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,969 ✭✭✭✭syklops


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

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


  • Registered Users 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 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 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 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,969 ✭✭✭✭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.


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


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


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


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

    In KDE its CTRL + shift + t.


  • Advertisement
  • Registered Users Posts: 13,985 ✭✭✭✭Johnboy1951


    CTRL + shift + t

    opens a new tab here (KDE)


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


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

    My mistake. Unfortunately I cannot edit it


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


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

    That's new tab for me.

    Stupid, awkward KDE.


  • Registered Users 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,969 ✭✭✭✭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,969 ✭✭✭✭syklops


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

    Stupid, awkward KDE.

    KDE rocks.


  • Registered Users Posts: 13,985 ✭✭✭✭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 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 Posts: 20,174 ✭✭✭✭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 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 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 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 Posts: 1,553 ✭✭✭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 Posts: 1,089 ✭✭✭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 /


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭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 Posts: 33,803 ✭✭✭✭Hotblack Desiato


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

    Life ain't always empty.



  • Advertisement
  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭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