[SOLVED] Clueless on how to remove spaces from all .jpg's beneath a folder
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Clueless on how to remove spaces from all .jpg's beneath a folder
OK, I have a folder, that has multiple subfolders (which have subfolders of their own) containing a variety of different files. What I need to do is remove all the spaces from the filenames of .jpg files only in all those folders.
Please tell me there is a way to do this with some fancy command way above my skillset.
OK, I have a folder, that has multiple subfolders (which have subfolders of their own) containing a variety of different files. What I need to do is remove all the spaces from the filenames of .jpg files only in all those folders.
Please tell me there is a way to do this with some fancy command way above my skillset.
There is a way to do this with some fancy command!
Create a myscript.sh
Code:
#!/bin/bash
for file in "$@"
do
newfilename="${file// /}"
if [[ "$newfilename" != "$file" ]]; then
echo mv "$file" "$newfilename"
fi
done
thanks guys. i92, that looks similar to something i've seen someone else do before, so i might go with that. I'd just like to remove the spaces, don't need to substitute an underscore. so do i just use the top line of your last post? can you explain what the part after "do" does, i'm confused.
if you want to do it in auto fashion you will need a script and run it.
certainly this requires workable knowledge of bash scripting.
you can get similar script here click this. Or click this it will also give you another idea.
thanks guys. i92, that looks similar to something i've seen someone else do before, so i might go with that. I'd just like to remove the spaces, don't need to substitute an underscore. so do i just use the top line of your last post?
Yes. But use this one instead:
Code:
find . -type f -name '* *'.[jJ][pP][gG] | while read file; do echo mv "$file" "${file// /}"; done
That will list the list of commands that will be run. If you like the results, then remove "echo" from the command and run it again to do the real work.
Quote:
can you explain what the part after "do" does, i'm confused.
find will generate a list of files that contains at least a space on their name, and whose extension is jpg, JPG, Jpg or some other combo with that letters. "while" will consume it, one item at a time, assigning a new file name on each loop to $file. On each loop, the command "mv" is run, and it moves the file pointed by $file to it's new name.
The part that is less clear to you is probably this:
Code:
"${file// /}"
This is a feature of the bash shell (meaning that other shells don't have it). Bash has some basic string processing capabilities. In this case, we use this to substitute all occurrences of " " (blank space) by "" (nothing). The syntax is this:
That results in a string that's very similar to the original file name, but doesn't contain any space, since all the blanks have been substituted by "". There's a similar form which will only substitute the first instance of the substring:
Code:
${variable/substring_to_substitute/new_substring}
You might want to check this article (and the responses which expand it):
OK, I have a folder, that has multiple subfolders (which have subfolders of their own) containing a variety of different files. What I need to do is remove all the spaces from the filenames of .jpg files only in all those folders.
1. copy and paste the shell script below
2. replace 'absolute_path_to_the_top_directory' by the
absolute path to your top folder
3. save and make the shell script executable,
then run the script
it will just echo what it's doing. If it looks ok you can
run it for real by uncommenting the line
# mv "$f" "$nf"
and removing this line
echo "mv \"$f\" \"$nf\""
I assume you have write permission in those folders
Code:
##### copy and paste below this line #####
#!/bin/sh
TOP=absolute_path_to_the_top_directory
if [ ! -d $TOP ]; then
echo "sorry $TOP doesn't exist or is not a directory"
exit 1
fi
find $TOP/* | while read f
do
if [ -d $f ]; then
continue
fi
g="${f##*/}"
ext="${g##*.}"
if [ $(echo $ext | tr '[:upper:]' '[:lower:]') == jpg ]; then
ng="$(echo $g | sed 's? \{1,\}??g')"
nf="${f%/*}/$ng"
if [ "$nf" != "$f" ]; then
# mv "$f" "$nf"
echo "mv \"$f\" \"$nf\""
fi
fi
done
##### end of copy above this line
so where you have the . at the beginning of the line, i should put in my full path to the root folder of where i want the magic to happen, or should i just cd /full/path then run it exactly as you have it written?
Either way will work, anyway, until you remove the "echo" it's safe to run it, and you can see what will it do. You could as well use a relative path (in fact, "." itself is a relative path).
ps. When running interactively I preffer to cd to the dir, because that way the paths are shorter and the output is less confusing, but besides that it's a merely cosmetic thing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.