LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to change names of directory in a given directory (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-change-names-of-directory-in-a-given-directory-4175446045/)

unix2000 01-17-2013 10:55 AM

how to change names of directory in a given directory
 
I am new to unix.
I have 1 directory(parent) where it has 5 directories in it.
I want to rename the parent directory as well 5 directories in it.
for example:
mv -rf ${i}/${j}/ ${i}_new/${prefix_name}_${j}/
${i}---> list of parent directories
${j}---> list of child directories present in parent directory.
${i}_new--->parent directory to be renamed.
${prefix_name}_${j}---> child directory to be renamed.

Is the above command correct? can anyone help me for this as i am using this in one of my script.

Thanks UNIX forum.

shivaa 01-17-2013 12:07 PM

Just for remane purpose you can use:-
Code:

~$ mv <old_name> <new_name>
Or you can create a simple script:-
Code:

#!/bin/bash
LIST=/tmp/dir_list.txt
cd /path/to/parant_dir
ls -ld /path/to/parant_dir | awk -F" " '{print $9}' > $LIST
for dir in $(cat $LIST)
do
echo "Enter new name for $dir: "; read newname
mv -v $dir $newname
echo "$dir renamed to $newname successfully."
cd
mv /path/to/parant_dir /path/to/parant_dir_new_name
done


David the H. 01-18-2013 09:11 AM

1) Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


2) Always quote variables to avoid word-splitting on whitespace, especially when working with filenames.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

But the ${..} brackets around variables do nothing except clutter up the code (in most cases). I recommend leaving them off generally.


3) But what do you mean when you say things like "${i}---> list of parent directories"? Does the $i variable contain more than a single entry? If so, then you can't just use a single command. You have to break it up somehow and loop over the individual entries.

Could you please post an example filetree and how you want it to look afterwards?

David the H. 01-18-2013 09:15 AM

@shivaa, you're still using the incorrect for loop pattern. I've mentioned this several times in threads you've posted. Would you please stop suggesting it to people?

Parsing ls isn't recommended either.

ukiuki 01-18-2013 09:47 AM

This sounds like homework to me.

shivaa 01-18-2013 10:04 AM

Hello David,

I agree. Since you suggested me to use while+read instead of using for, I have been using while+read only.
But I was just waiting for your response on an another thread (see) to find out how can I use read to capture input inside while+read loop.

Anyway, thanks for a explainations.

@unix2000: Try to follow what David the H. has suggested above.

suicidaleggroll 01-18-2013 10:14 AM

Quote:

Originally Posted by shivaa (Post 4872347)
Code:

LIST=/tmp/dir_list.txt
cd /path/to/parant_dir
ls -ld /path/to/parant_dir | awk -F" " '{print $9}' > $LIST
for dir in $(cat $LIST)


Why would you go through all of that when you could just do
Code:

for dir in /path/to/parent_dir/*
Why are you even using a long listing in the first place when you're just using awk to pull of the name? And why use an actual file to hold the list at all? All in all that seems very wasteful, both in processor time and in human time writing all of that out when it could be done better with one very simple line.




OP:
Code:

mv "/path/to/parent" "/path/to/parent_new"
for i in "/path/to/parent_new/*"; do
  dir=$(dirname "$i")
  name=$(basename "$i")
 
  mv "$i" "${dir}/prefix_name_${name}"
done


David the H. 01-18-2013 11:55 AM

Quote:

Originally Posted by shivaa (Post 4872952)
Hello David,

I agree. Since you suggested me to use while+read instead of using for, I have been using while+read only.
But I was just waiting for your response on an another thread ...(see) to find out how can I use read to capture input inside while+read loop.

My apologies then. My reply to the above link has been posted.

But then again the question in that thread does cover a rather specific situation, and doesn't prevent you from avoiding DRLWF loops elsewhere. ;)


All times are GMT -5. The time now is 05:26 AM.