fiticida
fiticida

Reputation: 694

Moving files containing spaces with mv command

I have the following input.

Entities can be 'MG SOCRATES' 'VALIND' 'SUSBTECT'

This entities have the following directories.

/home/tca/git/tca/<entity>

Example :

/home/tca/git/tca/MG SOCRATES
/home/tca/git/tca/VALIND
/home/tca/git/tca/SUSBTECT

In each directory I have files with this pattern something_or-something.

For example:

/home/tca/git/tca/SUSBTECT/asdsad2018-01-01-2018-12-31sdadsda
/home/tca/git/tca/SUSBTECT/asdsad2018-01-01_2018-12-31sdsadadsda

I want to move them to a subdicretory for each entity with the following structure.

I have for example this subdirectory. /home/tca/git/tca/SUSBTECT/2018-01-01_2018-12-31

What I want to do is to move from /home/tca/git/tca/SUSBTECT/ all files that matches the pattern _or- to the subdirectory.

My code does it correctly but fails for entity 'MG SOCRATES' because there is a space that moves command can not interpretate.

My code:

    entity_path="$entity_path""/*"$file_start"*"$file_end"*"
    echo `mv -t  "$path" $entity_path`

Where $entity_path all files matching the pattern and $path is the directory where I want to move my files.

I think is a problem about the spaces.

Any idea?

Upvotes: 0

Views: 864

Answers (1)

chepner
chepner

Reputation: 531125

There is no way to have a regular parameter expansion that undergoes pathname expansion, but not word-splitting, so your attempt to put the pattern in entity_path is going to fail. Either use the pattern directly,

mv -t "$path" "$entity_path"/*"$file_start"*"$file_end"*

or store the result of the pathname expansion in an array.

entities=( "$entity_path"/*"$file_start"*"$file_end"* )
mv -t "$path" "${entities[@]}"

Upvotes: 2

Related Questions