Unix/Linux Go Back    


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

Shell Programming and Scripting


Tags
bash, shopt extglob, solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-22-2015   -   Original Discussion by Ravi Kishore
Ravi Kishore's Unix or Linux Image
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 04:45 AM.. Reason: Add ICODE tags.
Sponsored Links
    #2  
Old Unix and Linux 04-22-2015   -   Original Discussion by Ravi Kishore
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 13 December 2017, 4:17 PM EST
Location: Aachen, Germany
Posts: 11,797
Thanks: 339
Thanked 3,659 Times in 3,357 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 Unix and Linux 04-23-2015   -   Original Discussion by Ravi Kishore
Ravi Kishore's Unix or Linux Image
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 03:49 AM.. Reason: code tags
    #4  
Old Unix and Linux 04-23-2015   -   Original Discussion by Ravi Kishore
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 14 December 2017, 3:02 AM EST
Location: Amsterdam
Posts: 11,652
Thanks: 518
Thanked 3,385 Times in 2,984 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 Unix and Linux 04-23-2015   -   Original Discussion by Ravi Kishore
Ravi Kishore's Unix or Linux Image
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 workedLinux
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:07 AM
Find/searching files in subdirectories excluding the fiels in Parent Directory Naveenkk Shell Programming and Scripting 1 07-08-2014 02:13 AM
Find command to search files in a directory excluding subdirectories jhilmil Shell Programming and Scripting 8 12-14-2012 09:23 AM
Zip files inside the directory, but not the directory itself Thales.Claro UNIX for Dummies Questions & Answers 2 07-20-2010 06:40 AM
Find Files in a Directory Excluding Subdirectories Amol_Dicholkar UNIX for Dummies Questions & Answers 2 12-11-2008 07:54 AM



All times are GMT -4. The time now is 04:07 AM.