Print and Append the matching filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print and Append the matching filenames
# 1  
Old 07-25-2011
Print and Append the matching filenames

Hi,

I want to check all files in a folder for some specific start string and append all matching filenames with _1, _2 .... for each file found in the directory.
But, $file below gives me all details of the files like access permissions, owner and filename etc. I just want all the filenames and I hope the blow code shoudl work ??

Code:
i=1
for file in `ls -l |grep "$filename"`
do 
echo "$file"
mv $file $file_i
i=`expr $i + 1`
done



Kindly help.

Chetan Vyas

Last edited by radoulov; 07-25-2011 at 06:25 AM.. Reason: Code tags.
# 2  
Old 07-25-2011
Code:
i=1
for f in "$filename"*; do
  printf '%s\n' "$f"
  mv -- "$f" "$f"_$i
  i=$(( i + 1 ))
done

# 3  
Old 07-25-2011
This was what i was trying to do. Now i have done picking up the filenames and renaming all of them in a loop.



Code:
Code:
i=0
for file in `ls |grep "$filename"`
do
echo "$file"
echo $i
i=`expr $i + 1`
mv /var/tmp/akk2/${file} /var/tmp/akk2/${file}_${i}
done


Last edited by radoulov; 07-25-2011 at 06:59 AM.. Reason: Code tags!
# 4  
Old 07-25-2011
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

2. Shell Programming and Scripting

How to append timestamp in the filenames using find?

Hi, How to change the filenames with timestamp in sub folders I have the following code to select the records. find . -type f -name '*pqr*' -ctime 1 -print The following is the example app_root_dir="/`echo $ScriptDir | cut -d'/' -f2`" $app_root_dir/../BadFiles directory uvw.bad... (3 Replies)
Discussion started by: bobbygsk
3 Replies

3. Shell Programming and Scripting

Need to append matching strings in a file

Hi , I am writing a shell script to check pvsizes in linux box. # for i in `cat vgs1` > do > echo "########### $i ###########" > pvs|grep -i $i|awk '{print $2,$1,$5}'>pvs_$i > pvs|grep -i $i|awk '{print $1}'|while read a > do > fdisk -l $a|head -2|tail -1|awk '{print $2,$3}'>pvs_$i1 >... (3 Replies)
Discussion started by: nanduri
3 Replies

4. Shell Programming and Scripting

Matching filenames and modifying them internally

hi i am trying to match filenames and modify the files internally depending on the match the filenames are something like this cm01FEB2012bhav cm01AUG2012bhav ... Internally the file(cm01FEB2012bhav) looks like this 20MICRONS,EQ,64.1,65.7,62.45,63.7,64.5,64.1,113043,01-FEB-2012... (4 Replies)
Discussion started by: adarsh1993
4 Replies

5. Shell Programming and Scripting

print all filenames in directory with path to another file

hi, i have a directory at /path/unix with the following files 1.txt 2.txt 3.txt 4.txt I want to make another file called filenames.txt at a different location called /path/home. So, my output file would be /path/home/filenames.txt with contents /path/unix/1.txt... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

6. Shell Programming and Scripting

[awk] print all filenames in directory

Hello, I was given a (I suppose) a simple task which I don't know how to do. Before coming here I have read a dozen of awk tutorials (full read, not just line-skipping) but I still don't know how to do this. Task: Write an script file 'check.awk' with a parameter current directory that... (5 Replies)
Discussion started by: c0dehunter
5 Replies

7. Shell Programming and Scripting

Script template for inputting filenames and print results

Hi, Hope you are all well. New to scripting, and all those characters are all a new language for me. Though hoping to get my little head round it all sooner or later. I was wondering whether anyone could help with a script template example. What I would like to happen is to run the script... (8 Replies)
Discussion started by: loky27
8 Replies

8. Shell Programming and Scripting

How to print the last column of the list contain filenames with spaces.

Hi experts, I have the following data: I want the last filed in the output. How to print the last field , It contains the file names and few filenames with white spaces . -rw-r--r-- 1 root root 0 2010-04-26 16:57 file1 2space_File.txt -rw-r--r-- 1 root root 0 2010-04-26... (2 Replies)
Discussion started by: rveri
2 Replies

9. Shell Programming and Scripting

Print filenames with spaces using awk

Hello all, I want to list the file contents of the directory and number them. I am using la and awk to do it now, #ls |awk '{print NR "." $1}' 1. alpha 2. beta 3. gamma The problem I have is that some files might also have some spaces in the filenames. #ls alpha beta gamma ... (7 Replies)
Discussion started by: grajp002
7 Replies

10. Shell Programming and Scripting

Count matching filenames in a folder

Hi all, I have 4 files for example named abc01012009.txt abc02012009.txt abc03012009.txt abc04012009.txt in a folder. I would like to firstly backup the latest file available, in this case, the latest date available, abc04012009.txt to its subfolder named backup, and then rename the... (4 Replies)
Discussion started by: tententen
4 Replies
Login or Register to Ask a Question