Summer Nights

A blog on embedded and real-time systems as well as on general computing issues

Linux Tips

  • To execute a linux command on multiple files matching a pattern (e.g. change file format to utf-8):
    for f in *.php; do iconv -f ISO-8859-1 -t UTF-8 $f -o $f; done
  • To stop X when in Gnome:
    /etc/init.d/gdm stop
  • To start X again from console:
    /etc/init.d/gdm start
  • List all users who have a home folder:
    cat /etc/passwd | grep "/home" |cut -d: -f1
    All users can be listed by:
    cat /etc/passwd | cut -d: -f1
  • To delete a user:
    userdel -r username
    -r removes the user files as well.
  • To make a tarball:
    tar -cvf xyz.tar folder_name
    To make tarball with bzip2:
    tar -cjvf xyz.tar.bz2 folder_name
  • To decompress a tarball:
    tar -xvf xyz.tar
    To decompress a tarball into a given folder:
    tar -C /desired/path/ -xvf xyz.tar
    To decompress a tarball with bzip2:
    bzcat xyz.tar.bz2 ¦ tar -xf -
    Add or drop -v to enable/disable verbose mode.
  • To disable email notifications from crontab, redirect the output to /dev/null. Append the following to the end of cronjob:
    >/dev/null 2>&1
    The first part sends the standard output (stdout) to /dev/null and the second part sends error stream (strerr) to the same place as stdout.
  • Delete all files in a directory except one file:
    rm -rf $(ls | grep -v '^some_file$')
  • Background and foreground. To send a running process to background:
    Press Ctrl - Z and then enter bg on the command prompt. The process will be sent to background.
    To see a list of jobs running in the background:
    Enter jobs at the command prompt.
    To bring a background process to foreground, enter:
    fg job_id
    The job_id can be found by entering jobs.
    Add an & to the end of a command, runs it directly in the background.
  • To copy the results of a find, and grep:
    find -name *xyz3* | grep 2955 | xargs -i cp {} ../folder1/
  • crontabs:To edit cron jobs:
    crontab -e -u username
    edits the cron jobs of a given user. You should follow the timing guidelines hinted in the beginning of the opened file.
    To edit system-wide cron jobs:
    emacs /etc/crontab
  • Wake On LAN (WOL) to wake up a machine on local network:
    etherwake 00:11:22:33:44:55
    If etherwake is not installed, then on debian, use:
    apt-get install etherwake
  • Setting PATH variable:
    emacs /etc/profile
    Add /desired/path to the end of PATH, like, e.g.,
    export PATH=$PATH:/desired/path
    After saving the file, issue:
    source /etc/profile
    to load the new path information.
    This will add it to all the users’ path except root. If you want to add it to root’s path as well, you need to edit .bashrc file in /root folder by adding the following:
    export PATH=$PATH:/desired/path
    And then reloading the info by:
    source /root/.bashrc
  • Kick a remote ssh user:
    First list all the users by:
    who
    It will also indicate their terminal id (e.g., pts/0, etc.). Just kill the desired terminal like:
    pkill -9 -t terminal_id
  • Delete all files/folders that match a certain criteria:
    ls | egrep '[0-9].{4,9}08' | xargs rm -rf
  • Mounting CD/DVD images:
    mount /path/to/image.iso /path/where/to/mnt -o loop
    umount /path/where/to/mnt
  • Run a script at startup:
    update-rc.d -f my_script start 99 2 3 4 5 .
    where
    – start is the argument given to the script (start, stop).
    – 99 is the start order of the script (1 = first one, 99= last one)
    – 2 3 4 5 are the runlevels to start
    Don’t forget the dot at the end. Use /etc/init.d/skeleton to write your own startup/stop/etc. script.
    Or you can simply use the defaults as:
    update-rc.d my_script defaults
    To run a script after X11 startup, use:
    sudo update-rc.d my_script start 99 5 .
    Another option for simpler commands is to add them in /etc/rc.local file.
  • To rename all files of a given type in a folder, example:
    for i in *.mpeg;do mv $i `basename $i .mpeg`.m2ts; done
  • List all files of a given size
    List files greater than 1 megabyte size:
    find -size +1M
    List files smaller than 50 byte size:
    find -size -50c
    List files with a size of 500 kilobytes:
    find -size 500k
    For further details look at man page of find command.
  • To display a detailed list of all the hardware in the system:
    lshw
  • To find out current ethernet speed, advertised speeds, etc.:
    ethtool eth0
  • To display the sizes of files and directories in a sorted list:
    du -sk * | sort -nr
  • List all the listening ports and the attached processes:
    netstat -tunlp
  • Set good permissions on web files and folders:
    sudo find /var/www -type d -print0 | xargs -0 chmod 755
    sudo find /var/www -type f -print0 | xargs -0 chmod 644

Leave a Reply

Your email address will not be published. Required fields are marked *