LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-14-2014, 06:48 AM   #1
roberto32
LQ Newbie
 
Registered: Dec 2013
Location: Slovakia (Modra)
Distribution: Suse 13.1. LMDE 13, ubuntu 12.04(not a newbie but passwd recovery is really weird a bug?)
Posts: 21

Rep: Reputation: Disabled
loop through all folders & files - change file/foldernames & content of files


I need bash script that'll loop through directory and all its subdirectories and rename all files and folders as well as contents of files (xml, txt files so easily edited e.g by vim) containing certain patterns ( 3 patterns to be exact). Sumarry : rename all files/folders and contents of files matching certain pattern. and my the file/foldernames also includes freespace. I've 3 paterns wk10 v15, wk10_v15, wk10v15 and all I need is to change that v15 to v35 or some other number

Last edited by roberto32; 06-14-2014 at 06:53 AM.
 
Old 06-14-2014, 07:41 AM   #2
roberto32
LQ Newbie
 
Registered: Dec 2013
Location: Slovakia (Modra)
Distribution: Suse 13.1. LMDE 13, ubuntu 12.04(not a newbie but passwd recovery is really weird a bug?)
Posts: 21

Original Poster
Rep: Reputation: Disabled
actually my code looks like this

Code:
 #!/bin/bash
$path =/home/robert/Templates/result_arch/*

for i in { $path.xml $path.txt $path.log }
do
vim $i | %s/wk10 && v05/wk10 && v15/gC%s/wk10_v05/wk10_v15/g && %s/wk10v05/wk10v15/g
:wq
done

for i in $path
do
find $ -name '*-wk10 v05-*' -exec bash -c 'mv $0 ${0/wk10 v05/wk10 v15}' {} \;
find $ -name '*-wk10_v05-*' -exec bash -c 'mv $0 ${0/wk10_v05/wk10_v15}' {} \;
find $ -name '*-wk10v05-*' -exec bash -c 'mv $0 ${0/wk10v05/wk10v15}' {} \;


#mv"$i" "`echo $i | sed 's/wk10 v05/wk10 v15/'`
#mv"$i" "`echo $i | sed 's/wk10_v05/wk10_v15/'`
#mv"$i" "`echo $i | sed 's/wk10v05/wk10v15/'`
done

Last edited by roberto32; 06-14-2014 at 07:48 AM.
 
Old 06-14-2014, 08:20 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
This is a job for find and sed: use find to walk the tree, use sed to substitute one pattern for another pattern (wk10 v15, wk10_v15 or wk10v15 with wk10 v35, wk10_v35 or wk10v35 or whatever else you may choose).

Before you start, make a back up copy of the entire tree:
Code:
cd <some_directory>
cp -pr <original_directory> .
About renaming all files and folders: use this little shell program to rename all files and directories ("folders") substituting a space with an underscore (this is for fixing Windows Weenie file names).
Code:
#!/bin/bash
#ident	"$Id$"
#
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of version 2 of the GNU General
#	Public License as published by the Free Software Foundation.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#	General Public License for more details.
#
#	You should have received a copy of the GNU General Public
#	License along with this program; if not, write to the Free
#	Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
#	MA 02111-1307, USA.
#
#	Name:		$Source$
#	Version:	$Revision$
#	Modified:	$Date$
#	Purpose:	Replace blanks in file names with underscores
#	Author:		Unknown
#	Date:		28 Feb 2010
#	$Log$

ONE=1				# For getting singular/plural right (see below).
number=0			# Keeps track of how many files actually renamed.
FOUND=0				# Successful return value.

for filename in *		# Traverse all files in directory.
do
     echo "$filename" | grep -q " "         #  Check whether filename
     if [ $? -eq $FOUND ]                   #+ contains space(s).
     then
       fname=$filename                      # Yes, this filename needs work.
       n=`echo $fname | sed -e "s/ /_/g"`   # Substitute underscore for blank.
       mv "$fname" "$n"                     # Do the actual renaming.
       let "number += 1"
     fi
done   

if [ "${number}" -eq "$ONE" ]                 # For correct grammar.
then
	echo "${number} file renamed."
else 
	echo "${number} files renamed."
fi 

exit 0
Save that as, say, blank-rename and make it executable:
Code:
chmod 755 blank-rename
Then get into the parent directory that you want to fix and execute it:
Code:
cd <directory>
blank-rename
Handy for when you're dealing with Windows file names (we don't use space characters in file names in Linux). Hang on to it, it'll come in handy over time.

find is pretty easy:
Code:
cd <parent directory>
find . -type f
sed is also pretty easy (and you can do the substitution in place with sed):
Code:
sed -i 's/v15/v35/g' ${file}
or, if you have other patterns that contain v15:
Code:
s -i 's/wk10\ v15/wk10\ v35/;s/wk10_v15/wk10_v35/g;s/wk10v15/wk10v35/g' ${file}
Read the man page for find and sed for more options.

If you intend to actually rename files and directories (other than changing the spaces to underscores), that's a little more complicated (you can see how it's done in blank-rename). Need to know a little more about what you intend.

Putting it together
Code:
cd <directory>
for file in $(find . -type f)
do
     s -i 's/wk10\ v15/wk10\ v35/;s/wk10_v15/wk10_v35/g;s/wk10v15/wk10v35/g' ${file}
done
Give that a shot and see what happens (don't forget to make a back up of your tree first!).

Hope this helps some.

Last edited by tronayne; 06-14-2014 at 08:23 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive chown folders & not files gutterboy Linux - Server 2 12-02-2010 01:10 PM
Sharing Files & Folders KapilSatav Linux - Networking 5 08-11-2009 06:42 AM
Owner 99 Group 99 files & folders adnanm Linux - Newbie 1 05-26-2008 11:02 AM
copying files & folders with ALL CAPS digity Linux - Newbie 3 11-01-2007 06:43 PM
Inporting files & folders ? fin Linux - Newbie 4 03-28-2003 10:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:37 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