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.
does any one has an idea how I can rename all files in a directory like file1 - file1000 for example?
I have many files all with different names and I want them to have all the same beginning in the name, appended with a counter , so file1, file2, file3 ....
Is there an easy way to do this?
I work with Bash-shell....
You'll want to man bash to find out more about bash scripting. You may also find grep or egrep useful, depending on what information you want to pick out of the current file names. Finally, use the mv command to rename a file.
#/bin/sh
cnt=0
for i in ./*; do
let cnt=cnt+1
rename $i pic$cnt.jpg
done
But somehow, not all files get renamed...
If I try it with the mv command, like this:
#/bin/sh
cnt=0
for i in ./*; do
let cnt=cnt+1
rename $i pic$cnt.jpg
done
I get lots of errors:
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
...
Why???
Please help me and give me a hint, I feel really lost...
Originally posted by merlin23
[B]If I try it with the mv command, like this:
#/bin/sh
cnt=0
for i in ./*; do
let cnt=cnt+1
rename $i pic$cnt.jpg
done
I get lots of errors:
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
I presume you mean
Code:
#/bin/sh
cnt=0
for i in ./*; do
let cnt=cnt+1
mv $i pic$cnt.jpg
done
This works on my system.
Do you have file names with spaces in? This would explain why mv thinks you are trying to rename multiple files, as it uses a space as a delimiter between files.
One other point, it looks as though you are running this in the directory where the files are, this means your shell script will be renamed too. It may take a bit of finding afterwards!
Useful tip:- If you want to know what is happening inside a shell script use #!/bin/bash -x
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.