LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-17-2013, 10:55 AM   #1
unix2000
LQ Newbie
 
Registered: Jan 2013
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 01-17-2013, 12:07 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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

Last edited by shivaa; 01-17-2013 at 12:09 PM.
 
Old 01-18-2013, 09:11 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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?
 
Old 01-18-2013, 09:15 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
@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.
 
Old 01-18-2013, 09:47 AM   #5
ukiuki
Senior Member
 
Registered: May 2010
Location: Planet Earth
Distribution: Debian
Posts: 1,030

Rep: Reputation: 385Reputation: 385Reputation: 385Reputation: 385
This sounds like homework to me.
 
Old 01-18-2013, 10:04 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.
 
Old 01-18-2013, 10:14 AM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by shivaa View Post
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

Last edited by suicidaleggroll; 01-18-2013 at 10:20 AM.
 
Old 01-18-2013, 11:55 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by shivaa View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
su:warning: cannot change directory to home/orausr: No such file or directory cdhar Linux - Newbie 4 09-11-2012 06:17 AM
[SOLVED] List files/directory names without directory contents? littlebigman Linux - Software 2 05-03-2011 04:42 AM
change default vsftp anonymous accout directory /var/ftp to other directory? hocheetiong Linux - Newbie 2 01-24-2010 06:33 PM
how to change file names to uppercase in a directory konramesh Linux - General 4 06-16-2009 06:13 AM
Script to change names of files in a directory geomonap Linux - General 2 12-03-2004 03:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration