Saturday, November 24, 2018

Memos on shell commands


  1. To see which configuration file the shell on your Mac is using, just type "echo $SHELL" in the terminal.
  2. To show all environmental variables: printenv
  3. Check the file type: file -I filename
  4. To see the symboilc link, use "-la" option of ls command, i.e. ls -la.
  5. 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/
  6. 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)
  7. To copy multiple files using cp command, use like:
    cp directory/{file1, file2} or you can even do like file{1,2}.
  8. Similar sequential command; the following will produce folder_1 to folder_9:
    mkdir ./folder_{1..9}
  9. "clear" to clear all terminal output.
    (not verified on macosX's terminal)
  10. memory status check
    ps aux --sort -rss
  11. Loop variables
  12. for databaseName in a b c d e f; do
      # do something like: echo $databaseName
    done 
  13. 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
  14. For sed, we can use "|" as separator instead of "/".
  15. 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"]
  16. 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
  17. To do multiple substitutions at one time, use throw replacement conditions with "-e" option as in:
    sed -e 'condition1' -e 'condition2'
  18. Show ls command's output as one file per one line:
    either "ls -1" or "ls | cat"
  19. String's length in shell script:
    echo ${#str}
    where str is variable defined in advance.
  20. 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
  21. 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/
  22. 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.
  23. 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.
  24. To strip \n line feed characters,
    tr -d '\n' < yourfile.txt
  25. Put the pattern in single quotes and enable Extended Regular Expression support with -E.
  26. 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)
  27. 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
  28. To count the total number of lines in a file
    wc -l file
    https://devconnected.com/how-to-count-files-in-directory-on-linux/
  29. To show 5 lines(in the 'B'efore text) that are at and after a line matching 'REGEX':
    grep -B 5 REGEX
  30. To check if a file exists,
    if [ -f FILEPATH ]; then SOME_ACTION...
    or
    [ -f FILEPATH ] && SOME_ACTION
    Symbol "[" is an alias for "test" command.
  31. Major jq commands summary:
    https://www.howtogeek.com/529219/how-to-parse-json-files-on-the-linux-command-line-with-jq/
  32. 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
  33. To count the number of characters of a string
    echo -n $YOUR_STR | wc -m
    -n option is to strip new line char.
  34. To show a character in a certain position,
    echo "SAMPLETEXT" | cut -c3
  35. 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
  36. check the size of a folder
    du -hs <path-to-file>






No comments:

Post a Comment

Writing a post in Markdown in Blogger

Following articles are helpful. * [【令和最新版】Blogger で Markdown を用いて記事を書く方法](https://qiita.com/hoge_fu-ga/items/267ea5ad4f8c120eaeb8) * [Bl...