how to change names of directory in a given directory
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.
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.
#!/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
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.
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?
@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?
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.
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
Last edited by suicidaleggroll; 01-18-2013 at 10:20 AM.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.