Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Append string to all the files inside a directory excluding subdirectories and .zip files


👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-22-2015
Ravi Kishore Ravi Kishore is offline
Registered User
 
Join Date: Apr 2015
Last Activity: 28 May 2015, 7:16 AM EDT
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Append string to all the files inside a directory excluding subdirectories and .zip files

Hii,

Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories.

Eg.
file1: test1.log
file2: test2.log
file3 test.zip
After running the script
file1: string_test1.log
file2: string_test2.log
file3: test.zip

Thanks in advance,

Last edited by Don Cragun; 04-23-2015 at 03:45 AM.. Reason: Add ICODE tags.
Sponsored Links
    #2  
Old 04-22-2015
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 July 2018, 5:31 PM EDT
Location: Aachen, Germany
Posts: 13,077
Thanks: 450
Thanked 4,016 Times in 3,693 Posts
Please use code tags as required by forum rules!

Any attempts from your side?

---------- Post updated at 13:06 ---------- Previous update was at 13:00 ----------

Anyhow, with a recent bash - unfortunately you don't mention the system nor the shell you use - you could try:
Code:
for FN in !(*.zip) ; do [ -d "$FN" ] && continue; echo mv $FN "String_$FN"; done

after setting shopt -s extglob.
If that's not available, try
Code:
for FN in *; do [ -d "$FN" ] || [ ${FN#*.} == "zip" ] && continue; echo mv $FN "String_$FN"; done

In any case, remove echo when happy with what you see...
The Following User Says Thank You to RudiC For This Useful Post:
Ravi Kishore (04-22-2015)
Sponsored Links
    #3  
Old 04-23-2015
Ravi Kishore Ravi Kishore is offline
Registered User
 
Join Date: Apr 2015
Last Activity: 28 May 2015, 7:16 AM EDT
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Thank you very much Rudic...this worked great

---------- Post updated 04-23-15 at 12:16 PM ---------- Previous update was 04-22-15 at 04:45 PM ----------

Hey, this worked fine when I run through command line,
command:
Code:
for FN in !(*.zip)  ; do [ -d "$FN" ] && continue; mv $FN "appIn1a_$FN"; done

but it gives
syntax error near unexpected token `('

when I use this same in bash script.

Please suggest the changes

Last edited by Scrutinizer; 04-23-2015 at 02:49 AM.. Reason: code tags
    #4  
Old 04-23-2015
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 19 July 2018, 1:50 AM EDT
Location: Amsterdam
Posts: 11,853
Thanks: 546
Thanked 3,469 Times in 3,057 Posts
Try putting:
Code:
shopt -s extglob

At the beginning of your script.
The Following User Says Thank You to Scrutinizer For This Useful Post:
Ravi Kishore (04-23-2015)
Sponsored Links
    #5  
Old 04-23-2015
Ravi Kishore Ravi Kishore is offline
Registered User
 
Join Date: Apr 2015
Last Activity: 28 May 2015, 7:16 AM EDT
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Thank you very muchh...it worked
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unzip all the files with subdirectories present and append a part of string from the main .zip files Ravi Kishore Shell Programming and Scripting 0 04-22-2015 05:07 AM
Find/searching files in subdirectories excluding the fiels in Parent Directory Naveenkk Shell Programming and Scripting 1 07-08-2014 01:13 AM
Find command to search files in a directory excluding subdirectories jhilmil Shell Programming and Scripting 8 12-14-2012 08:23 AM
Zip files inside the directory, but not the directory itself Thales.Claro UNIX for Dummies Questions & Answers 2 07-20-2010 05:40 AM
Find Files in a Directory Excluding Subdirectories Amol_Dicholkar UNIX for Dummies Questions & Answers 2 12-11-2008 06:54 AM



All times are GMT -4. The time now is 06:48 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?