Using iname

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
4,414
Reaction score
4,615
Credits
41,683
This is just a little shell script I use from time, to recursively search all my directories, and find all files that end in .jpg, or jpeg
and move them to my /home/user/Pictures directory

Code:
#!/bin/bash

# Directory to start the search
START_DIR="/path/to/start/directory"

# Destination directory
DEST_DIR="/home/$USER/Pictures"

# Find and move all .jpg and .jpeg files (case insensitive) to the destination directory
find "$START_DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -exec mv {} "$DEST_DIR" \;

echo "All .jpg and .jpeg files have been moved to $DEST_DIR."

I never really thought there was anything special about this script until I showed it to a friend.
He wondered why all the files that ended in .JPG and JPEG got moved as well. It's because I'm using "iname" instead of name.
iname does both uppercase and lowercase files.
 
Last edited:


For those who are trying to learn bash shell scripting...
How would I do the same for the file below? It's different because it's looking for mp3's and m4a's, but as it is written
(because I'm too lazy to fix it yet) it doesn't catch uppercase MP3 and M4A files. How would you fix this?

Code:
#!/bin/bash

# Directory to start the search
START_DIR="/path/to/start/directory"

# Destination directory
DEST_DIR="/home/$USER/Music"

# Find and move all .mp3 and .m4a files to the destination directory
find "$START_DIR" -type f \( -name "*.mp3" -o -name "*.m4a" \) -exec mv {} "$DEST_DIR" \;

echo "All .mp3 and .m4a files have been moved to $DEST_DIR."

If only I had two eyes to see it. :cool:
 
Last edited:
This is just a little shell script I use from time, to recursively search all my directories, and find all files that end in .jpg, or jpeg
and move them to my /home/user/Pictures directory

Code:
#!/bin/bash

# Directory to start the search
START_DIR="/path/to/start/directory"

# Destination directory
DEST_DIR="/home/$USER/Pictures"

# Find and move all .jpg and .jpeg files (case insensitive) to the destination directory
find "$START_DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -exec mv {} "$DEST_DIR" \;

echo "All .jpg and .jpeg files have been moved to $DEST_DIR."

I never really thought there was anything special about this script until I showed it to a friend.
He wondered why all the files that ended in .JPG and JPEG got moved as well. It's because I'm using "iname" instead of name.
iname does both uppercase and lowercase files.
I feel right now the same way I felt about a year or so into my Linux journey when I discovered egrep: Why didn't I know this?
Seriously, I legitimately in all my years did not know iname, I always used regex this whole time... Well, you learn something new every day.
 


Follow Linux.org

Staff online

Members online


Top