Score:0

move files that match file names in another directory

in flag

I have two directories: dir_1 which has a.mp4, b.mp4, c.mp4, d.mp4 and dir_2 which has only a.txt, b.txt. The script should move a.mp4 and b.mp4 to dir_3 because only these file names match the file names in dir_2. I find it to hard follow the scripts when I need to do these type of particular operations.

Score:2
in flag

Try,

for f in "dir_2"/*; do
    filename=${f##*/}
    mv -t "dir_3" "dir_1/${filename%.*}".*
done
  • Loop files in dir_2 (use *.txt to only loop these files)
  • ${f##*/} gives you the file name without the path.
  • ${filename%.*} gives you the filename without extension.
  • mv -t "dir_3" "dir_1/${filename%.*}".* moves all files with given filename (cleared from the extension) to dir_3. You can specify .mp4 instead of .* if you want.
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.