Linux Tips

Using the Linux command line

Basics:
  • <tab> key auto-completes program and file names
  • <up> key goes through your command history
  • <ctrl>-<c> interrupts the current command
  • refers to your home directory
  • is used to match multiple files
  • exit - quit the current shell (terminal)
  • man [program] - show documentation for a program
Module management:
  • module avail - list available modules
  • module load [module] - load a module
  • module list - list loaded modules
  • module rm [module] - unload a module
Statistical software on the command line:
  • module load R; R
  • module load stata; stata-mp
  • module load sas; sas -memsize 0 -sortsize 0 -maxmemquery 0 -work /kellogg/tmp [program.sas]
  • module load matlab/r2016a; matlab
File Commands:
  • ls - list contents of directory
  • ls -la - include hidden files and show details
  • cd [dir] - change directory
  • cd - change to your home directory
  • pwd - print working directory
  • mkdir [dir] - create a directory
  • rm [file] - delete a file
  • rmdir [dir] - delete an empty directory
  • cp [file1] [file2] - copy file1 to file2
  • cp -r [dir1] [dir2] - copy directory
  • cat [file] - print the contents of a file
  • less [file] - browse a file
  • head [file] - print the beginning of a file
  • tail [file] - print the end of a file
  • touch [file] - create an empty file
  • ln -s [file] [link] - create a symbolic link
  • df -h - list free disk space on system
  • du -sh [dir] - print size of folder's contents
Text editors:
  • nano [file] - a simple editor
  • vi [file] - a complex editor
  • emacs [file] - another complex editor
Archiving:
  • tar tvf [tarball] - list contents of a tarball
  • tar xvf [tarball] - extract a tarball
  • tar czvf [tarball.tar.gz] [file1] [file2] ... - create a tarball
  • unzip [zipfile] - unzip a zipfile
  • zip [zipfile] [file1] [file2] ... - create a zipfile
Process management:
  • ps - list your currently active processes
  • top - list all processes on the system
  • w - list who is logged in and what they are doing
  • kill [pid] - kill a process by process id
  • killall [process] - kill all processes by name
  • screen - a background process manager
  • bg - list bash background jobs
  • fg - brings the more recent bash bg job to the foreground
File Permissions:
  • chmod go-rwx [file] - revoke permissions for group and others to read, write, or execute a file.
  • chmod +x [file] - make a file executable by everyone
    • You can mix and match the u,g,o,+,-,r,w,x parameters as needed.
Searching:
  • grep [pattern] [files] - search for a regular expression pattern in the files.
  • grep -r [pattern] [dir] - search recursively in a directory
  • [command] | grep [pattern] - search for a pattern in the output of a command
  • find . | grep [pattern] - search for a file whose name matches a pattern
  • find . -name "*.txt" - more efficiently find all files ending in ".txt"
  • diff [file1] [file2] - show the difference between two files
Pipes:
  • [command1] | [command2] - use output of command1 as input to command 2
  • [command] > [file] - save output of command to a file
  • [command] >> [file] - append output of command to a file
  • [command] &> [file] - save standard output and error output to a file
Environment variables:
  • env - list environment variables
  • export PATH=[dir]:$PATH - add a directory to your executable search path
  • which [application] - show location of a program
Advanced:
  • source [file] - run commands in a file
  • ./[script_file] - run an executable script in the current folder
  • ssh [user]@[hostname] - log into another machine
  • wc -l [file] - count lines in a file
  • awk "{print \$3}" [file] - print the third word of every line
  • sort [file] - sort the lines of a file alphabetically
  • sort [file] | uniq - print unique lines in a file 
  • crontab -e - define jobs to be run periodically
  • [command] & - run command in the background
  • nohup [command] - allow command to continue running even if terminal is closed
  • [command1]; [command2]; ... - run a sequence of commands
  • my_var="hello world" - set a variable $my_var to the value "hello world"
  • my_var=$([command]) - save a command's output in a variable
  • echo ${my_var} - print the value of a variable
  • wget [url] - download a file
  • lynx [http_address] - browse the web
  • echo "message body" | mail -s "message subject" recipient@gmail.com - send an email
  • finger [username] - print information about a user
  • uptime - show system's uptime
  • cat /proc/cpuinfo - list CPU info