Sponsored Content
Top Forums Shell Programming and Scripting Working with directories, need assistance Post 302343846 by gratuitous_arp on Thursday 13th of August 2009 08:24:55 PM
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

 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
DH_USRLOCAL(1)							     Debhelper							    DH_USRLOCAL(1)

NAME
dh_usrlocal - migrate usr/local directories to maintainer scripts SYNOPSIS
dh_usrlocal [debhelperoptions] [-n] DESCRIPTION
dh_usrlocal is a debhelper program that can be used for building packages that will provide a subdirectory in /usr/local when installed. It finds subdirectories of usr/local in the package build directory, and removes them, replacing them with maintainer script snippets (unless -n is used) to create the directories at install time, and remove them when the package is removed, in a manner compliant with Debian policy. These snippets are inserted into the maintainer scripts by dh_installdeb. See dh_installdeb(1) for an explanation of debhelper maintainer script snippets. When the Rules-Requires-Root field is not (effectively) binary-targets, the directories in /usr/local will have ownership root:staff and the mode will be 02775. These values have been chosen to comply with the recommendations of the Debian policy for directories in /usr/local. When Rules-Requires-Root has an effective value of binary-targets, the owners, groups and permissions will be preserved with one exception. If the directory is owned by root:root, then ownership will be reset to root:staff and mode will be reset to 02775. This is useful, since that is the group and mode policy recommends for directories in /usr/local. OPTIONS
-n, --no-scripts Do not modify postinst/prerm scripts. NOTES
Note that this command is not idempotent. dh_prep(1) should be called between invocations of this command. Otherwise, it may cause multiple instances of the same text to be added to maintainer scripts. CONFORMS TO
Debian policy, version 2.2 SEE ALSO
debhelper(7) This program is a part of debhelper. AUTHOR
Andrew Stribblehill <ads@debian.org> 11.1.6ubuntu2 2018-05-10 DH_USRLOCAL(1)
All times are GMT -4. The time now is 02:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy