**/*
not recursive?
#
shopt -s globstar
Recursive chmod
#
From https://stackoverflow.com/a/11512211/8810271
Want set all files with mode 644 and all sub directories 755?
find /opt/lampp/htdocs -type d -exec chmod 755 {} \;
find /opt/lampp/htdocs -type f -exec chmod 644 {} \;
chmod 644 {} \;
specifies the command that will be executed byfind
for each file.{}
is replaced by the file path, and the semicolon denotes the end of the command (escaped, otherwise it would be interpreted by the shell instead offind
).
If Statement #
if [ $1 -gt 100 ]
then
echo Hey that\'s a large number.
fi
Single Square Brackets #
These are frequently used, and search CONDITIONAL
in bash man page for more.
Operator | Description |
---|---|
! EXPRESSION | The EXPRESSION is false. |
-n STRING | The length of STRING is greater than zero. |
-z STRING | The lengh of STRING is zero (ie it is empty). |
STRING1 = STRING2 | STRING1 is equal to STRING2 |
STRING1 != STRING2 | STRING1 is not equal to STRING2 |
INTEGER1 -eq INTEGER2 | INTEGER1 is numerically equal to INTEGER2 |
INTEGER1 -gt INTEGER2 | INTEGER1 is numerically greater than INTEGER2 |
INTEGER1 -lt INTEGER2 | INTEGER1 is numerically less than INTEGER2 |
-d FILE | FILE exists and is a directory. |
-e FILE | FILE exists. |
-r FILE | FILE exists and the read permission is granted. |
-s FILE | FILE exists and it's size is greater than zero (ie. it is not empty). |
-w FILE | FILE exists and the write permission is granted. |
-x FILE | FILE exists and the execute permission is granted. |
From https://stackoverflow.com/a/31366734/8810271
[
is just a regular command with a weird name.
]
is just an argument of[
that prevents further arguments from being used.Nothing is altered in the way that Bash parses the command. In particular,
<
is redirection,&&
and||
concatenate multiple commands,( )
generates subshells unless escaped by\
, and word expansion happens as usual.
[[ X ]]
is a single construct that makesX
be parsed magically.<
,&&
,||
and()
are treated specially, and word splitting rules are different. There are also further differences like=
and=~
.
[
is a built-in command (compgen -b
), and[[
is a keyword (compgen -k
)
That's why you need to type spaces after [
and before ]
. Furthermore, there is no need to type an entire if statement to debug a condition. Just type [[ 0 > 1 ]]
and see your shell prompt (if configured) !
Listing awfully named files in order #
Sometimes, you get files like photo_1.jpg
, photo_2.jpg
, ..., photo_10.jpg
, ...
If using plain *.jpg
, you will get photo_1.jpg
, photo_10.jpg
, ..., photo_2.jpg
, ...
Quite annoying, isn't it? Solve it by ls -v
, namely sort by version.
From man page:
Sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)