Skip to content

Latest commit

 

History

History
108 lines (82 loc) · 2.19 KB

Bash.org

File metadata and controls

108 lines (82 loc) · 2.19 KB

Bash

Bash Oneliner Sed Awk

Find

for file in $(find . -name "*.txt"); do
    # Do something
done

# Or
find "$dir" -type f | while read -r FILE; do
    # Do something
done

## Or
# {} is a placeholder for the filename
# + tells it to include as many paths as possible
  # Alternatively, use \; to say to use only one file per invocation
find . -name "*.csv" -exec command {} +

CSV

# the second sort identifies the second field (-k 2) and sorts by that
echo $PATH | cut -d / -f 2 | sort | uniq -c | sort -k 2

xargs

Runs a command against each individual output

# -n 1 -- only one arg
# -P10 parallelize with 10 threads
# -I{} use the arg with a placeholder
# -o save the output to placeholder.csv
seq 0 1000 10000 | xargs -n 1 -P10 -I{} curl "site.org/file.csv/{}" -o {}.csv

Basic Commands

# translate char to another
tr ':' '\n'

# select fields out of delimiters
# select the second field (-f) out of the / delimiters
cut -d / -f 2

# identify the number of unique fields and the count of them
uniq -c
#! /bin/bash
var=3
echo "User: $var"

echo hell{o,yeah}

# Replace a command with its output
echo $(date)

# Check the number of args
if [[ $# -lt 2  || $# -eq 4 ]]; then
fi

for val in list; do
done

for x; do # defaults to in $@
done

Arg parsing

$@Args
$#number of params
$kk-th positional command line param

File-related tests

-eFile exists
-dFile exists and is a directory
-rfile is readable
-wwriteable
-xexecutable
-ssize > 0

String-related tests

-z STRINGlength 0
-n> 0
==equality

Functions

function funcname {
}