Bash
Bash command guideBash 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 directoriescp file1 file2
: copies contents of file1 into file2cp -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 file1cp -R dir1 dir2
: copies contents of directory dir1 into directory named dir1 within dir2
mv
: moves and renames files and directoriesmv filename1 filename2
: renames filename1 to filename2mv 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 directoriesrm file
: remove listed filesrm -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
totext_files
directoryrm *~
: delete all files in working directory that end with~
Command Info
- Four Types of Commands:
- Executable program: like /usr/bin, compiled binaries written in particular language
- Command built into the shell: bash has many builtin commands (ie
cd
) - Shell function: mini shell scripts incorporated into the environment
- 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 executableshelp [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 filecommand >> 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 inputuniq
: removes duplicate lines from stream of datagrep
: examines input lines and returns those matching specific patterngrep -rl "search string" /path/to/search
to search files in the directory for a string
fmt
: reads standard input text and formats it for outputpr
: takes text input and splits into pages with with page breaks, header, footerhead
: outputs first few lines of inputtail
: outputs last few lines of inputtr
: translates characters ie from lower to uppercaseawk
: powerful language for constructing filters
Disk space
df -ha
: “disk free” on all available disksdu -sh <path>
: disk usage for file or directory
Other Commands
ps
: process status, info about currently running processesps aux | grep <app>
: show processes involving an app or keyword
find
: find filesfind . -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 tochgrp <group>
: change group, all users in group have group permissionschmod [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
- Great documentation on Shell parameter expansion, useful for recent bash scripting
Useful Scripts
- To convert terrible and annoying
.docx
to.txt
files, runlibreoffice --headless --convert-to "txt:Text (encoded):UTF8" *.docx
in the directory of choice.