Home Man
Search
Today's Posts
Register

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

Tags
bash, shell scripts, shopt extglob, solved

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 04-22-2015
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.
# 2  
Old 04-22-2015
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)
# 3  
Old 04-23-2015
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
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)
# 5  
Old 04-23-2015
Thank you very muchh...it worked
Login to Reply

« Previous Thread | Next Thread »
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 all files in the current directory excluding hidden files and directories ksailesh1 UNIX for Advanced & Expert Users 7 02-03-2014 05:09 AM
Find command to search files in a directory excluding subdirectories jhilmil Shell Programming and Scripting 8 12-14-2012 08:23 AM
Find all files for a user, excluding a directory steve2x4 Shell Programming and Scripting 4 08-30-2010 02:11 PM
Count number of files in directory excluding existing files ammu UNIX for Dummies Questions & Answers 3 08-25-2010 01:54 PM
Replace a string in all files under a directory and its subdirectories EAGL€ Shell Programming and Scripting 4 12-13-2009 12:53 AM
How to find files only inside the subdirectories only? Tuxidow Shell Programming and Scripting 3 09-08-2009 07:09 AM
Find Files in a Directory Excluding Subdirectories Amol_Dicholkar UNIX for Dummies Questions & Answers 2 12-11-2008 06:54 AM
How to remove directory with subdirectories and files? ppa108 UNIX for Dummies Questions & Answers 9 11-25-2008 01:02 AM


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

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