Bash

Bash command guide

Bash is the Bourne Again Shell, enhanced version of original Unix shell sh.

Bash

The shell is a program that takes commands and gives them to the OS for execution. The shell can be interacted with via GUI (graphical user interface) or CLI (command line interface).

  • Last character of shell prompt: # implies root user
  • Last character of shell prompt: $ implies non-root user

Commands

General command structure: command -option arguments

Manipulating Files

  • Wildcards (for selecting files):
    • *: matches any characters
    • ?: matches any single character
    • [characters]: matches any character inside of [characters] set
      • [:alnum:]: alphanumeric characters
      • [:alpha:]: alphabetic characters
      • [:digit:]: numerals
      • [:upper:]: uppercase alphabetic characters
      • [:lower:]: lowercase alphabetic characters
    • [!characters]: matches any character not inside of [characters] set
    • Examples:
      • g*: match all filenames beginning with letter “g”
      • [abc]*: matches any file beginning with a, b, or c followed by any other characters
      • *[![:lower:]]: any filename not ending with lowercase letter
  • cp: copies files and directories
    • cp file1 file2: copies contents of file1 into file2
    • cp -i file1 file2: same as above but notifies user is file2 exists (and will be overwritten if accepted)
    • cp file1 dir1: copies file1 contents into dir1 as file named file1
    • cp -R dir1 dir2: copies contents of directory dir1 into directory named dir1 within dir2
  • mv: moves and renames files and directories
    • mv filename1 filename2: renames filename1 to filename2
    • mv file... directory: listed files are moved into directory
    • → if second argument (ie file2, dir2) does not exist, first argument file/dir is simply renamed. If second argument does exist, file2 is overwritten or dir1 is moved within dir2
  • rm: removes (deletes) files and directories
    • rm file: remove listed files
    • rm -r dir: (recursively) removes all files in dir
  • mkdir [directory]: creates directories
  • Above commands can be used with wildcards. Some examples:
    • cp *.txt text_files: copy files in working directory with names ending with .txt to text_files directory
    • rm *~: delete all files in working directory that end with ~

Command Info

  • Four Types of Commands:
    1. Executable program: like /usr/bin, compiled binaries written in particular language
    2. Command built into the shell: bash has many builtin commands (ie cd)
    3. Shell function: mini shell scripts incorporated into the environment
    4. Alias: command built from other commands, like a manual preset for, say, a builtin with a lot of options
  • type [command] : shows command type (one of above four)
  • which [command]: locates a command, works only for executables
  • help [command]: displays info about given command
    • can sometimes also use command –help; depends on if command supports it
    • notation note: [x] means argument x is optional, [-y|-z] means either -y or -z can optionally be used
  • man [command]: formal documentation for given command; not just for bash like help command; uses less, so navigate accordingly

I/O Redirection

  • Standard Output:
    • command > file: command is executed, results {overwrite} the named file
    • command >> file: command is executed, results are {appended} to named file
  • Standard Input:
    • command < file: command takes input from file instead of keyboard; results displayed
    • example: sort < in.txt > out.txt → sort command takes input from in.txt file and writes output to out.txt file; redirect operators must come after additional options in command
  • Pipelines: connecting multiple commands together
    • com1 | com2: standard output of command com1 is fed as standard input to command com2
    • example: ls -l | less → listing files in working directory fed to less as scrollable text
  • Filters: takes std input, performs operation on it (the “filter”), sends to std output
    • sort: sorts standard input
    • uniq: removes duplicate lines from stream of data
    • grep: examines input lines and returns those matching specific pattern
      • grep -rl "search string" /path/to/search to search files in the directory for a string
    • fmt: reads standard input text and formats it for output
    • pr: takes text input and splits into pages with with page breaks, header, footer
    • head: outputs first few lines of input
    • tail: outputs last few lines of input
    • tr: translates characters ie from lower to uppercase
    • awk: powerful language for constructing filters

Disk space

  • df -ha: “disk free” on all available disks
  • du -sh <path>: disk usage for file or directory

Other Commands

  • ps: process status, info about currently running processes
    • ps aux | grep <app> : show processes involving an app or keyword
  • find: find files
    • find . -name "<file regex>" -type f -delete will delete all files matching pattern recursively inside of current directory. Run without the -delete option to list the files that are matched, in the case you’d like to check before deleting.

Permissions

Permissions in Linux are broken into three categories: read, write, and execute. These permission categories apply three different groups: user, group, and world.

  • chown <user>: change owner, the user that user group permissions apply to
  • chgrp <group>: change group, all users in group have group permissions
  • chmod [ugoa][+ or –] [rwx] file: change permissions applying across groups (can use number options instead)

On occasion, you will see a + after the permissions when, say, viewing permissions via ls -l. This indicates that there are additional ACL settings in place, and these can be viewed by running getfacl <dir>.

Permission Rules

I recently ran into some issues trying to access folders as the postgres user; I was having a difficult time trying to figure out why the user was unable to see files in a mounted HDD. I even set permissions on a particular folder to the postgres user, and it was still unable to see anything. As it turns out, there are some greater rules that govern the access to subfolders within directories. The primary rule is: you can only traverse a directory if you have execute permissions on it (as well as trivial read access). That is, you need execute permissions on every folder along the path you’re trying to access. Just having access to a subfolder isn’t always enough: in my particular case with postgres, the mounted drive did not allow users in the group or others execute access on the directories, only smgr and root. I ended up having to modify settings with setfacl to allow Postgres to set up the tablespace.

Working with setfacl

  • Use -m to set (modify)
  • setfacl [ugoa]:[name,empty]:[rwx] sets ACL settings for group type, name of that group (if any), and the permissions to which the settings should be changed.

Scripting

Useful Scripts

  • To convert terrible and annoying .docx to .txt files, run libreoffice --headless --convert-to "txt:Text (encoded):UTF8" *.docx in the directory of choice.