$ printf "%x\n" 12345678
bc614e
$ printf "%d\n" 0xbc614e
12345678
stdbuf -i0 -o0 -e0 COMMAND
https://stackoverflow.com/questions/3465619/how-to-make-output-of-any-shell-command-unbuffered
while [[ <condition> ]]
do
<do something>
done
while ./my_script; do sleep 10; done
https://stackoverflow.com/questions/25157642/how-to-repeatedly-run-bash-script-every-n-seconds
# stop process
Ctrl-Z
# resume
fg
https://stackoverflow.com/questions/1879219/how-to-temporarily-exit-vim-and-go-back
cal
https://www.cyberciti.biz/faq/howto-displays-calendar-date-of-easter/
sort -k2n
cat foo.txt | sort -k 3 # sort by third column
https://unix.stackexchange.com/questions/104525/sort-based-on-the-third-column
# replace N with line
head -N file | tail -1
# NUM is line number (possibly faster for giant file)
sed 'NUMq;d' file
https://stackoverflow.com/questions/6022384/bash-tool-to-get-nth-line-from-a-file
cat testfile | awk '{ print length, $0 }' | sort -n | cut -d" " -f2-
https://stackoverflow.com/questions/5917576/sort-a-text-file-by-line-length-including-spaces
awk '{ print length }' abc.txt
wc -L my_file
http://www.cs.cmu.edu/afs/cs/academic/class/15513-m19/www/codeStyle.html
# declare an array variable
declare -a arr=("element1" "element2" "element3")
## now loop through the above array
for i in "${arr[@]}"
do
echo "$i"
# or do whatever with individual element of the array
done
# You can access them using echo "${arr[0]}", "${arr[1]}" also
https://stackoverflow.com/questions/8880603/loop-through-an-array-of-strings-in-bash
ARRAY=()
ARRAY+=('foo')
ARRAY+=('bar')
comm -12 file1 file2
https://stackoverflow.com/questions/2696055/intersection-of-two-lists-in-bash
<command> |& <command>
Syntactic sugar for 2>&1 |
in Bash. Introduced in Bash 4.0.
https://stackoverflow.com/questions/16497317/piping-both-stdout-and-stderr-in-bash/37085215#37085215
sudo /sbin/shutdown -r now
http://tldp.org/LDP/lame/LAME/linux-admin-made-easy/system-shutdown-and-restart.html
# command here is actually the name of a <command>!
help command
https://askubuntu.com/questions/512770/what-is-use-of-command-command
# Replace first occurrence of PATTERN with STRING or empty string
${PARAMETER/PATTERN/STRING}
${PARAMETER/PATTERN}
# Replace all occurrences of PATTERN with STRING or empty string
${PARAMETER//PATTERN/STRING}
${PARAMETER//PATTERN}
http://wiki.bash-hackers.org/syntax/pe#search_and_replace
function print_something() {
echo Hello $1
}
https://ryanstutorials.net/bash-scripting-tutorial/bash-functions.php
cat temp | while read in; do host "$in"; done
https://stackoverflow.com/questions/13939038/how-do-you-run-a-command-for-each-line-of-a-file
# Linux
sha256sum /path/to/file
# Mac OS
shasum -a 256 /path/to/file
https://stackoverflow.com/questions/3358420/generating-a-sha256-from-the-linux-command-line
<(...)
basically runs a command and captures the output as a file-like object.
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
bash -s stable < <(curl ...)
is equivalent to
curl ... > something
bash -s stable < something
https://superuser.com/questions/404780/what-bash-syntax-mean http://tldp.org/LDP/abs/html/process-sub.html
diff <(ls $first_directory) <(ls $second_directory)
cp -v file1.txt{,.bak}
# is equivalent to
cp -v file1.txt file1.txt.bak
mv foo{.bak,}
# is equivalent to
mv foo.bak foo
https://www.cyberciti.biz/faq/explain-brace-expansion-in-cp-mv-bash-shell-commands/
# JSON already in file
python -m json.tool my_json.json
# pipe to pretty-print
echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool
https://stackoverflow.com/a/1920585/1128392
SCRIPTPATH=$( cd $(dirname $0) ; pwd )
locate libffi
wget URL -P my_dir
http://stackoverflow.com/questions/1078524/how-to-specify-the-location-with-wget
$ expr 1 + 1
2
# floating-point arithmetic (not supported natively in Bash)
$ awk "BEGIN {print 208955300 / 15892428}"
13.1481
https://www.shell-tips.com/2010/06/14/performing-math-calculation-in-bash/
# split on space, take 11th field, remove comma, sum
cat MY_FILE | cut -d ' ' -f 11 | sed 's/,//' | awk '{s+=$1} END {print s}'
http://stackoverflow.com/questions/450799/shell-command-to-sum-integers-one-per-line
LC_ALL=C sort
https://superuser.com/questions/631402/sort-lexicographically-in-bash
sort -V
# Example output
0.1.0
0.1.1
0.1.2
0.1.10
# don't truncate ps command
ps axuww
https://stackoverflow.com/questions/2159860/viewing-full-output-of-ps-command
# -f prints uid, pid, parent pid, ...
ps -ef
https://superuser.com/questions/117913/ps-aux-output-meaning
Get process id using ps -ef
. Then use ps -eo pid,cmd,lstart | grep <pid>
.
http://stackoverflow.com/questions/5731234/how-to-get-the-start-time-of-a-long-running-linux-process
echo "STRING
WITH
NEWLINES" | tr '\n' ' '
echo -e $(cat filename)
grep PATTERN myfile | sed 's/\\n/\n/g'
sed '/^\s*$/d'
http://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed
Synonym of source
. Runs commands from file in current shell.
bash file
causes file to be run in child process, and parent script will not see modifications (e.g.: to variables).
export
makes the variable available to sub-processes.
http://stackoverflow.com/questions/1158091/defining-a-variable-with-or-without-export
readarray
is not available on OSX.
# instead of
readarray -t myArray < thing_per_line
# use
while read line; do
myArray+=($line)
done < thing_per_line
sudo -u pigmgr COMMAND
http://askubuntu.com/questions/294736/run-a-shell-script-as-another-user-that-has-no-password
echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode
http://askubuntu.com/questions/178521/how-can-i-decode-a-base64-string-from-the-command-line
cat foo.json | python -m json.tool
# will print error if invalid, otherwise prints out json
http://stackoverflow.com/questions/3858671/unix-command-line-json-parser
Validate but don't output tree:
xmllint -noout foo.xml
time (sleep 1 && sleep 1)
Not supported natively, but can just define a function:
graph ()
{
dot -o$1.pdf -Tpdf $1.gv
}
graph my_graph_data
https://stackoverflow.com/questions/7131670/make-a-bash-alias-that-takes-a-parameter
Check what an alias is assigned to
type ALIAS
http://askubuntu.com/questions/102093/how-to-see-the-command-attached-to-a-bash-alias
pids=""
for command in $COMMANDS
do
# Use & (ampersand) to run task in background
# Use $! to get pid of most recent background command
$command &
pids+=" $!"
done
for pid in $pids
do
wait $pid
done
filename="$1"
while read -r line
do
echo "$line"
done < "$filename"
http://stackoverflow.com/questions/10929453/bash-scripting-read-file-line-by-line
for test_file in tests/test_*.py
do
./$test_file
done
http://stackoverflow.com/questions/20796200/how-to-iterate-over-files-in-directory-with-bash
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
# Tests the return status of the last executed command
# AND
if COMMAND1 && COMMAND2
# e.g.:
if ! grep foo my_file
then # Run if "foo" not present in my_file
...
fi
# OR
if COMMAND1 || COMMAND2
[ -d FILE ] # true if FILE exists and is a directory
# Conditional expressions (include square brackets [ ] around them)
[ -f FILE ] # true if FILE exists and is regular file (not directory of link)
[ -e FILE ] # true if FILE exists. FILE could be directory.
[ -h FILE ] # true if FILE exists and is a symlink
[ -L FILE ] # true if FILE exists and is a symlink
[ -n STRING ] or [ STRING ] # true if length of STRING is non-zero
[ -r FILE ] # true if FILE has read permission for current user
[ -s FILE ] # true if FILE exists and has a size greater than 0
[ -x FILE ] # true if FILE exists and is executable
[ -z STRING ] # true if STRING is of length 0 (empty string)
# can also be used to test if a variable is set
[ STRING1 == STRING2 ] # true if two strings are equal
[ STRING1 != STRING2 ] # true if two strings are not equal
[ $# < 1 ] # true if no arguments passed to script
[ ! EXPR ] # true if EXPR is false
https://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/fto.html
Examples:
# integer comparison
# equal to
if [ "$a" -eq "$b" ]
# http://tldp.org/LDP/abs/html/comparison-ops.html
# check number of arguments
# https://stackoverflow.com/questions/18568706/check-number-of-arguments-passed-to-a-bash-script
if [ "$#" -ne 2 ]; then
echo "Two arguments required."
exit 1
fi
if [ -f /var/log/messages ]; then
echo "/var/log/messages exists."
fi
# http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_02.html
if [ $[$year % 400] -eq "0" ]; then
echo "This is a leap year. February has 29 days."
elif [ $[$year % 4] -eq 0 ]; then
if [ $[$year % 100] -ne 0 ]; then
echo "This is a leap year, February has 29 days."
else
echo "This is not a leap year. February has 28 days."
fi
else
echo "This is not a leap year. February has 28 days."
fi
0
is true, 1
is false. See http://stackoverflow.com/questions/3924182/how-the-keyword-if-test-the-value-true-of-false. Exit codes of commands are evaluated. 0
is true, 1
is false.
-lt # less than
http://www.tldp.org/LDP/abs/html/comparison-ops.html
See http://stackoverflow.com/questions/229551/string-contains-in-bash.
string='My long string';
if [[ $string == *"My long"* ]]
then
echo "It's there!";
fi
free
top
# check memory usage of process
ps aux PID
# VSZ = virtual memory usage of entire process (in KiB)
# RSS = resident set size, the non-swapped physical memory that a task has used (in KiB)
https://superuser.com/questions/117913/ps-aux-output-meaning
See http://www.linux.com/news/software/applications/8208-all-about-linux-swap-space.
Check vm.swappiness
in /etc/sysctl.conf
. Default is 60
. To verify:
sysctl vm.swappiness
A higher number means the system is more likely to swap pages out into swap space.
You may need to add a grep
to exclude directories (which are also 0 bytes):
hadoop fs -ls <dir> | awk '{ if ($5 == 0) print $8 }'
To delete these 0-byte files, do:
hadoop fs -ls <dir> | awk '{ if ($5 == 0) print $8 }' | xargs hadoop fs -rm
getent group <groupname>
Update one file in zip
# will update if file already exists
zip -r foo.zip path/to/file
https://stackoverflow.com/questions/4799553/how-to-update-one-file-in-a-zip-archive
Exclude files
zip -r $(BASE_NAME).zip $(BASE_NAME) -x foo/{*.class,*.swp}
https://gist.github.com/ldong/15ee0b1faa121891e9b53dddcefa0ca0
Zip ignore hidden files
zip -r bitvolution.zip bitvolution -x *.git*
https://askubuntu.com/questions/28476/how-do-i-zip-up-a-folder-but-exclude-the-git-subfolder
Zip hidden files
zip -r foo.zip .
http://stackoverflow.com/questions/12493206/zip-including-hidden-files
Zip/unzip folder:
zip -r foo.zip foo
unzip foo.zip
Unzip one file:
unzip foo.zip file
Unzip one file to standard out:
unzip -p foo.zip file
List files in a zip:
zipinfo foo.zip
unzip -l foo.zip
Remove a file from a zip:
zip -d foo.zip file
Extract gunzip'd .gz file to specified file:
gzip -c -d file.gz > file.out
-c
is equivalent to --stdout
. -d
is equivalent to --decompress
.
Show files in tar/tarball/tar.bz2:
tar -tvf file.tar
tar -ztvf file.tar.gz
tar -jtvf file.tar.bz2
Extract .tar file to different directory:
tar -xf archive.tar --directory=/target/directory
Tar but exclude some directories:
tar cvzf file.tar.gz --exclude 'dir/a/*' --exclude 'dir/b/*' dir
Extract one file from tarball to standard out:
tar -Oxzf tarball.tar.gz path/to/file
cd -
See:
- http://stackoverflow.com/questions/428109/extract-substring-in-bash
- http://tldp.org/LDP/abs/html/string-manipulation.html
# string can be a variable name
${string%%substring}
http://tldp.org/LDP/abs/html/string-manipulation.html
$ word="abcde"
$ echo ${word:0:3}
abc
$ word="abcde"
$ echo $word | cut -c 1-3
abc
# Remove longest match of `substring` from `$string`
${string##substring}
Also see http://wiki.bash-hackers.org/syntax/pe#substring_removal.
Remove last character from substring:
${MYSTRING%?}
%
(percent) matches from the end. ?
matches any character. See http://wiki.bash-hackers.org/syntax/pattern.
Usually used to signify end of arguments
# -- means end of arguments
# $$ means pid of current process
kill -- -$$
https://stackoverflow.com/questions/976059/shell-script-to-spawn-processes-terminate-children-on-sigterm https://unix.stackexchange.com/questions/11376/what-does-double-dash-mean
See http://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables.
$1, $2, $3 # positional parameters
$@ # array representation of positional parameters
$# # number of parameters
$* # IFS (Internal Field Separator) expansion
$- # current shell options
$$ # pid of current shell
$_ # most recent parameter
$IFS # input field separator
$? # most recent foreground exit status
$! # PID of most recent background command
$0 # name of shell or shell script
See http://ss64.com/bash/bang.html. On Linux, Alt + .
prints out the last word of the last command.
!! # run last command again
!foo # run most recent command starting with foo
!foo:p # !foo dry run, adds !foo to command history
!$ # last parameter/last argument of the last command
^foo^bar # run last command replacing foo with bar
See http://askubuntu.com/questions/191999/how-to-clear-bash-history-completely.
history -c && history -w # clear current history, then write empty history to ~/.bash_history
setfacl -m user:<user>:rwx <file>
setfacl -x <user> <file> # Remove <user> from <file>'s ACL list
setfacl --remove-all <file> # Remove all ACLs for a file
setfacl --remove-default <file> # Remove default ACLs
getfacl <file>
A user needs execute permissions on a directory to cd
into it. Even if a file is 777, a user still needs permissions on the chain of parent directories to access the file.
With substitution:
cat temp | xargs -I 'TABLE' hive -e 'drop table TABLE'
find . -name *.jar | grep hadoop- | grep -v tests | grep -v sources | xargs -n1 -I% scp % $ANOTHER_MACHINE:hadooptest
One argument per command line:
find . -name *.jar | xargs -n 1 jar tf
Substitute in multiple places:
find . -name *.jar | grep PATTERN1 | xargs -n1 -I% sh -c 'echo % && jar tf % | grep PATTERN2'
-I%
tells xargs
to replace %
with the arguments passed in. See http://stackoverflow.com/questions/18731610/xargs-with-multiple-commands for details.
scp -oProxyCommand="ssh -W %h:%p proxyhost.example.com" myFile destinationhost.example.com:
https://superuser.com/questions/276533/scp-files-via-intermediate-host
scp FILE1 FILE2 DESTINATION
scp lib/{j1.jar,j2.jar,j3.jar} .
diff -r dir1 dir2
diff -w FILE1 FILE2
diff <(ls -1a ./dir1) <(ls -1a ./dir2)
If your patch file patch.diff
looks like
diff --git a/file b/file
...
to apply as if the diff looks like
diff --git file file
...
use
patch -p1 < patch.diff
patch -p0 < patch.diff
patch FILE PATCH
patch -R < PATCH
filterdiff -i '*file' patch.diff > filtered.diff
One issue with filterdiff -i
is it removes any diff --git ...
and index ...
lines. To avoid this, you can use filterdiff -x
or filterdiff -X
. However, it does not exclude new and deleted files:
filterdiff -X fileWithExcludePatternsOnePerLine patch.diff > filtered.diff
# mtime
touch -m --date="7 days ago" FILE
ls -t
ls -l # mtime (modification time)
ls -cl # ctime (inode change time)
ls -ul # atime (access time)
ls -d */
ls ~USER
Add the following to your .bashrc or .bash_profile to enable color ls output by default:
alias ls="ls --color=auto"
# On Mac, to mimic default Linux colors
export LSCOLORS=ExGxBxDxCxEgEdxbxgxcxd
ls | tr "\\n" " "
ls | cat
ls -d */
tree -d
# smallest to largest
ls -Slhr
ls -Ssh
http://superuser.com/questions/368784/how-can-i-sort-all-files-by-size-in-a-directory
This is an OSX-only feature:
ls -l@
To show the value of extended attributes, use
xattr -l <filename>
See http://unix.stackexchange.com/questions/103114/what-do-the-fields-in-ls-al-output-mean. The first character is the file type. See http://unix.stackexchange.com/a/103117. c
means character special file, which behave like pipes and serial ports. Writing to them causes an immediate action, like displaying something on the screen or playing a sound. See http://unix.stackexchange.com/questions/60034/what-are-character-special-and-block-special-files-in-a-unix-system.
df -h
du -h DIR
du of every file/directory (including hidden ones) in current directory, sort from smallest to largest:
See http://askubuntu.com/questions/356902/why-doesnt-this-show-the-hidden-files-folders.
# du hidden files
du -sh .[!.]* * | sort -h
# Linux
date -d @1432752946.852
# Mac
date -jf %s 1446662585
date +%Y%m%d
# Linux, works on Mac too
date +%s
# Mac
date -j -f "%Y%m%d %T" "20170808 00:00:00" +%s
date --date="1 day ago" +%Y%m%d
# Mac
date +%s
date +%s%3N
TZ=America/Los_Angeles date
VAR=$((your-command-including-redirect) 2>&1)
https://stackoverflow.com/questions/3130375/bash-script-store-stderr-in-a-variable
See http://www.gnu.org/software/bash/manual/bashref.html#Redirecting-Standard-Output-and-Standard-Error.
# depending on shell, print output to console and file
./a.out 2>&1 | tee output
./a.out |& tee output
<command> &> file
https://stackoverflow.com/questions/363223/how-do-i-get-both-stdout-and-stderr-to-go-to-the-terminal-and-a-log-file https://askubuntu.com/questions/625224/how-to-redirect-stderr-to-a-file
nslookup <ip_address>
nslookup <hostname>
# $1 is the first argument to the script
value="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$1")"
echo $value
https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command
$''
causes escape sequences to be interpreted.
echo $'a\tb'
hostname
ls -l /proc/PROCESS_ID/fd | less
cat /proc/cpuinfo
cat /proc/meminfo
cat <file1> >> <file2>
See http://unix.stackexchange.com/questions/87745/what-does-lc-all-c-do.
LC_ALL=C
rsync -avr --exclude='path1/to/exclude' --exclude='path2/to/exclude' source/ destination
source/
(with trailing slash) means copy contents of source
into destination
. source
means copy source
folder itself into destination
.
https://askubuntu.com/questions/333640/cp-command-to-exclude-certain-files-from-being-copied
# copy and show progress
rsync --progress SOURCE DEST
rsync -az
-a
means "archive" mode, which preserves symbolic links, permissions, etc. -z
enables compression for the data transfer.
if ! command ; then ... ; fi
command1 && command2
command1 || command2
# question mark at end
${MY_VARIABLE?}
# `:` also checks if variable is null
# "error_message" is optional error message to print
${MY_VARIABLE:?error_message}
set -u
https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -o pipefail
https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -e
http://stackoverflow.com/questions/19622198/what-does-set-e-in-a-bash-script-mean
set -x
http://serverfault.com/questions/391255/what-does-passing-the-xe-parameters-to-bin-bash-do
# Skip first 3 GB
tail -c +3221225472 FILE | less
NOTE: This does not work on Mac/BSD bash:
# On Mac
brew install coreutils
greadlink -f <file>
# This also follows symbolic links.
readlink -f <file>
last
See http://stackoverflow.com/questions/13542832/bash-if-difference-between-square-brackets-and-double-square-brackets. [[]]
is an extension to []
and supports some extra conditional expressions.
FOO=${VARIABLE:-default}
${!parameter}
Suppose $parameter
is JAVA_HOME
, then ${!parameter}
is ${JAVA_HOME}
.
PS1
environment variable can be used to control the appearance of the Bash prompt. See http://ss64.com/bash/syntax-prompt.html.
export PS1="My simple prompt> "
# host:current_directory user$
export PS1="\h:\W \u$ "
https://superuser.com/questions/60555/show-only-current-directory-name-not-full-path-on-bash-prompt
quota -s ahsu
# If command prints nothing, no quota set.
# https://askubuntu.com/questions/703323/why-would-the-quota-command-not-work-at-all?newreg=577748f150494a27a66b070d7d421831
For an explanation of the output, see http://serverfault.com/questions/94368/understanding-quota-output
To calculate the amount of space used, multiply the number of blocks (first number) by the BLOCK_SIZE (defined in /usr/include/sys/mount.h). Reference: http://stackoverflow.com/questions/2506288/detect-block-size-for-quota-in-linux
bind -P
Esc + D
# on Mac
Esc + Backspace
Esc, Ctrl-h
nc -z -w5 <host> <port>
-z
means scan for listening daemons without sending any data.-w5
sets timeout to 5 seconds.
This will print a message if successful:
echo $? # 0 on success, 1 on failure
See http://www.ehow.com/how_5915486_tell-last-time-computer-rebooted.html.
uptime
The time after up
is how long the computer has been running without reboot
head FILE
# Option 1
$HOME/.bash_profile
# Option 2
/etc/paths.d
# System defaults are in `/etc/paths`
http://www.cyberciti.biz/faq/appleosx-bash-unix-change-set-path-environment-variable/
There should be .pc files in the PKG_CONFIG_PATH
.
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
./configure --help
Cycle through clipboard:
Ctrl + Alt + H, arrow keys, Enter
$ string="A FEW WORDS"
$ echo ${string,}
a FEW WORDS
$ echo ${string,,}
a few words
$ echo ${string,,[AEIUO]}
a FeW WoRDS
tree DIR
tree -d DIR # print directories only
# Exclude some directories
tree -I build
# http://unix.stackexchange.com/questions/61074/tree-command-for-multiple-includes-and-excludes
# Rename all *.txt to *.text
for f in *.txt; do
mv -- "$f" "${f%.txt}.text"
done
https://unix.stackexchange.com/questions/19654/how-do-i-change-the-extension-of-multiple-files
See http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash.
extension="${filename##*.}"
filename="${filename%.*}"
##
is greedy match and removal from beginning.%
matches and removes from the end.
# -o only prints matching portions, one per line
getent netgroup my_custom_netgroup | grep -o '(' | wc -l
https://stackoverflow.com/questions/16679369/count-occurrences-of-a-char-in-a-string-using-bash
tr -s ' ' | cut -d ' ' -f 4
-s
means --squeeze-repeats
, which means to replace repeated instances of the character with a single instance.
cat FILE | tr -d '[[:space:]]'
http://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable
mailx -s "Testing mailx" -a testattachment EMAIL_ADDRESS_1,EMAIL_ADDRESS_2
BODY_OF_MESSAGE
<Ctrl+D>
xinput list
lsusb
xev | grep button
# Click around in pop-up window
curl http://example.com > example.txt
Check out /etc/motd
.
tail -n +2
echo 'maps.google.com' | rev | cut -d'.' -f 1 | rev
# $NF is number of fields
hadoop fs -ls /input/part* | awk '{printf "%s ", $NF}'
https://stackoverflow.com/questions/22727107/how-to-find-the-last-field-using-cut
awk '{ print $4 " " $1 " " $5 }' FILE
See http://stackoverflow.com/questions/2129123/rearrange-columns-using-cut.
sed -i '2i#include <stdlib.h>' *
https://unix.stackexchange.com/questions/442524/how-to-prepend-a-line-to-all-files-in-a-directory
sed 's/\/$//'
See http://stackoverflow.com/questions/9044465/list-of-dirs-without-lates.
# Mac
sed -i '' '/pattern/d' ./infile
sed -i '' '/pattern/d' ./*.conf
# Linux
sed -i 's/old-word/new-word/g' *.txt
# sed per file
grep -rl PATTERN * | xargs -I% sed -i 's/old-word/new-word/g' %
sed -i 's/%{foo}/\/export\/apps\/foo/g' my_file
# Mac
# http://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux
sed -i '' 's/old-word/new-word/g' *.txt
# portable
perl -pi -w -e 's/ahsu/dalitest/g' *.job
# -p means to assume following loop around your program
LINE:
while (<>) {
... # your program goes here
} continue {
print or die "-p destination: $!\n";
}
# -i means files processed by '<>' construct are to be edited in-place
# -w prints warnings about dubious constructs
# -e used to enter a one-line program
### lowercase line
perl -pi -w -e 's/(^LINE_TO_LOWERCASE$)/lc($1)/ge'
# /e tells Perl to evaluate replacement as Perl expression first
- http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/
- http://lifehacker.com/5810026/quickly-find-and-replace-text-across-multiple-documents-via-the-command-line
See http://stackoverflow.com/a/677212/1128392.
if [ command -v java >/dev/null 2>&1 ]; then
...
fi
ls -ld [[:digit:]]*
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_03.html#sect_04_03_02
yes | ./script
http://askubuntu.com/questions/338857/automatically-enter-input-in-command-line
# Test
find ./my_dir -mtime +1
# Delete
find ./my_dir -mtime +1 -delete
ls -l /tmp | grep $USER | tr -s ' ' | cut -d ' ' -f 9 | xargs -I% rm -rf /tmp/%
for i in {a,b,c,d}; do cp HW3-Exp-1a.qry HW3-Exp-3${i}.qry; done
https://unix.stackexchange.com/questions/291065/duplicate-file-x-times-in-command-shell https://www.cyberciti.biz/faq/bash-for-loop-array/
cp -Lr FROM TO
http://superuser.com/questions/216919/how-to-copy-symlinks-to-target-as-normal-folders
Mix and match single and double-quoted strings.
'foo'"'"'bar' # foo'bar
https://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings
- Preserve literal value of all characters except
$
,\``,
\, and
!` (when history expansion is enabled). - http://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
http://stackoverflow.com/questions/13373249/extract-substring-using-regexp-in-plain-bash
echo "US/Central - 10:26 PM (CST)" | grep -oP "\-\s+\K\d{2}:\d{2}"
$ echo "US/Central - 10:26 PM (CST)" |
perl -lne 'print $& if /\-\s+\K\d{2}:\d{2}/'
# Mac
ln -hfs NEW_LOCATION EXISTING_LINK
# https://superuser.com/questions/36626/how-to-change-a-symlink-in-os-x/938865#938865
# Linux
ln -fns NEW_LOCATION EXISTING_LINK
# http://serverfault.com/questions/389997/how-to-override-update-a-symlink
$ declare -A my_arr
$ my_arr["ABC"]="abc"
$ my_arr["DEF"]="def"
$ my_arr["GHI"]="ghi"
$ echo ${my_arr[@]}
def ghi abc
$ for k in "${!my_arr[@]}"; do echo $k; done
DEF
GHI
ABC
$ for k in "${my_arr[@]}"; do echo $k; done
def
ghi
abc
${#array[@]}
http://unix.stackexchange.com/questions/193039/how-to-count-the-length-of-an-array-defined-in-bash
# Linux
df -T
mount
http://unix.stackexchange.com/questions/53313/how-to-show-the-filesystem-type-via-the-terminal
for i in {1..5}
do
echo "Welcome $i times"
done
# http://www.cyberciti.biz/faq/bash-for-loop/
for f in *; do echo $f; done
# read all files in directory into comma-separated string
a=""; for f in *.q; do a+=$f; done; echo $a
http://www.cyberciti.biz/faq/bash-loop-over-file/
# into multiple lines
echo $FOO | tr '<delimiter>' '\n'
# Example
getent netgroup NETGROUP_NAME | tr ')' '\n' | wc -l
# On whitespace
sentence="this is a story"
stringarray=($sentence)
echo ${stringarray[0]}
first_word=${stringarray[0]}
# http://stackoverflow.com/a/13402368/1128392
# Split on /
IFS='/' read -ra PARTS <<< "$A"
GROUP="${PARTS[5]}.${PARTS[6]}.${PARTS[7]}"
MODULE="${PARTS[8]}"
VERSION="${PARTS[9]}"
ARTIFACT="$GROUP:$MODULE:$VERSION"
echo $ARTIFACT
http://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization
s=$(cat << EOF
multi
line
string
EOF
)
echo "$s"
http://stevemorin.blogspot.com/2011/01/multi-line-string-in-bash.html