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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append string to all the files inside a directory excluding subdirectories and .zip files
# 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 04: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...
This User Gave Thanks to RudiC For This Post:
# 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 03:49 AM.. Reason: code tags
# 4  
Old 04-23-2015
Try putting:
Code:
shopt -s extglob

At the beginning of your script.
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-23-2015
Thank you very muchh...it workedSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unzip all the files with subdirectories present and append a part of string from the main .zip files

Hi frnds, My requirement is I have a zip file with name say eg: test_ABC_UH_ccde2a_awdeaea_20150422.zip within that there are subdirectories on each directory we again have .zip files and in that we have files like mama20150422.gz and so on. Iam in need of a bash script so that it unzips... (0 Replies)
Discussion started by: Ravi Kishore
0 Replies

2. Shell Programming and Scripting

Find/searching files in subdirectories excluding the fiels in Parent Directory

Hi All, requirement is to find and remove the files from sub directories but it should exclude the files from parent directory. At present i am using the below one but it finds and remove files from both parent and sub directories. find ${PATH} -type f \( -name securitas\* -o -name \*gz... (1 Reply)
Discussion started by: Naveenkk
1 Replies

3. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

4. Shell Programming and Scripting

Find command to search files in a directory excluding subdirectories

Hi Forum, I am using the below command to find files older than x days in a directory excluding subdirectories. From the previous forums I got to know that prune command helps us not to descend in subdirectories. Though I am using it here, not getting the desired result. cd $dir... (8 Replies)
Discussion started by: jhilmil
8 Replies

5. Shell Programming and Scripting

Zip all the files including directories - subdirectories

Hi, Is is possible to zip a folder and all its contents within the folder ( including sub-directories and files) into a zip file? and can regain the same structure if unzipped? Thanks (6 Replies)
Discussion started by: rudoraj
6 Replies

6. UNIX for Dummies Questions & Answers

need to zip all the files excluding todays file

HI All, At present in my server the log folder was filled up and causing memory issue. So I am planning to write a script in such a way that the files which are older than 30 days will be deleted and also need to find the files which were not compressed and need to compress this file.... (4 Replies)
Discussion started by: arumilli
4 Replies

7. Shell Programming and Scripting

Searching for a string in .PDF files inside .RAR & .ZIP archives.

Hi, I have got a large number of .PDF files that are archived in .RAR & ZIP files in various directories and I would like to search for strings inside the PDF files. I would think you would need something that can recursively read directories, extract the .RAR/.ZIP file in memory, read the... (3 Replies)
Discussion started by: lewk
3 Replies

8. UNIX for Dummies Questions & Answers

Zip files inside the directory, but not the directory itself

Hi, Im facing a problem that im stucked, I have the following structure: thales@pereirtc-vbox:/home/VfARM$ ls code config doc lib manifest.bak manifest.rel manifest.v3 ns pub if i try to execute zip -q -o arm.zip VfARM/* it will create a zip file with the folder VfARM.... (2 Replies)
Discussion started by: Thales.Claro
2 Replies

9. Shell Programming and Scripting

Replace a string in all files under a directory and its subdirectories

Hello Friends, I've been trying to write a script which finds a string and change it with another string. For this i want to search all files (with its arguments) under a spesific directory and its subdirectories. For example lets assume i want to replace an IP= 192.168.0.4 with another... (4 Replies)
Discussion started by: EAGL€
4 Replies

10. UNIX for Dummies Questions & Answers

Find Files in a Directory Excluding Subdirectories

Hi, I have a filename Location.txt in a directory /abc. Similar name file is present in its subdirectory /abc/xyz. I want to find the file which is present only in /abc and not in /abc/xyz. Please any1 of u can provide a quick suggestion. Its very urgent. Thanks, Amol (2 Replies)
Discussion started by: Amol_Dicholkar
2 Replies
Login or Register to Ask a Question