FILE="example.tar.gz"
echo "${FILE%%.*}"
# example
echo "${FILE%.*}"
# example.tar
echo "${FILE#*.}"
# tar.gz
echo "${FILE##*.}"
# gzdirname /path/to/dir/filename.txt
# /path/to/dir
basename /path/to/dir/filename.txt
# filename.txt
basename /path/to/dir/filename.txt .txt
# filenameRecursive walk - print all files
find * -type [f|d] -maxdepth 3 -mindepth 1 -name "foo.txt"ROOT=/workspaces/ServerData/cache/local;
find $ROOT/* -maxdepth 1 -mtime +30 -user $USER -exec stat --format "%x %n" '{}' \; | sort
find * -type f | while read FILE; do
echo $FILE;
# do something with file...
done;see stackoverflow
To test if a variable named var is defined:
if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fiValue of
$foowill default to"bar"if$1is not already defined:foo=${1:-"bar"}
if ! [ -d $HOME/foo ]; then echo "foo doesnt exist"; fiif [ $mod_file_count -eq 0 ]
then
echo "mod_file_count = 0";
return 0;
else
echo "mod_file_count != 0";
exit 1;
fiecho "col1,col2,col3" | cut -d',' -f2
# col2
Column names
FILE=<filename>
head -n 1 $FILE | awk 'BEGIN { FS="|" } { for(i = 1; i <= NF; i++) { printf("%d %s\n", i, $i) } } 'Column sample
FILE=path/to/pipedelimited.txt
COL_POS=1
head -n 10 $FILE | awk -v COL=$COL_POS 'BEGIN { FS="|" } { printf("%s\n", $COL) }'Column value histogram
FILE=path/to/pipedelimited.txt
COL_POS=1
cat $FILE | awk -v COL=$COL_POS 'BEGIN { FS="|" } { printf("%s", $COL) }' | sort -n | uniq -cCOL_POS=1
find * -type f | while read FILE; do
echo $FILE;
cat $FILE | awk -v COL=$COL_POS 'BEGIN { FS="|" } { printf("%s\n", $COL) }'
done;FS: Field SeperatorNF: Number of fields in current recordNR: Current record number (overall line number)FNR: Current record number (file line number)$0: Entire current record$n: nth field in current recordFILENAME: current filenameENVIRON: enviroment variables (egENVIRON["USER"])
-n|--quiet|--silentonly print a line if explicitly requests withp-i|--inplaceedit the file inplace
Pretty Print
cat somefile.txt | column -t -s,TASK_DESC=$(jira_desc $TASK_ID 2>&1)
: ${ACTIVATOR_BIN:="$(which activator)"}| Syntax | Result |
|---|---|
arr=() |
Create an empty array |
arr=(1 2 3) |
Initialize array |
${arr[2]} |
Retrieve third element |
${arr[@]} |
Retrieve all elements |
${!arr[@]} |
Retrieve array indices |
${#arr[@]} |
Calculate array size |
arr[0]=3 |
Overwrite 1st element |
unset arr[2] |
Delete 3rd element |
arr+=(4) |
Append value(s) |
str=$(ls) |
Save ls output as a string |
arr=( $(ls) ) |
Save ls output as an array of files |
${arr[@]:s:n} |
Retrieve n elements starting at index s |
for ELEMENT in ${ARRAY[@]}; do
printf "%s\n" $ELEMENT;
done;netstat -p tcpmount current folder
docker run -it --mount "type=bind,source=$(pwd),destination=/foo" --user $(id -u):$(id -g) --workdir="/foo" ubuntu bash