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.
I have a /ton/, and I mean a tonof photos that I have to rename, and I was wondering if there was a way to automate the process of removing the first 6 digits of every filename in a folder?
I'm using Raspbian, strangely enough, if that makes any difference!
You can use the following bash script to remove first 6 letters of all files in a directory.just change the directory path with your path in the rename_dir variable.
run the script from anywhere (if you run the script in the same directory,which you want to rename the files,it will rename this script also..i think)
#!/bin/bash
rename_dir="/root/Desktop/ton"
for file in `ls -A $rename_dir`
do
newfile=`echo $file | sed 's/^......//g'`
mv $rename_dir/$file $rename_dir/$newfile
done
Just out of curiosity, what part of it dictates how many letters are removed?
and I'm guessing that this moves the files around during the process. Is there a way to leave them where they are and just process the names? (Super limited space, big files)
Re post #3: each '.' represents 1 'character' at that position. The '^' means match starting at the beginning of the string in qn.
Incidentally, 'mv' is actually the Linux rename cmd, so its an in-place cmd .
rename_dir="/root/Desktop/ton"
#Change the for-loop separator to handle spaces:
OLDIFS=$IFS
IFS=`echo -en "\n\b"`
for file in `ls -A $rename_dir`; do
newfile=`echo $file | sed 's/^......//g'`
mv "$rename_dir/$file" "$rename_dir/$newfile"
done
#Restore IFS:
IFS=$OLDIFS
Re filename space(s): in *nix generally, the default convention for any prog/cmd is that input params are space separated... hence your problem.
So, spaces-in-names are syntactically legal, but, as you've seen, not advised
Frankly I've never seen one in any file supplied by a *nix supplier; either OS or App and all(?) experienced *nix techs avoid them like the plague (where possible).
Frankly I've never seen one in any file supplied by a *nix supplier; either OS or App and all(?) experienced *nix techs avoid them like the plague (where possible).
The only one I can think of is VirtualBox, they throw spaces all over the place.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.