- To see which configuration file the shell on your Mac is using, just type "echo $SHELL" in the terminal.
- To show all environmental variables: printenv
- Check the file type: file -I filename
- To see the symboilc link, use "-la" option of ls command, i.e. ls -la.
- tee command "tee command reads the standard input and writes it to both the standard output and one or more files.": https://www.geeksforgeeks.org/tee-command-linux-example/
- The contents enclosed by a pair of back-quotation "`" will be (together with back-quotations themselves) replaced by the output of the command within.
So, if one wants to output a log file giving its name datetime part, he/she can write:
log_'date +%Y-%M-%'
(not verified on macosX's terminal) - To copy multiple files using cp command, use like:
cp directory/{file1, file2} or you can even do like file{1,2}. - Similar sequential command; the following will produce folder_1 to folder_9:
mkdir ./folder_{1..9} - "clear" to clear all terminal output.
(not verified on macosX's terminal) - memory status check
ps aux --sort -rss - Loop variables
for databaseName in a b c d e f; do # do something like: echo $databaseName done- Strings enclosed by single quotes will be treated literally, while those by double quotes will be expanded at variables contained in it:
str=efg
sed "s/def/$str/" somefile - For sed, we can use "|" as separator instead of "/".
- To overwrite file content by sed, we need "-i" option to let sed take a backup:
sed -i.bak s/ABC/DEF somefile
[This will produce "some file.bak"] - To save a sed output, piping the result to tee as(https://unix.stackexchange.com/questions/283407/can-sed-save-its-output-to-a-file):
sed bla bla bla | tee file_name
or
sed bla bla bla > file_name - To do multiple substitutions at one time, use throw replacement conditions with "-e" option as in:
sed -e 'condition1' -e 'condition2' - Show ls command's output as one file per one line:
either "ls -1" or "ls | cat" - String's length in shell script:
echo ${#str}
where str is variable defined in advance. - String replacement: ${str[@]/be/eat} will replace first occurrence of "be" with "eat".
To replace the pattern repeatedly, double the first slash: ${str[@]//be/eat}
https://www.learnshell.org/en/Basic_String_Operations - To check if a bash variable is empty:
if [ -z "$var" ];
https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/ - To parse json files, use jq command:
https://linuxconfig.org/how-to-parse-a-json-file-from-linux-command-line-using-jq
Use -r(or --raw-output) option to remove quotations. - In some cases of piping, one need additional command such as xargs:
ls _directory_ | xargs mkdir
will make folders whose names match the result of ls command. - To strip \n line feed characters,
tr -d '\n' < yourfile.txt - Put the pattern in single quotes and enable Extended Regular Expression support with -E.
- In sed's substitution, \n where n is any number points to n-th group(enclosed by round brackets) in the pattern expression to be replaced.
sed 's:start_(a.*z)_end:\1:'
will replace "start_(a****z)_end" with (a****z) - To insert a new line by sed, one can use '$'\n''
sed 's:PATTERN:\'$'\n'':' file
https://stackoverflow.com/questions/6111679/insert-linefeed-in-sed-mac-os-x/24299845#24299845 - To count the total number of lines in a file
wc -l file
https://devconnected.com/how-to-count-files-in-directory-on-linux/ - To show 5 lines(in the 'B'efore text) that are at and after a line matching 'REGEX':
grep -B 5 REGEX - To check if a file exists,
if [ -f FILEPATH ]; then SOME_ACTION...
or
[ -f FILEPATH ] && SOME_ACTION
Symbol "[" is an alias for "test" command. - Major jq commands summary:
https://www.howtogeek.com/529219/how-to-parse-json-files-on-the-linux-command-line-with-jq/ - To get all keys which are arranged directly under the top round bracket, or on the top of the structure.
jq '. | keys' JSON_FILEPATH
For keys that are contained in an item "item1",
jq '.item1 | keys' JSON_FILEPATH - To count the number of characters of a string
echo -n $YOUR_STR | wc -m
-n option is to strip new line char. - To show a character in a certain position,
echo "SAMPLETEXT" | cut -c3 - To check the bit depth of an image:
https://superuser.com/questions/148661/how-can-i-find-out-if-a-png-file-is-24-bit-color-or-32-bit-color-on-mac-os-x - check the size of a folder
du -hs <path-to-file>
Saturday, November 24, 2018
Memos on shell commands
Subscribe to:
Post Comments (Atom)
Writing a post in Markdown in Blogger
Following articles are helpful. * [【令和最新版】Blogger で Markdown を用いて記事を書く方法](https://qiita.com/hoge_fu-ga/items/267ea5ad4f8c120eaeb8) * [Bl...
-
Build in Release Go to "Product" menu, "Scheme" and "Edit Scheme". Select "Run" from the list...
-
OpenCV implements various key point detectors and descriptors as subclasses of features2d class. But it is not clear at once whether such...
-
I installed pyenv and a version of python on it, but executing "brew info opencv" keeps telling me that I don't satisfy depe...
No comments:
Post a Comment