bits of code

By | 13th May 2016

How to chroot properly. What are the devices and directories to mount

for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt/$i; done

To create user in kobotoolbox

If you have access to the hosted server:

  • You can get into the kpi container doing:
    docker exec -it <kpi> bash
  • Then enter the Django shell:
    ./manage.py shell_plus
  • Then from inside the ipython shell, you can create a user doing the following:
    User.objects.create(username='<some new user>')
  • Or if you are creating a whole bunch of users:
for user in users:
    User.objects.create(username=user)

(Additionally you can assign permissions to those users as you create them through the shell — perhaps listing usernames and permissions in your spreadsheet, importing that and iterating through as you create and assign, etc. I can send through some sample code if that will be useful)

  • pretify json via commandline
    cat myfile.json | python -m json.tool
  • Start Unetbootin in debian. change sudo to su accordingly.
    sudo export QT_X11_NO_MITSHM=1 unetbootin

    archlinux forum
  • Reset git HEAD
    git reset HEAD~1
  • Include full path in the output of find command for current directory
    find -name "filename" -exec readlink -f {} \;
  • Check progress of dd command while writing to a drive. Very useful while creating bootable pen drives.

    Create bootable drive.
    dd if=[iso path] of=[device] bs=4


    While the above command is running open another terminal and give this command and check output in previous terminal
    watch -n5 'sudo kill -USR1 $(pgrep ^dd)'

  • How to come out of ssh connections that got closed.
    Enter, ~, .
     type these on the terminal whose connection is lost.
  • Play music from a folder and sub folders.
    mplayer -playlist <(find "$PWD" -name "*.mp3" -type f)
  • Adjust recording time for gnome default recorder which is activated with CTRL+ALT+SHIFT+R and to stop use the same combination before max recording time is reached or recording will stop automatically reaching the max time.
    gsettings set org.gnome.settings-daemon.plugins.media-keys max-screencast-length 360

    360 sec
  • Extract audio from video file
    ffmpeg -i sample.avi -q:a 0 -map a sample.mp3
  • List all public_html folder with long list in home directory
    find . -maxdepth 1 -type d -exec ls -l {} + | grep 'public_html' | grep 'drw'
  • List files based on extension, date and copy them to a directory
    find . -type f -newermt 2018-04-18 ! -newermt 2018-04-19 | grep php | xargs -P 2 -I _FILE_ cp _FILE_ [foldername]/
  • Sort unique values and get word with length between 8 and 63
    cat [textfile] | sort | uniq | pw-inspector -m 8 -M 63 > [textfile]
  • Exclude folders from compression with tar. Make sure to remove trailing ‘/’
    tar -zcv --exclude='[folder]' -f [backup.tgz] [dir_to_backup]
  • find zombie processes
    ps xao pid,ppid,pgid,uname,stat,comm | awk '$5=="Zs"'
  • Find size of directories from the current path and sort them in decending order with highest first.
    du -sch * | sort -h -r
  • Command to get hardware information in the system
    inxi -Fxz