Working with directories, need assistance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Working with directories, need assistance
# 1  
Old 08-13-2009
Working with directories, need assistance

Hello there,

I have the following issue and I need you assistance.

I have created a script that searches for directories that have spaces, for example:

./assets/en/images/pricing/current offers/SIMPLE GRID
./assets/en/images/pricing/current offers/ADVERTISED PRICE

But now the requirements have been updated and I have to rename the directory with an "_", so the result will be:

./assets/en/images/pricing/current_offers/SIMPLE_GRID
./assets/en/images/pricing/current_offers/ADVERTISED_PRICE

I have tried something like this:

mv ./assets/en/images/pricing/'current offers'/'SIMPLE GRID' ./assets/en/images/pricing/'current_offers'/'SIMPLE_GRID' but is not working.

Could you please give me a suggestion or any other idea to do this?

Thanks for the help.

Best regards

---------- Post updated at 04:27 PM ---------- Previous update was at 04:07 PM ----------

I have tried also these
find . -name '* *' | while read file;
do
target=`echo "$file" | sed 's/ /_/g'`;
echo "Renaming '$file' to '$target'";
mv "$file" "$target";
done;

But is not working

I received an error, since it replace the first found instance for example
assets/en/images/pricing/current_offers but when tries to change the second
assets/en/images/pricing/current offers/SIMPLE GRID it shows "No such file or directory"

thanks
# 2  
Old 08-13-2009
I'm assuming this is a *nix environment. In which case you can't just use spaces. A path with spaces is understood as FILENAME\ WITH\ A\ SPACE.

Each space has to be escaped, and spaces in reg exp are \s.

In your code:
Code:
target=`echo "$file" | sed 's/ /_/g'`;

should be
Code:
target=`echo "$file" | sed 's/\s/_/g'`;

Hope this points you in the right direction.

Last edited by chompy; 08-13-2009 at 09:04 PM.. Reason: Forgot an example
# 3  
Old 08-13-2009
This is one solution.

The first matter of business is to reverse the order of the matches, so you are looking at the last matches first, which will be the deepest subdirectories/files in a particular tree. This will prevent you from renaming a parent directory first, and later having an invalid path to a subdirectory. You can use 'tac' to reverse the order of the matches returned by find.

You are getting the error message because you are replacing all spaces in the line with underscores, including those in the path. However, the mv command will only rename the filename (or directory) given after the last slash, not the entire path including a filename.

For example, if I have a directory structure like this (with spaces):

parent dir
\_ sub dir

And I do this to change the spaces into underscores:

Code:
mv   ./parent\ dir/sub\ dir   ./parent_dir/sub_dir

mv is going to give me an error because parent_dir (with underscores) does not exist for sub\ dir to be placed there. mv is only operating on sub\ dir, and it is given an invalid path.

To prevent this, separate the filename from the path, so you can operate on the filename separately from the path. The 'dirname' and 'basename' commands help you do this.

Lastly, use sed to replace all spaces in the filename with underscores, then glue the path and filename back together in your mv command.

Changing your code around, it would end up looking something like this:

Code:
#!/bin/bash

find . -name "* *" | tac | while read file ; do

        directory=`dirname "$file"`
        target=`basename "$file" | sed "s/ /_/g"`
        echo "Renaming '$file' to '${directory}/${target}'"
        mv "$file" "${directory}/${target}"

done

# 4  
Old 08-14-2009
Code:
mv SIMPLE\ GRID SIMPLE_PRICE
mv ADVERTISED\ PRICE ADVERTISED_PRICE

# 5  
Old 08-17-2009
Thanks for all the help guys... It works!!!

Best regards :0)
Pura vida
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Giving read write permission to user for specific directories and sub directories.

I have searched this quite a long time but couldn't find the right method for me to use. I need to assign read write permission to the user for specific directories and it's sub directories and files. I do not want to use ACL. This is for Solaris. Please help. (1 Reply)
Discussion started by: blinkingdan
1 Replies

2. Shell Programming and Scripting

Grep with Regular expression now working on file directories

Hello Everyone, I have a file sam1 with the below content SYSYSID;MANDT;/SIE/AD_Z0M_INDX;/SIE/AD_Z0M_KEY1 echo $Regex \bSYSYSID\b|\bMANDT\b|\b/SIE/AD_Z0M_INDX\b|\b/SIE/AD_Z0M_KEY1\b cat sam1 | grep -Eo $Regex I expect the result as SYSYSID MANDT /SIE/AD_Z0M_INDX /SIE/AD_Z0M_KEY1... (4 Replies)
Discussion started by: sam99
4 Replies

3. Shell Programming and Scripting

Perl Script not working on all directories

Hi Folks, I have a script that I am using. The files are in Directory c:\files\change\options In that directory I have many other sub folders like R1 R2 R5 E4 etc... When I run this script in windows, It looks like its just changing the first folder R1 and not the rest. Can I get an... (6 Replies)
Discussion started by: richsark
6 Replies

4. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

5. UNIX for Advanced & Expert Users

wget assistance to manage parent directories...

looking for a wget option that does not retrieve entire path from source box... currently working with the following syntax: wget --no-verbose --mirror --no-parent --no-host-directories --page-requisites --ftp-user=mis --ftp-password=secret ftp://192.x.x.x/tmp/test is anyone familiar... (0 Replies)
Discussion started by: mr_manny
0 Replies

6. Shell Programming and Scripting

Script for parsing directories one level and finding directories older than n days

Hello all, Here's the deal...I have one directory with many subdirs and files. What I want to find out is who is keeping old files and directories...say files and dirs that they didn't use since a number of n days, only one level under the initial dir. Output to a file. A script for... (5 Replies)
Discussion started by: ejianu
5 Replies

7. Shell Programming and Scripting

Working with multiple home directories.

I need to rename a directory in every home directory on a given workstation. I am a newb to scripting so maybe thats why I cant exactly figure out how to correctly do this. The first thing I need to be able to do to write this script is figure out how to list all the directorys (these are not... (11 Replies)
Discussion started by: trey85stang
11 Replies

8. UNIX for Dummies Questions & Answers

Listing only directories in the current working directory using the "ls" command

Hello All, I am trying to list only directories in my current directory using the command "ls -d". But the output only contains the default directory "." and doesn't list the rest of the directories in the working directory. Can anyone explain why this is happening (2 Replies)
Discussion started by: igandu
2 Replies

9. Shell Programming and Scripting

how to change working directories in perl?

i am new to perl. i am writing a perl script. i want to know how to change the working directories? for ex. i have a perl script in c:\proj\ . i want to run this script in this directory but i need my script to change its working directory to D:\xyz\ dynamically in the script. your help is... (1 Reply)
Discussion started by: megastar
1 Replies
Login or Register to Ask a Question

Featured Tech Videos