• home
  • rss
  • search
  • archive

this is a long and useful list of all the terminal Mac commands i frequently use

Customize your Terminal Bash Prompt

Before you begin your terminal ways, you can make your bash prompt on the terminal look fancy.I love tweaking the way my prompt looks and these are the settings I use:

  • Check your current Bash Prompt paramters

    echo $PS1
    default is \h:\W \u\$  
    

Now the Terminal commands in increasing order of geek-pleasure:

File Commands

  • remember at any point to quit command and return to prompt hit Ctrl + C

    • Change Directory

      cd <directory you want to change to>
      
    • Show Present directory

      pwd
      
    • Make Directory

      mkdir <name of directory you want>
      

    File Listing

    • File Listing Basic

      ls -al
      

      File Listing with more details

      ls -altr
      
    • Open File

      open -a <Name of Application> <filename>
      open -a Fraise todo.txt
      
    • Deleting

      delete file

      rm <filename>
      

      delete directory

      rm -r <directory name>
      

      forcefully delete

      rm -f <filename>
      

      example : remove all pesky DS_Store

      rm -rvf '.DS_Store'
      
    • Copying File1 to File2

      cp <file1> <file2>
      
    • Rename File1 to File2

      mv <file1> <file2>
      
    • Create Symbolic Link

      ln -s <filename> <linkname>
      
    • output contents of file

      more <filename>
      less <filename>
      head <filename>
      tail <filename>
      
    • show the last lines as they grow

      tail -f <filename></ul> 
      

    Searching

    • Find all instances of a file

      locate <filename>
      
    • File Finding Advanced

      find /Applications -name "*.ttf"
      
    • the famous grep searcher

      grep <pattern> <files> <command> | grep <pattern>
      
    • recursively grep through directories

      grep -r <pattern> <directoryname> 
      
    • Find an application

      whereis <application name>
      
    • which is the default application that will run

      which  <application name>
      

    Compairson Commands

    • Find the differences between two folders

      diff -rq /Users/kaushikgopal/Music /Volumes/Storage/Music 
      

    Process Management

    • Display currently active processes

          ps  
          ps aux
      
    • Terminate or kill a process

          kill <pid>
          killall <process name>
      
    • sequence of commands to relaunch the Finder App

          ps | grep Finder (get PID)
          kill PID
          open /System/Library/CoreServices/Finder.app
      

    rsync and the world of Synchronization

    rsync deserves a post of it’s own, but to give you a brief idea, here are a couple of commands i use frequently:

    • Synchronize Music Storage folders on Macbook and External Drive

          rsync -av --progress --size-only --exclude '.DS_Store' --exclude 'iTunes' /Users/kaushikgopal/Music/ /Volumes/Storage/Music/
      
      • a – Archive includes a bunch of parameters to recurse directories, copy symlinks as symlinks, preserve permissions, preserve modification times, preserve group, preserve owner, and preserve device files. You usually want that option for all your backups.
      • -v : Verbose
      • -z : compress and send
      • -n : Dry run
      • -u : Update by checking filestamp
      • -E : Make sure all the correct Metadata goes through
      • -P or –progress : allows rsync to continue interrupted transfers and show a progress status for each file.
      • –delete : delete files that don’t exist on the sending side
      • –delete-excluded : also delete excluded files from dest dirs
      • –exclude-from ‘/home/backup/exclude.txt’

      Note: – A trailing / on a source name means “copy the contents of this directory”. Without a trailing slash it means “copy the directory”. This difference becomes particularly important when using the –delete option. – a trailing slash on the source changes this behavior to transfer all files from the directory src/bar on the machine foo into the /data/tmp/.

    System Information

    • show current date and time

          date
      
    • show this month’s calendar

          cal
      
    • show system’s uptime

          uptime
      
    • Show user currently logged in

          whoami
      
    • show cpu information

          cat /proc/cpuinfo
      
    • show memory information

          cat /proc/meminfo 
      
    • directory free space

      df -h
      
      • h – in human readable form
    • Show disk usage or check file sizes or directory space usage

      du -sh *
      
      • s – summary
      • h – in human readable form

    File Permissions

            chmod  <octal permissions> <filename>
    
    • 4 = read (r) ; 2 = write (w) ; 1 = execute (x)
    • order : owner/group/world
    • e.g1 chmod 777 – rwx for everyone
    • e.g2 chmod 755 – rw for owner and rx for everyone

    Network Commands

    • ping the host

          ping <host>
      
    • get DNS for domain

          dig <domainname>
      
    • get whois for domain

          whois <domainname>
      
    • download file directly

          wget <file>
      
    • continue stopped download

          wget -c <file>
      
    • recursively download files from url

          wget -r <file>
      

    SSH connection

    • connect to host as user

      ssh user@host
      
    • connect to host as user using port p

      ssh -p <port> user@host
      
    • connect to host and use bind port

      ssh -D <port> user@host
      
    • connect to host and use bind port

      ssh -D <port> user@host
      

    Miscellaneous Cool Commands

    • Make two commands run one after the other with an & (take the example of killing a process and then reopening)

      kill PID && open /System/Library/CoreServices/Finder.app
      
    • Check bitrate of mp files

      afinfo filename |grep "bit rate"
      
    • add a hidden file to ensure finder doesn’t add index files

      sudo touch /Volumes/drive_name/.metadata_never_index
      
    • find out what process or application is using a specified file at that given moment.

      lsof /path/to/filename
      
    • find out what process or application is using a specified file in real time.

      sudo opensnoop -f /var/log/system.log
      
    • find out what files are being used by an applicaiton.

      sudo opensnoop -n Safari
      

    Some useful links: