Ubuntu cheat sheet

BASH prompt tricks

  • To search back through the command line history for a particular term hit Ctrl-r and then type the term. Hit Ctrl-r again to move backwards to the next instance of the term.
  • To edit the current user's cron jobs: crontab -e
File operations
  • See size of current directory: du -hs
  • See size of specified directory: du -hs /mydirectory
  • See listing of subdirectories with sizes: du -h /mydirectory
  • List of directories sorted by size: du /mydirectory | sort -nr
  • Recursively delete directory and its contents: sudo rm -Rd /media/temp/mydirectory
  • Recursively delete the contents of the current dir: rm -R *
  • Recursively copy /media/source/Documents to directory Documents under current directory preserving permissions, ownership & timestamps: sudo cp -av /media/source/Documents Documents
  • Recursively copy /media/source/Documents to /media/temp/Documents preserving permissions, ownership & timestamps: sudo rsync -av /media/source/Documents/ /media/temp/Documents
    - Note that if you run this without sudo the ownership of all of the files and directories gets changed to the current user and at least some hidden files won't copy because of permissions issues.
  • Get information about a drive partition: sudo fdisk -l
  • Mount partition from command line: sudo mount -t ext3 /dev/sdb1 /media/mountpoint
    • Where ext3 is the filesystem type, /dev/sdb1 is the location of the partition and /media/mountpoint is the directory where the partition should be mounted
  • Filesystem mounting configuration: /etc/fstab
  • Remount filesystem after editing fstab: sudo mount -a
  • Unmount mount: sudo umount /mount_point
  • Fix smbfs mount that has stopped working:
    1. Unmount smbfs mount: sudo umount /problem_mount_point
    2. Delete mount point: sudo rmdir /problem_mount_point
    3. Recreate mount point: sudo mkdir /problem_mount_point
    4. Remount smbfs mount (assuming its in fstab): sudo mount -a
  • Mount Samba share from command line or script:
    sudo mount -t smbfs -o username=defaults,password=defaults,uid=usersname,gid=groupname,umask=0000 "// 1" /media/zihuatanejo
  • Network configuration file: /etc/network/interfaces
  • Hosts file: /etc/hosts
  • Log on log: /var/log/auth.log
Permissions & Ownership
  • Give all permissions to all users: chmod a+rwx filename
  • Give all permissions recursively to all users to all files and directories under MyDirectory: sudo chmod -R a+rw /home/andy/MyDirectory/
  • Change ownership of file: sudo chown andy /home/andy/myfile
  • Change ownership of all files and sub-directories recursively: sudo chown -R andy /home/andy/MyDirectory
Repositories and Package Management
  • Basic Aptitude command line commands:
    • $sudo aptitude update | Updates the package lists
    • $sudo aptitude safe-upgrade | Upgrade as many packages as possible without removing existing packages.
    • $sudo aptitude full-upgrade | Upgrade everything, including deleting existing packages to solve dependencies
    • $sudo aptitude [ install | remove | purge ] pkg1 [pkg2] | Take actions on individual packages
    • $sudo aptitude search search terms | Search packages for terms
    • $sudo aptitude show pkg_name | Show information on the package

  • Repositories file: /etc/apt/sources.list
  • Editing Repositories from command line: https://help.ubuntu.com/community/Repositories/CommandLine
  • To install and configure predefined package collections, like LAMP server: sudo tasksel
  • To configure fonts (among other things) edit: /usr/share/scite/SciTEGlobal.properties
  • To configure PHP syntax highlighting edit: /usr/share/scite/html.properties
  • In order to run a BASH script you have to:
    • First make the file executable by running chmod 700 scriptname
    • Then to execute it from the current directory you have to use ./scriptname which translates to 'run scriptname from the current directory'
Services (Samba, SSH, Apache, etc)
  • As of Ubuntu 10.04 you can no longer start and stop services using /etc/init.d/apache2 stop.
  • The new method for starting and stopping services is: sudo service apache2 start
  • Good article on SSH port forwarding here:
  • To make a tunnel to a server behind a firewall:
    ssh -L 8888: andy@ -p 1234
    Where 8888 is a port on the client machine, is the port and IP address that you want traffic forwarded to from localhost:8888, and andy@ -p 1234 is the username, IP address, and port of the remote machine you are making the ssh connection to.
  • Whenever you make configuration changes you have to restart to have them take effect:
    $sudo /etc/init.d/ssh restart
  • To mount:
    sshfs username@remotemachine:/remotedirectory localmountpoint
  • To unmount: fusermount -u localmountpoint

No comments: