Basic Linux Commands

  1. CLI Login terminal: Ctrl-alt-Fn
  2. Help
    1. Show help on command (e.g., ls) or configuration file name: man ls
    2. Sometimes need to look in different manual number (e.g., crontab): man 5 crontab
    3. Search help for keyword (e.g., cron): man -k cron
  3. Listing directory contents
    1. List directory: ls
    2. Also, if no ls: echo *
    3. List long (permissions, ownership, size, name): ls -l
    4. List all files (including starting with .): ls -a
    5. List long by most to least recent: ls -lt
    6. List with markers for file types: ls -F
    7. Remove default colors on ls: unalias ls

      This will only work for this login session for this user and this environment (and any spawned from it). To make it work for every login for this user, append that line to ~/.bash_profile

  4. Directory navigation
  5. Bash shell ctrl characters:
    1. Stop/kill current command/program: Ctrl-c
    2. Control scrolling: Ctrl-s to pause, Ctrl-q to resume
    3. Suspend current command/program: Ctrl-z
    4. Resume last suspended program: fg
  6. Shell redirection:
    1. <: read from
    2. >: create new or overwrite old
    3. >>: create new or append to existing
  7. Standard Files:
  8. Pipe: |

    Use standard output of command as standard input to another. There can be multiple pipes involved (i.e., each command separated by pipe character).

    Simple example: ls -lt | head

  9. Count number of lines (e.g., files): ls -1 | wc -l
  10. File operations:
    1. Change ownership: chown user.group file
    2. Change permissions: chmod perms file
  11. Make a directory: mkdir testdir
  12. Make a directory with intermediate directories: mkdir -p public_html/test
  13. Copy files:
    1. Straightforward: cp ~/.bashrc ~/.mybashrc
    2. Copy permissions as well: cp -p ~/.bashrc ~/.mybashrc
    3. Copy recursively (all files and subdirectories): cp -r public_html my_html
  14. Move/rename files/directories: mv
  15. Remove/delete a file:
    1. Straightforward: rm ~/.mybashrc
    2. Force it (no prompt): rm -f ~/.mybashrc
    3. Force it and recurse for all files and subdirectories (BE CAREFUL): rm -rf my_html
  16. Show the first 10 lines of a file or output: dmesg | head
  17. Show last 10 lines of a file or output: tail /var/log/secure
  18. Show last n lines as the file changes: tail -f /var/log/messages
  19. Find a file (e.g., RCS) anywhere: find / -name "RCS" -print
  20. Make a symbolic link to a directory or file (ln -s orig new): ln -s /opt/java1.6 /opt/java
  21. Use regular expressions to search files (grep):
    1. Find a user name in the password file: grep -i rondeau /etc/passwd
    2. Find something in a directory of files: grep -R localhost /etc
    3. Find something at the start of a line: grep "^root" /etc/passwd
    4. Find something at the end of a line: grep "bash$" /etc/passwd
    5. Find one of two things: grep "(root|nobody)" /etc/passwd
  22. Show how much space on file systems in human-readable form: df -h
  23. Show all processes (useful piped to grep): ps auxwww
  24. Show who is logged on: who