I'm kinda new to shell scripting, Here's a problem that I have faced and dunno how to solve:
I want my script to rename the files stored in a .txt file line by line(there's no pattern that I can use with mv, so I want mv to deal with a file generated by ls /home/whatever/ > whatever_files.txt). So, Let's that that now mv is dealing with the first line in whatever_files.txt which has a file name /home/whatever/whatever545.jpg, I want the new file to be called file_whatever.jpg. But in order to do that, I figured out that I must store the name of the file in a variable and having a mv command which looks like this:
mv whateverhey.jpg file_$filename

but I dunno how to store that name in a variable and how to make mv deal with files listed in a text file.


I figured out that I can a non C-style loop to work with all the files in directory but when I write this it doesn't work:
for filename in *
mv $filename hello_$filename



cd /to/my/directory
find .  -name '*.jpg' | \
while read file
    mv $file "new"$file

PS: mv works only within a filesystem. Even though you may be
in directories that are close in terms of directory trees does not mean there are no intervening mountpoints. -

I indeed sometimes try to access some directories behind mount points(for example, hdb1 mounted on /home/user/myfolder), while the script is being executed from the user home directory, Do you mean that this can result in some problems?


This article has been dead for over six months. Start a new discussion instead.