Home > database >  How to move all folders in a directory with mv and bash variables
How to move all folders in a directory with mv and bash variables

Time:04-29

I wanted to take paths to directories from files that we got from a user as parameters of function and from files extract the paths and move all files and folders in the source directory to the destination directory, but something went wrong: It writes to me, that "No such file or directory"

With this input

enter image description here

Where dest_adr.txt contains this path: C:\Users\Michal\Desktop\tmp and source_adr.txt contains this path: C:\Users\Michal\Desktop\test\

#!/bin/bash

FILE_WITH_ADRESS_TO_BLENDER_PATH=$1
FILE_WITH_ADRESS_TO_FOLDER_WITH_ADDONS=$2

function move_folders(){    
    mv "${PATH_TO_FOLDER_WITH_ADDONS_}"/* "${PATH_TO_BLENDR_DIRECTORIE_}"/
}   

if [  $# -eq 0 ]
  then
      PATH_TO_BLENDR_DIRECTORIE="C:/Program Files/Blender Foundation/Blender 3.0/3.0/scripts/addons"
      FOLDER_WITH_ADDONS="none"
      echo $PATH_TO_BLENDR_DIRECTORIE
      echo $FOLDER_WITH_ADDONS
  else  
      PATH_TO_BLENDR_DIRECTORIE_=$(cat $FILE_WITH_ADRESS_TO_BLENDER_PATH | sed -e 's/\\/\//g' -e 's/\C://g')
      PATH_TO_FOLDER_WITH_ADDONS_=$(cat $FILE_WITH_ADRESS_TO_FOLDER_WITH_ADDONS | sed -e 's/\\/\//g' -e 's/\C://g')
      echo $PATH_TO_BLENDR_DIRECTORIE_
      echo $PATH_TO_FOLDER_WITH_ADDONS_
      move_folders
  fi

CodePudding user response:

Use Shell Parameter Expansion

adrian@pc:/tmp/move> path_to_file=/usr/share/man/man1/bash.1.gz
adrian@pc:/tmp/move> folder_path=${path_to_file%/*} 
adrian@pc:/tmp/move> echo $folder_path 
/usr/share/man/man1
adrian@pc:/tmp/move> file_name=${path_to_file##*/}
adrian@pc:/tmp/move> echo $file_name 
bash.1.gz

Don't use cat to send variable, it will send content of file no path, use echo or printf instead.

CodePudding user response:

The error occurs because the test/ directory is empty.

Try again with files within it. It's expected behavior to fail with a wildcard if you don't have files in the source directory you are trying to move.

Edit: Or use mv -r

  • Related