Some useful Unix commands


  • The most basic: navigation: 
    • cd, cd ..
    • ls
    • ls -alShr
    • ls -alShr | less
    • ls -alhF
    • cp, mv, rm, rm -r 
  • The locations in which the shell searches for executables:
    • echo $PATH
  • Other useful echo commands:
    • echo $SHELL

  • Finding stuff:
    • find command
      • Find all files with name "*alpha*" :
        • find . -name "*alpha*" -print
      • List of all such files in a text file
        • find . -name "*alpha*" -print >> list.txt
      • Number of such files:
        • find . -name "*alpha*" -print | wc -l
      • Find all txt files created in the last 60 days: 
        • find /home/you -iname "*.txt" -mtime -60 -print

    • awk command
    • sed command
    • grep command
    • locate command
  • List only directories...
    • find . -type d -depth 1
    • ls -F | grep /
    • ls -ld -- */


  • Shell script: create many (in this example, 100) directories:
    • for i in {1..100}; do mkdir subdirectory_$i; done
    • mkdir subdirectory_{1..100}

  • Add pdf extension to all files in a folder:
    • for i in *; do mv $i ${i}.pdf ; done
  • Finding the location of the executable which the shell runs when a command is entered:
    • which ls
    • whereis gcc
  • Current values of all environment variables:
    • set
    • printenv
  • Counting 
    • the number of lines in a file
      • wc -l
    • creative use
      • ls -alshr > tmp.txt wc -l
      • find . -name '*.f90' | xargs wc -l

  • redirect the output of a command into a file: 
    • command > filename 
    • Instead, if we wish to append in the file named filename:
      • command >> filename 
    • set > filename.txt;

  • Content of a text file:
    • head, tail
    • cat,
    • vi, nano, pico
    • less
  • Use of | and >
  • Login as a root user:
    • sudo -i
  • Getting help:
    • man, man --help
    • help
    • whatis
    • apropos
    • info
    • man 5 passwd
    • man -a passwd
  • Processes related information:
    • top
    • ps

  • System related info:
    • System name 
      • uname -s
      • uname
    • Network host name:
      • unmae -n 
    • Kernel version
      • uname -v
    • Info about kernel release:
      • uname -r
    • machine hardware name:
      • uname -m
    • machine processor architecture name
      • uname -p
    • All of the above info together
      • uname -a

      • sysctl -n machdep.cpu.brand_string
    • Hardware info:
      • sysctl -a hw
      • /usr/sbin/system_profiler SPHardwareDataType
    • Storage related infor:
      • /usr/sbin/system_profiler SPStorageDataTypesw_vers
    • RAM:
      • sysctl hw.memsize
    • All CPU related info:
      • sysctl -a | grep cpu
    • Full info (this will take a lot of time and generate a lot of output, see help):
      • /usr/sbin/system_profiler 
      • /usr/sbin/system_profiler -h
      • /usr/sbin/system_profiler -listDataTypes
    • Disk space
      • diskutil list
      • df -h
      • du -h





Comments