NB! I have already started a thread on this subject on ComputerHope (with the thread title "Recursive File Renaming & Logical Sorting"). However, on ComputerHope they are perhaps more specialized in Windows Command Prompt, and not that much in shell scripts for Bash (I guess).
I have a bulk renaming problem that I have to tackle in some way with the help of a shell script for Bash. I should write a shell script to recursively rename all the image files in a folder and its subfolders to "001", "002", "003" and so forth. The aim is to preserve all the file extensions (and my image files have many different extensions: jpg, jpeg, png, bmp and so on). All the folder names should also remain unchanged. Here's a short example showing what I mean:
As you can see, once the script is applying new filenames to the files that it can find, it starts from "001" in each folder.
Thus far, I have tried to put together a batch for Windows Command Prompt and a shell script for Bash to accomplish this. The problem with Command Prompt is that there does not seem to be a way of displaying files in "human" numerical order unless the files were first renamed with zero-padded filenames (which would be very time-consuming because I have hundreds of folders with hundreds of subfolders full of images). In other words, there is no "logical sorting" or "natural sorting" as it may sometimes be referred to as. Once you type "dir" in Windows Command Prompt, you'll get a result like this:
As you can notice, the problem is that 10-12 are displayed before 2-9 in Windows Command Prompt. Thus, if the files are renamed with a batch for Windows Command Prompt, the lack of any "natural sorting" will inevitably affect the renaming. In Bash, however, this problem can be bypassed by piping your preferred directory listing command through "sort -h". For example, "ls|sort -h" displays all the files in "human" numerical order.
The problem is that I'm a complete beginner with Bash, and I did my utmost to put together a shell script but everything was to no avail. I also ran many different ready-made scripts (with some minor changes) without any success. For example, I found the shell script below on Stack Overflow but it doesn't do quite what I want it to do even though I did my best to edit it:
Do you have any suggestions on how to remedy this problem with Bash so that I could rename my image files in the way explained above? I really wanted to do this by myself but I don't have the skills so any help you can provide would be most welcome!
P.S. The images are on my Windows PC but I'm using Cygwin (a Unix-like environment and command-line interface for Windows) to run all shell scripts for Bash.
Last edited by JewzeyfGhewbelz; 12-28-2012 at 05:46 PM..
Reason: More clarity and precision, less ambiguity
This kind of problem always seems so easy until you try. I've made many attempts, but they're never quite satisfactory. I haven't found any kind of general solutions that humans can't break, and trying to fix up the corners just makes it easier to confuse.
Imagine this sequence of files for instance.
Is it part of the sequence, or is it not? It's ambiguous.
Or how about this one:
Does cover belong at the beginning or the end, and how would you tell a program to know this? Are you sure the cover will always belong at the beginning?
Should we assume the jpeg is a typo because they have the same prefix of asd? Where does the prefix start, where does it end, and how is it deliniated from the rest?
Or this sequence:
Do 'cover for some file name containing 1234' and 'some file name containing 1234 #' belong to the same sequence or not? Should # be considered part of the name?
And what happens when you get a sequence like this?
You end up tacking on special case after special case after special case onto your program to handle all these special things -- and then you get something like this thrown at you:
Or maybe the files all have 'cover' in their name so they all end up being 'special' and jammed in first in no particular order, or even skipped. Or this, or that, or a million other things.
If humans would just name their files in rational, consistent, predictable ways, they wouldn't need reordering. But all too often human context is needed to explain where things belong.
Can you explain what this line of script is doing.
What I have understood is :
-- variable C is the name of a software which is either not installed, so it must be installed or allready installed and then should be update if newer version found
-- branch B="$B $C" is to install the software
--... (4 Replies)
Hi!
I wanted to simplify my bash prompt, so I edited my etc/bashrc file. I thought this was the file that would override any other env files. When I opened it, I saw that the way it was setup was not what my prompt looked like, although I forget exactly what was there. But i edited it the way I... (1 Reply)
hello
does someone want to help me for this one ?
i want to rename files & a folder according to the similarities in filenames
for example :
the file with the good name
cglogo tougl1953 dgmel bogd 01 -- ttgductoog ggdté gollogtd.ext1the others files needed to be renamed
cglogo... (5 Replies)
hi,
I would like to monitor a log file, which rolls over, everytime a server is restarted.
I would like to grep for a string, and to be more efficient i'd like to grep only newly appended data. so something like a 'tail -f' would do, however, as the log rolls over i think a 'tail -F' is... (2 Replies)
Hi, i want to rename a group of directories and files of my music, some items are like this:
- , for directories.
- , for files.
I want to do something like this:
, for directories.
, for files.
This is my code:
#!/bin/bash
for fname in *.mp3; do
echo item: $fname
mv... (2 Replies)
How does one make a recursive script in the bash shell? For instance, if I wanted a script that stepped through the directory structure starting with . and going to the deepest level, and ran make in each folder in the tree.
Yeah, I know a makefile could be setup to do this, but it's not... (11 Replies)
I have UWin version of Unix for Destop
I have files with extension *.abc. I want to rename it to *.csv.
Some of the filenames have spaces.
I also to rename the files with extension *.abc in the corresponding subfolders
ex
==
abc def.abc
ghi jkl.abc
mnopqr.abc
uvwxyz.abc
rename as... (4 Replies)
I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves.
Also, the script should only move a file from /exports to... (6 Replies)
Hi all,
In our webserver customer wants to see the latest 10 files arrived. So the names are hardcoded in webpage like :
filename_01.txt
filename_02.txt
....
....
filename_10.txt
where the filename_01.txt is the latest one (typically the output of ls -1t) in the /../webpage directory.... (2 Replies)
In the ls command, -t option and -R option dont work simultaneously.
ls -t ---> lists the files with sorting based on file date
ls -R ---> lists the files recursively.
How to make utilize both in the same command.?
I want to sort the recursive files listing.. (1 Reply)