Score:0

How to fix this error in cp?

ht flag
#!/bin/bash

SrcDir=$1
DstDir=$2

#If DstDir is not exist, this creats it
if ! [ -a $DstDir ]; then
   mkdir $DstDir
fi

#Formats 
Vformat=(mp4 avi 3gp mpeg mkv wmv mov)
Iformat=(jpg jpeg png)

Year(){
   ls -l --time-style=full-iso $1 | tr -s '[:blank:]' | cut -d' ' -f6 | cut -d'-' -f1 | sort -u
}
export -f Year

Finder(){
   find $SrcDir -iname "*.$File"
}

#Make Year of File
for File in ${Vformat[@]}
do
   Finder $File
done 2>/dev/null | xargs -I {} -r -n1 bash -c 'Year {}' | xargs -I {} -n1 mkdir -p $DstDir/{}/videos

for File in ${Vformat[@]}
do
   Finder $File
done 2>/dev/null | xargs -I {} -r -n1 bash -c 'cp -r {} ${DstDir}/$(Year {})/videos'




cp: cannot creat regular file '/2023/videos': No such file or dir

Why ${DstDir} is not print? My programme copies the video files in a folder with the name of the year of their last change. The year function returns the year of the last file change. So why does cp give an error at the last line of the script?

Bodo avatar
pt flag
The code snippet in your question does not show that a variable `DstDir` would be set before you use it. Why do you expect `${DstDir}` to expand to a non-empty value? Please [edit] your question and add more information and the missing code parts. What input do you use and what result do you want to get?
hr flag
In `bash -c 'cp -r {} ${DstDir}/$(Year {})/videos'`, `$DstDir` is expanded by the new `bash` shell - but (unlike the function `Year`) you didn't export it
hr flag
... however I urge you to explain your end goal, so that we can suggest alternative ways to approach it
UnitedKingdom avatar
ht flag
My programme copies the video files in a folder with the name of the year of their last change. The year function returns the year of the last file change. So why does cp give an error at the last line of the script?
ru flag
Because you have spaces inside the command. Assuming the file/directory is named FUDGE, then within the single quotes you end up with this command verbatim that is processed via Bash: `cp -r FUDGE DSTDIR/YEAR FUDGE/videos`. The space between YEAR and FUDGE in my example needs to be escaped. Otherwise it's seen as an extra argument to `cp`.
Score:0
ht flag

In bash -c 'cp -r {} ${DstDir}/$(Year {})/videos', $DstDir is expanded by the new bash shell - but (unlike the function Year) you didn't export it – steeldriver

I must just export $DstDir. I'm so sorry. I'm new.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.