简体   繁体   中英

Bash : Moving files according to Name

Heyo guys

So I want to move all the files containg the name "American" and "Dad" into its according folder in the "TV shows" folder. I thought I had a solution to this but in the end it didn't work out. I got too many arguments because of the "*" in the if argument (at least I think that's the problem).

Can I somehow get past the "too many arguments" ? Should I try a whole different approach ?

Thanks in advance

Here's an extract of the code

if [ -d *Game* ] && [ -d *Thrones*]; then
    game=( $(find *Game* -maxdepth 0))
    mv ${game[@]} ../TV\ Shows/Game*/*s04

elif [ -d *American* ] && [ -d *Dad*] ] ; then
    dad=( $(find *American* -maxdepth 0))
    mv ${dad[@]} ../TV\ Shows/American*/*.9

The problem is indeed that [ -d *Game* ] is the same as [ -d "Game_of_Thrones_S04E01.mkv" "Game_of_Thrones_S04E02.mkv" ... ] which is invalid.

If you want to move all files matching *Game* or *Thrones* , you can instead do:

shopt -s nullglob      # Handle the case of having no matches
# shopt -s nocaseglob  # Uncomment to match case insensitively

for f in *Game* *Thrones*
do
  mv "$f" "../TV Shows/"Game*/*s04
done

for f in *American* *Dad*
do
  mv "$f" "../TV Shows/"American*/*.9
done

This should be closer to what you want:

find *Game* *Thrones* -maxdepth 0 -exec mv {} ../TV\ Shows/Game\ of\ Thrones/ \;
find *American* *Dad* -maxdepth 0 -exec mv {} ../TV\ Shows/American\ Dad/ \;

I removed the * from the destination of your mv commands because that most probably doesn't do what you expect. The mv command will move all the specified names into the last argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM