Home ยป How can I move all non-hidden .jpeg files using shell expansion characters?

How can I move all non-hidden .jpeg files using shell expansion characters?


You can use a shell pattern with [ ] character classes.

To move each file (and folder)

  • that is located directly in the present working directory, and
  • whose name does not start with a . (i.e., the file is not hidden), and
  • whose name ends with .jpg or one of its seven other case variants (like .JPG or .Jpg)

to /tmp, you can use the command:

mv -- *.[Jj][Pp][Gg] /tmp
  • This uses character classes to match any files whose names end in ., then J or j, then P or p, then G or g. Character classes (with [ ]) use more compact syntax than brace expressions ({ }), and also have the benefit that they only expand to names of files that actually exist, so you won’t get error messages even if some case variants of .jpg don’t appear at the end of any filenames. (You’ll still see an error message if no files are matched.)
  • When the shell expands patterns, it excludes files that begin with . by default, so nothing special has to be done to achieve that.
  • The -- option ensures mv interprets everything after it as a filename and not as an option. Thus even if the pattern matches files whose names start with -, they will be treated as literal filenames and not options to mv. (This could alternatively have been achieved by prefixing the pattern with ./, i.e. mv ./*.[Jj][Pp][Gg] /tmp.)

You can enable the nocaseglob shell option and give a simple pattern.

An alternative way is to turn on the nocaseglob shell option, which causes filename patterns to matched case-insensitively. Since you likely don’t need to change this setting for subsequent commands, you can surround the commands that enable nocaseglob and move the files in parentheses so they run in a subshell, so that the calling environment is unaffected:

(shopt -s nocaseglob; mv -- *.jpg /tmp)

Since jpg is only three characters long, you may prefer the above method with character classes. However, for case-sensitive globbing of longer words, it becomes quite handy to be able to set nocaseglob.

What happens if there are files with the same name already in /tmp?

However you decide to specify the pattern of files to be moved, if a file with the same name as one of the files being moved already exists in /tmp, then mv‘s default behavior will be followed, as we have not given mv any options about how to handle a collision. That is to say:

  • If the pre-existing destination file has write permissions for the user running mv, it will be automatically and silently overwritten by the source file.
  • If the pre-existing destination file does not have write permissions for the user running mv, then mv will prompt the user and ask what to do. (Overwriting the file might still succeed in this case, if the user owns the pre-existing destination file.)

To specify different behavior when the destination file already exists, you can use the --backup, -b, -f, -i, -n, or -u options, as documented in:

  • man mv and
  • 11.4 mv: Move (rename) files (in the GNU coreutils reference manual).


mv ./*.{j,J}{p,P}{g,G} /path/to/folder/

or alternatively

mv "$(pwd)"/*.{j,J}{p,P}{g,G} /path/to/folder/

Related Solutions

How can I pass a command line argument into a shell script?

The shell command and any arguments to that command appear as numbered shell variables: $0 has the string value of the command itself, something like script, ./script, /home/user/bin/script or whatever. Any arguments appear as "$1", "$2", "$3" and so on. The...

What is pointer to string in C?

argv is an array of pointers pointing to zero terminated c-strings. I painted the following pretty picture to help you visualize something about the pointers. And here is a code example that shows you how an operating system would pass arguments to your...

How do mobile carriers know video resolution over HTTPS connections?

This is an active area of research. I happen to have done some work in this area, so I'll share what I can about the basic idea (this work was with industry partners and I can't share the secret details ๐Ÿ™‚ ). The tl;dr is that it's often possible to identify an...

How do I change the name of my Android device?

To change the hostname (device name) you have to use the terminal (as root): For Eclair (2.1): echo MYNAME > /proc/sys/kernel/hostname For Froyo (2.2): (works also on most 2.3) setprop net.hostname MYNAME Then restart your wi-fi. To see the change, type...

How does reverse SSH tunneling work?

I love explaining this kind of thing through visualization. ๐Ÿ™‚ Think of your SSH connections as tubes. Big tubes. Normally, you'll reach through these tubes to run a shell on a remote computer. The shell runs in a virtual terminal (tty). But you know this part...

Difference between database vs user vs schema

In Oracle, users and schemas are essentially the same thing. You can consider that a user is the account you use to connect to a database, and a schema is the set of objects (tables, views, etc.) that belong to that account. See this post on Stack Overflow:...

What’s the output of this code written in java?

//if you're using Eclipse, press ctrl-shift-f to "beautify" your code and make it easier to read int arr[] = new int[3]; //create a new array containing 3 elements for (int i = 0; i < 3; i++) { arr[i] = i;//assign each successive value of i to an entry in...

How safe are password managers like LastPass?

We should distinguish between offline password managers (like Password Safe) and online password managers (like LastPass). Offline password managers carry relatively little risk. It is true that the saved passwords are a single point of failure. But then, your...

Can anyone tell me why this program go to infinite times?

while (i <= 2) { while (i > 0) { a = a + b; i--; <- out the inner while loop when i = 0 } printf("%d", a); i++; <- at here, the i==0 each time, so infinity loop } Because your nested loop always restores the value of i to 0, And 0 <= 2 is always...

How to conditionally do something if a command succeeded or failed

How to conditionally do something if a command succeeded or failed That's exactly what bash's if statement does: if command ; then echo "Command succeeded" else echo "Command failed" fi Adding information from comments: you don't need to use the [ ... ] syntax...

How to turn JSON array into Postgres array?

Postgres 9.4 or newer Obviously inspired by this post, Postgres 9.4 added the missing function(s): Thanks to Laurence Rowe for the patch and Andrew Dunstan for committing! json_array_elements_text(json) jsonb_array_elements_text(jsonb) To unnest the JSON array....

Implementing a 2D destructible landscape (like Worms)

I don't know how the landscape in worms was implemented exactly, but I'm pretty sure they used a bitmap for the landscape (at least in the older games of the series). A very basic approach would be a bitmap image (B/W) where black pixels represent air and white...

Huge procedurally generated ‘wilderness’ worlds

I think I better understand what you are asking now. Noise is not random - it's random-looking but is completely based on a mathematical formula and is repeatable. All the information is encoded in the formula. This means that you can have a formula that...

How does one prevent homing missiles from orbiting their targets?

First of all, you should make all calculations about what acceleration to apply in the missile's frame of reference (that's where the missile is stationary and everything else moves around it, also often called "object coordinates" or "local coordinates" in...

Is UDP still better than TCP for data-heavy realtime games?

No, UDP is still superior in terms of performance latency, and will always be faster, because of the philosophy of the 2 protocols - assuming your communication data was designed with UDP or any other lossy communication in mind. TCP creates an abstraction in...