User Tools

Site Tools


bash

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
bash [2022/03/01 10:18] – created rjtbash [2024/01/10 10:59] (current) – Move all files in subdirectories to parent rjt
Line 2: Line 2:
  
 The **__B__ourne __A__gain __Sh__ell** is the default command language / shell in [[Linux]], and I've never bothered to try any other. You type stuff in (or paste it from stackexchange...) and useful stuff happens. The **__B__ourne __A__gain __Sh__ell** is the default command language / shell in [[Linux]], and I've never bothered to try any other. You type stuff in (or paste it from stackexchange...) and useful stuff happens.
 +
 +===== Features =====
 +
 +==== Arguments ====
 +
 +You can use [[getopts]] to read arguments attached to the command. You use a ''while'' loop and a ''case'' statement to figure out what to do with the arguments/flags entered.
 +
 +In this example you can use ''-i'' and ''-o'' arguments to specify file names and they will be stored in variables ''input'' and ''output''.
 +
 +<code bash>
 +while getopts :io flag
 +do
 + case $flag in
 + i) input=${OPTARG} ;;
 + o) output=${OPTARG} ;;
 + esac
 +done
 +</code>
 +
 +==== Conditional Constructs ====
 +
 +=== if ===
 +
 +
 +-n string True if the length of string is non-zero. -a file True if file exists. DPMT USE -a, it's weird
 +
 +In the documentation of test you will also see a the switch -e. This switch tests the following argument and evaluates to true if that argument is a file or directory that exists. More useful still is the -f switch which evaluates to true if the following argument exists and is a regular file (as opposed to a directory or a block device, or whatever).
 +
 +=== case ===
  
 ===== Examples ===== ===== Examples =====
Line 9: Line 38:
   * https://askubuntu.com/questions/1107016/how-to-change-names-of-files-in-a-directory-to-random-generated-names   * https://askubuntu.com/questions/1107016/how-to-change-names-of-files-in-a-directory-to-random-generated-names
  
-<code>+<code bash>
 for file in *.jpeg; do for file in *.jpeg; do
  mv -- "$file" "$RANDOM.jpeg"  mv -- "$file" "$RANDOM.jpeg"
Line 15: Line 44:
 </code> </code>
  
-<code>+<code bash>
 for file in *.jpeg; do for file in *.jpeg; do
  mv -- "$file" "$(mktemp --dry-run XXXXXXXX.jpeg)"  mv -- "$file" "$(mktemp --dry-run XXXXXXXX.jpeg)"
 done done
 </code> </code>
 +
 +==== Move all files in subdirectories to parent ====
 +
 +  * https://unix.stackexchange.com/questions/358284/move-all-files-inside-sub-folders-to-parent-folder
 +
 +<code bash>
 +find . -mindepth 2 -type f -print -exec mv {} . \;
 +</code>
 +
 +==== See Also ====
 +
 +Bash examples elsewhere on the wiki:
 +
 +  * [[ffmpeg]]
 +  * [[imagemagick]]
 +  * [[tumbler]]
  
 ===== See Also ===== ===== See Also =====
bash.1646090307.txt.gz · Last modified: 2022/03/01 10:18 by rjt