I am wondering if it possible to use bash to mv
multiple files with different extensions into one directory, for example
mv 1.png 2.jpg dir/
except match many files
I am wondering if it possible to use bash to mv
multiple files with different extensions into one directory, for example
mv 1.png 2.jpg dir/
except match many files
Use globs! A glob is a pattern that expands to multiple file names. It's also very nicely explained here.
For example *
matches any string of any length, and you could move all JPG and PNG files somewhere with:
mv *.jpg *.png dir
Using the extglob
option in Bash, you could also do:
shopt -s extglob
mv *(*.jpg|*.png) dir
Simplest way would be to use a mix of brace expansion and globbing
mv -iv -- *.{png,jpg} dir/
Which gets expanded to
mv -iv -- *.png *.jpg dir/
Which gets expanded to (all files -- including dirs/symlincs -- ending in .png
or .jpg
)
mv -iv -- 1.png 2.jpg ... dir/
Note: -iv --
is including for safety/verbosity
-i, --interactive
prompt before overwrite
-v, --verbose
explain what is being done
[GETOPT] The special argument "--" forces an end of option-scanning
regardless of the scanning mode.
(Meaning everything after it gets treated as an argument)
yes, it does.
$ tree
.
├── a.png
├── b.jpg
└── dir
1 directory, 2 files
$ mv a.png b.jpg dir
$ tree
.
└── dir
├── a.png
└── b.jpg
1 directory, 2 files
$
mv do not care for file extentions, all it knows is the original file and the target directory requested by you.