The which command executable cannot find shell alies and shell builtins (such as cd). To see those, use:
type -a some-alias-or-shell-bultin
Most of the time this is stored in the /etc/passwd file so the following would work:
grep username /etc/passwd
However, when some kind of directory service is used the information may not reside there. So, the following always works:
getent passwd username
This is explained wonderfully in this Unix stackexchange post.
TL;DR:
Below are some examples to build up intution:
$ ((1+1)) $ echo $? 0 $ ((1-1)) $ echo $? 1 $ if ls > /dev/null ; then echo 'a'; else echo 'b'; fi aSome other examples:
$ touch foo ; if [ -e foo ] ; then echo 'a'; else echo 'b' ; fi a $ rm -f foo ; if [ -e foo ] ; then echo 'a'; else echo 'b' ; fi b $ if ((1+2>2)); then echo 'a'; else echo 'b'; fi aThe below prints a because the assignment succeeds and so the condition for the if statement is evaluated to true:
$ a=0; if [ a=1 ]; then echo 'a'; else echo 'b'; fi a… the correct way is to use arithmetic evaluation:
$ a=0; if (( $a==1 )); then echo 'a'; else echo 'b'; fi bThe below prints nothing as the assignment was done in a subshell:
$ ( b=1 ); echo $b
set
print only the shell variables, not the functions(set -o posix; set)
env
vs. printenv
vs. set
env
is POSIX, printenv
isn't. Other than that,
in my system env
and printenv
yield identical output.
set
however is a different beast and provides
more comprehensive output as it includes both shell-local variables
(since it's a shell builtin it can see them) and environment variables
which are passed on to every executed program. env
however
is a program and so can only see environment variables, not shell-local
variables (source)
BTW, the fact that set
is a built-in can be established
by the fact that which set
produces no output.
${variable}
for variables$((expression))
for arithmetic expressions$(command)
for command output substitutiontools/sleep-until-modified.sh
In the following manner, e.g. to invoke make
when a LaTeX file changes:
while sleep-until-modified.sh my-awesome-document.tex ; do make ; doneNB:Unfortunately, the above way does *not* work for more than one file. So, use the second method if practicable:
when-changed
utilityInstall with:
sudo pip install when-changed
… or as I did more recently following the instructions from the github repo:
pip install https://github.com/joh/when-changed/archive/master.zip
Use like:
when-changed esa-sre-gedais-sc-0001.tex *.input -c makeNotice that, using this method, more than one file can be tracked.
More recently, I also did:
when-changed $(find . -iname \*.java) -c "ant clean-except-ivy && ant dry-compile"… which exemplifies monitoring a truly arbitrary set of files and executing two commands.
sudo
with the passwordemacs ~/.easy.install
sudo
with -S
:echo <password> | sudo -S do-your-shit
sudo chmod +x ~/.easy.install
sudo chmod 700 ~/.easy.install
find
when there are spaces in the filenamesfor
iteration with a while
iteration
that is line-based as opposed to word-based:
find . -iname Thundercats\ -\ 10\* | while read f; do sudo cp "$f" /media/mperdikeas/thundercats-season-1/; done
scp
that requires a password in a script filesshpass
as shown below:
sshpass -f $PASS_FILE scp rawdar.zip user@$DEST:~/rawdar.warIf, after the copy, you also need to
ssh
to the destination and you further need sudo privilleges,
sshpass
can't supply both passwords, so you need to put the action that requires sudo in
a script and execute it. E.g. as shown below:
sshpass -f $PASS_FILE ssh user@$DEST <<'ENDSSH' ./publish-rawdar.sh ENDSSH... where the script at the
ssh
destination is:
user@user-virtual-machine:~$ cat publish-rawdar.sh #!/usr/bin/env bash readonly ECHO=/bin/echo $ECHO 'top-secret-password' | sudo -S cp rawdar.war /var/lib/tomcat7/webapps user@user-virtual-machine:~$Relevant excrepts from functional file:
#!/usr/bin/env bash readonly PASS_FILE=~/esac.neuropublic.pwd readonly DEST=172.31.148.22 rm -f rawdar.zip && zip -r rawdar.zip rawdar.html rawdar.org.files/ sshpass -f $PASS_FILE scp rawdar.zip user@$DEST:~/rawdar.war sshpass -f $PASS_FILE ssh user@$DEST <<'ENDSSH' ./publish-rawdar.sh ENDSSH
for f in $(find . -iname \*.edds); do printf "%s ==> " $f && od -j 34 -N 2 -A x -t x1z -v $f | cut -c7-30 | head -1 ; done
tr "\\n" " "
findinjava fromValue | sort | awk '{print $1}' | rev | cut -c 2- | rev | tr "\\n" " "
echo foobar | rev | cut -c (n+1)- | rev
$ mkfifo npipe $ ls npipe prw-r--r-- 1 mperdikeas users 0 Mar 11 14:12 npipe| $ echo "boo" > npipe & [1] 22271 $ cat < npipe boo [1]+ Done echo "boo" > npipe
find . -type f -iname \*.java -print0 | xargs -0 sed -i 's/XMLUtils.getString/XMLUtils.getStringNew/g'
#!/usr/bin/env bash for file in $(find . -type l); do if [[ -h "$file" ]]; then echo "file $file is a symlink" if [[ "$(readlink -f "$file")" == "$(realpath repo-wide-libs/ant-contrib-1.0b3.jar)" ]]; then ln -sf "$(dirname "$(readlink "$file")")"/ant-contrib.jar "$(dirname "$file")" && rm -f "$file" fi fi donesource
find . -type f -exec sed -i 's/foo/bar/' {} +source To correctly escape the string passed to `sed` use my `sedescape` function (lives in .bashrc).
screen -x
screen -dRR
find . -iname *.java -print0 | wc -l --files0-from=-
find . -type d -execdir touch {}/foo \;NB: On why {}/foo is needed, see: this SO post.
du -ch .
scp -rp 'mperdikeas@192.168.2.2:~/"mperd-digital camera images"' /home/mperdikeas/
awk '{gsub("/","."); print}'
"$1"
to pass an argument to an underlying program
instead of just $1
. See: ~/playground/java/why-I-should-always-quote-arguments for more.
~/playground/patch/Makefile
Makefile, also shown below:
patch: -diff -u b a > b2a.patch patch -o b.corrected b < b2a.patch diff a b.corrected clean: rm -f b2a.patch b.correctedHowever, this idiom will ask mask real problems (such as using -dif instead of -diff. Therefore the correct way to solve this conundrum (i.e. to not don't ignore the exit value, but also to not fail if it's non-zero) is shown in
/home/mperdikeas/playground/ant-patch/02/Makefile
file, also shown below:
patch: FORCE diff -u src/a/random/path/b b.corrected > b2a.patch; [ $$? -eq 1 ] ant diff src/a/random/path/a src/a/random/path/b.corrected clean: rm -f b2a.patch src/a/random/path/b.corrected FORCE:The ability to use the following syntax:
; [ $? -eq 1 ]
is plain bash (the double '$' in the
Makefile is to account for Make's escaping conventions), as is shown in the following bash examples:
$ echo $[3+2] 5 $ [ $[3+2] -eq 5 ] $ echo $? 0 $ [ $[3+2] -eq 6 ] $ echo $? 1
diff <(ls) <(ls)Named pipes can be created with mkfifo:
mkfifo foo find ~ > foo & tail -f fooThe difference of the above to creating a file is that the size of a named pipe is 0. A named pipe lives on the filesystem and survives a reboot but never holds any data, only metadata.
$ type emacs emacs is aliased to `emacs -nw'(or the more low tech: alias | grep emacs)
diff -r --exclude=.svn dir-a dir-b
grep -c new `find . -name '*.java'`
wc -l `find . -name '*.java'`
find . -name '*.java' | xargs wc -l
$script session.log
for i in `seq 1 10`; do echo "number is: " $i ; done
gksudo software-center > trace 2>&12>&1 means "put the output of channel 2 (stderr) to where channel 1 (stdout) currently goes" which is why the redirection to file has to occur before the stderr redirection to stdout.
for f in $(find src/ -type f -iname \*.java) ; do sed -i '1ipackage _int.esa.esavo.harvesting;' $f ; done
find -type f -print0 | xargs -r0 stat -c %y\ %n | sortsource
find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "
diff --ignore-matching-lines='^//' Foo.java
echo -n foo | sha256sumThe -n is necessary as otherwise echo will also sent the new line character to be SHA256-encoded as part of the password. In cases of salting the usually arrangement is a simple concatenation of the salt followed by the password (in that order so the cracker doesn't have an attack vector). So if password is "foo" and salt is "salt":
echo -n saltfoo | sha256sum
#!/bin/bash scp VatRefund.ear root@node-testapp1:/var/tmp/ || exit 1 cat <<OEF | ssh node-testapp1 || exit 1 cd /opt/jboss/jboss-eap-6.0/bin ./jboss-cli.sh --connect controller=123.30.228.54:9999 undeploy --server-groups=other-server-group VatRefund.ear deploy --server-groups=other-server-group /var/tmp/VatRefund.ear OEF exit 0
find . -iname *.script -type f -printf '%T@ %p\n' | sort -k 1nr | sed 's/^[^ ]* //' | head -n 10
nc mail.semantix.gr 25This method ensures that nothing's stored anywhere.
sudo apt-get install autojump
find . -iname \*.ini -exec grep '^[^#].*#' /dev/null '{}' ';'
find . -iname \*.csv -exec sh -c "grep foo {} && echo {}" \;
find $(pwd) -iname report2.jrxml
find . -iname \*.java | grep facade | while read f ; do file $f ; doneIn such, cases do a:
find . -iname \*.java | /bin/grep facade | while read f ; do file $f ; done
cp -pr -L --copy-contents
cd srcPath && find . -type d -exec mkdir -p /destPath/'{}' ';'
cd srcPath && find . ! -type d -exec sh -c 'cat "{}" > /destPath/"{}"' \;
strace -f ./NeuroCode.exe Project.xml 2>&1 | lessOnce in the output of less, you can use a / to search for NeuroCode.exe and find the system calls it makes.
ps aux | grep '[t]erminal'will return all lines containing terminal which the line you invoked does not.
jar tvf Orders-ejb.jar | grep entities| cut -c38- | grep -v /$ | sed 's,^,import ,' | sed 's,/,.,g'(in the above example at the sed command the comma character is used as the separator - this is handier than the usual slash character when one has to replaces slashes)
find . -iname \*.java -print0 | wc -l --files0-from -
find . -exec file {} \; | awk -F/ '{print $NF}' | awk -F: '{print $NF}' | sort | uniq
find . -iname \*.xml -exec grep "login-module" '{}' /dev/null \;The following simpler version also seems to work:
find . -iname *.xml -exec grep "login-module" {} /dev/null \;
find jboss-as-7.1.1.Final/ -iname *.jar | awk -F/ '{print $NF}'
find . -iname *.jar | while read JARF; do jar tvf $JARF | grep a/b/c/D.class && echo $JARF ; doneif there is too much output, try:
find /home/mperdikeas/git-jboss/ -iname *.jar | while read f ; do echo $f && (jar tvf $f | grep a.b.c.D) ; done | grep -B1 ^\("." instead of "/" works practically as well as it stands for any character).
awk 'NF==6;{}' 3.ply | wc -l
mperdikeas@mperdikeas ~ $ dd if=/dev/zero of=a_file_of_3MBs bs=1M count=3 3+0 records in 3+0 records out 3145728 bytes (3.1 MB) copied, 0.00516917 s, 609 MB/s mperdikeas@mperdikeas ~ $ dd if=/dev/zero of=a_file_of_1453bytes bs=1 count=1453 1453+0 records in 1453+0 records out 1453 bytes (1.5 kB) copied, 0.0184214 s, 78.9 kB/s
tar cf - wa-integration/ | (cd /c/BW/ ; tar xvf - )
find directory_name -type f | wc -l
for i in `find ./sample/`; do grep -H "sample string" $i; done
curl -HAccept:text/plain http://example.com/basePUT:
curl -XPUT -HContent-type:text/plain --data "stuff:morestuff" http://example.com/base?param=valDELETE:
curl -XDELETE http://example.com/base/user/123POST:
curl -d "param1=value1¶m2=value2" http://example.com/base/
Another curl tip: To send a POST request whose data is not in application/x-www-urlencoded format, use the --data-binary option instead of --data.
find folderToSearch/ | cut -c 73-
$ cat nice.sh tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed ${1}qWhat it does:
Given the above script the following text:
"Tests a distributed system made up of two LEON boards connected with the spacewire, and each one of them having a local FPGA component. The code is clear; the bug reports on Mantis"
becomes (when piped with: echo "(the above text)" | nice.sh 5) :
3 the 2 of 2 a 1 with 1 up
scp -r mperdikeas@elrond:~/TAPKIT/Conversions/Genesis/sources-auto /cygdrive/c/fromElrondTo upload single file to remote directory:
scp buildSpecification.py mperdikeas@elrond:~/TAPKIT/Conversions/Genesis/pythonscp can be setup to not prompt for password using ssh-keygen and appending the public key to the remote host(s)'s ~/.ssh/authorized_keys file (for the respective user).
date ; time find / > /dev/null 2>&1; date
dpkg -P mysql-server
ps auxf('f' stands for 'full')
pwd -P