Prepend name of directory to children folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Prepend name of directory to children folders
# 1  
Old 03-29-2008
Prepend name of directory to children folders

Hi, I am a shell scripting newbie. I am in need of a shell script that will prepend the name of the parent directory to the child directory.

For example if the shell script called rename.sh is invoked with ">rename.sh /home/foobar/Simple" and the structure of the folder Simple is :

Simple
|_A
|_B
|_C

I need the script to rename A, B and C to "Simple-A", "Simple-B" and "Simple-C" as shown below:

Simple
|_Simple-A
|_Simple-B
|_Simple-C

Thanks in advance for any help rendered.
# 2  
Old 03-29-2008
Code:
#!/bin/sh


DIR=${1}
PREF=hzsho
cd $DIR
for i in `ls`; 
do 
        mv $i ${PREF}${i};
done

# 3  
Old 03-29-2008
Quote:
Originally Posted by mirusnet
Code:
for i in `ls`;

Please don't perpetrate this construct. It's a Useless Use of Backticks and it's wrong if there are any subdirectories. The correct way to loop over all files in a directory is simply

Code:
for i in *

# 4  
Old 03-29-2008
If it were my script, I would perhaps also prefer for it to work in the current directory.

Code:
PREF=$(basename "$(pwd)")

... and do away with the DIR stuff. Otherwise, to conform with the original spec, you should use PREF=$(basename "$DIR")

Last edited by era; 03-29-2008 at 04:10 PM.. Reason: basename $DIR addition
# 5  
Old 03-29-2008
Thanks

Hi era and mirusnet,

Thanks so much for your immediate assistance. This is exactly what I was looking for...Smilie
# 6  
Old 03-29-2008
I have a minor follow up. If I were in the folder when the command is executed from within the parent folder, for example:
Code:
>/bin/folderRename.sh .

then the file names get set with the period and the name of the parent folder is not applied.

Code:
if [${1} eq '.'] 
  then 
   DIR=$PWD
  else
   DIR=${1}
fi
PREF=$(basename "$DIR")
cd $DIR
for i in *; 
do 
        mv $i ${PREF}${i};
done

I get a complaint stating "[.: command not found". Looks like I cannot use the "." for comparison.

Please assist. Thanks again.
# 7  
Old 03-29-2008
You need a space after the [, just like after any other command. (Yes, it's a command. It's confusing at first.) Also before the closing bracket.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Tar.gz/zip folders in a directory with _EBASE string

Hi Folks - Happy Friday and I hope you all are well! What's the easiest way to tar.gz / zip all direct children directories in a folder that have the string _EBASE (suffix)? Thank you! (6 Replies)
Discussion started by: SIMMS7400
6 Replies

2. Shell Programming and Scripting

Bash to list all folders in a specific directory

The below bash is trying to list the folders in a specific directory. It seems close but adds the path to the filename, which basename could strip off I think, but not sure why it writes the text file created? This list of folders in the directory will be used later, but needs to only be the... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Move specific folders and subfolders in a directory

I am trying to move specific folders and subfolders within a directory using the below. I can see the folders to move and they are at the location, but I am getting an error. Thank you :). mv -v /home/cmccabe/Desktop/NGS/API/6-10-2016{bam/{validation,coverage},bedtools /media/cmccabe/"My... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. UNIX for Beginners Questions & Answers

How to loop command over folders in a directory?

Hello all, I'm fairly new to scripting so please bear with me. I will try to explain as best as I can but if there's anything that is not clear, please let me know. I have a directory (see below) with numerous folders and within each of those folders are various files. I would like to run a... (11 Replies)
Discussion started by: azurite
11 Replies

5. Shell Programming and Scripting

Cpio all *.txt-files out of folders to just one directory

I need a hint for reading manpage (I did rtfm really) of cpio to do this task as in the headline described. I want to put all files of a certain type, lets say all *.txt files or any other format. Spread in more than hundreds of subdirectories in one directory I would like to select them and just... (3 Replies)
Discussion started by: 1in10
3 Replies

6. Shell Programming and Scripting

Copy the files in directory and sub folders as it is to another directory.

How to copy files from one directory to another directory with the subfolders copied. If i have folder1/sub1/sub2/* it needs to copy files to folder2/sub1/sub2/*. I do not want to create sub folders in folder2. Can copy command create them automatically? I tried cp -a and cp -R but did... (4 Replies)
Discussion started by: santosh2626
4 Replies

7. Shell Programming and Scripting

Delete multiple folders in a directory which are two weeks old

I need help. I have to delete multiple directories inside a directory that are two weeks old. Example: Today is July 09, 2012 Folder1 > folder1 (created June 4, 2012) -- should be deleted > folder2 (created June 2, 2012) -- should be deleted > folder3 (created... (4 Replies)
Discussion started by: jasperux
4 Replies

8. UNIX for Dummies Questions & Answers

[SOLVED] Delete files and folders under given directory

I have a requirement to delete the files and folders under a given directory. my directory structure is like this.. Data | A(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files |--files | B(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files ... (1 Reply)
Discussion started by: ramse8pc
1 Replies

9. UNIX for Dummies Questions & Answers

Counting number of folders in a Directory

Help Needed ! Can we count number of folders of specific date in a directory, even if directory has folders of different dates. Please reply as soon as possible. (1 Reply)
Discussion started by: vishal_215
1 Replies

10. UNIX for Advanced & Expert Users

mv OR cp many files from 1 big directory with sub folders all to 1 spot?

Hello All. I am trying to do this from a terminal prompt on my mac.... I have 100 folders all named different things. Those 100 folders are inside ~/Desktop/Pictures directory. Each of the 100 folders are uniquely named. The image files inside of each folder only have some similarities. ... (1 Reply)
Discussion started by: yoyoyo777
1 Replies
Login or Register to Ask a Question