orglinks.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. #
  3. # ChatGPT Promt:
  4. #
  5. # write me a bash script that finds all the .org files under $HOME and
  6. # hard links them to ~/orgfiles/, changing the "/" in paths to "__"
  7. # This is bash, be safe
  8. set -u
  9. set -e
  10. # use my info,warn,error aliases
  11. source ~/lib/bash/bashutils.sh
  12. # Define source and destination directories
  13. source_dir="$HOME"
  14. destination_dir="$HOME/orgfiles"
  15. # Max hard links to make. If I have more than 10k org files, there are problems.
  16. MAXFILES=10000
  17. # Create the destination directory if it doesn't exist
  18. mkdir -p "$destination_dir"
  19. # Find all .org files under $HOME and loop through them
  20. find "$source_dir" -type f -name "*.org" | grep -v "$destination_dir" | head -$MAXFILES | while read -r org_file; do
  21. # Get the relative path by removing the source directory part
  22. relative_path="${org_file#$source_dir/}"
  23. # Replace "/" in paths with "__"
  24. modified_relative_path1="${relative_path//\//__}"
  25. # Replace " " in paths with "_"
  26. modified_relative_path="${modified_relative_path1// /_}"
  27. # Construct the destination path
  28. destination_path="$destination_dir/$modified_relative_path"
  29. # if the target file exists, continue
  30. if [ -f $destination_path ] ; then
  31. continue
  32. fi
  33. # Attempt to create a hard link to the destination
  34. if ln "$org_file" "$destination_path" 2>/dev/null; then
  35. info "Linked file: $org_file -> $destination_path"
  36. else
  37. warn "Failed to create a hard link for $org_file to $destination_path"
  38. fi
  39. done