Renaming file - Query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming file - Query
# 1  
Old 10-16-2015
Renaming file - Query

Hi,

I have file with format
Code:
abcd.gz.20151011.1

, i need to rename as
Code:
abcd.gz.20151011

.we can use move command for one file. How to change multiple files.

Last edited by Scrutinizer; 10-16-2015 at 04:40 AM.. Reason: code tags
This User Gave Thanks to nag_sathi For This Post:
# 2  
Old 10-16-2015
Adapt to suit your requirement:
Code:
for file in /dir/filepattern*
do
    name_before_last_period=${file%\.*}
    mv $file ${name_before_last_period}
done

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 10-16-2015
Hello nag_sathi,

Could you please try following and let me know if you have queries.
Code:
for file in abcd.gz.*
do
 echo mv $file ${file%\.*}
done

Above will check for all files like abcd.gz.* if you want to check all .gz files then following may help in same.
Code:
for file in *.gz.*
do
 echo mv $file ${file%\.*}
done

Also remove echo in above solutions if you are happy with results then it will rename them once you remove it.

NOTE: Also you should run this in the directory where you have files, else you could mention complete path like /path/to/files/*.gz.* or /path/to/files/abcd.gz.*.


Thanks,
R. Singh

Last edited by RavinderSingh13; 10-16-2015 at 04:21 AM.. Reason: Added a comment for solution now
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 10-16-2015
Note: the backslash escape before the dot (\.) is not necessary since this is not regex and the dot does not have a special meaning.
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming file and check for the renamed file existence

Hi Am trying to move a file from one name to another When I do "ls" to check for the moved filename I can see the file but when I try the same with a script am unable.. I think am doing some pretty silly error.. please help.. toMove=`ls | grep -E "partition.+"` mv $toMove partition._org... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

2. UNIX for Dummies Questions & Answers

renaming a file

Hi I remember i have create a file "Dechistory" before a month. But i dont remember exact location. I know i need to find out first that fiiles exact location for renaming it .......but i waant to that without going on that location.......how to do that ??? (1 Reply)
Discussion started by: tusharjoshi
1 Replies

3. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

4. UNIX for Dummies Questions & Answers

Need help with Renaming a file

I have a file named as Pro_PatAct_MMDDYYYY.csv. I need to renmae it to this Pro_PatAct.csv without the date timestamp. Can someone help me to achieve this using a regular expn. (3 Replies)
Discussion started by: imran_affu
3 Replies

5. Shell Programming and Scripting

Renaming file

Hello, I have an outstanding issue..Iam on linux and using a putty to connect to my server and then fire our unix shell script. At location /usr/sam a file called "er 1 32.txt" out boss transfer via application. From my end on terminal when i want to transfer this file to some other location... (2 Replies)
Discussion started by: j_panky
2 Replies

6. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

7. Shell Programming and Scripting

Renaming a file use another file as a sequence calling a shl

have this shl that will FTP a file from the a directory in windows to UNIX, It get the name of the file stored in this variable $UpLoadFileName then put in the local directory LocalDir="${MPATH}/xxxxx/dat_files" that part seems to be working, but then I need to take that file and rename, I am using... (3 Replies)
Discussion started by: rechever
3 Replies

8. Shell Programming and Scripting

file renaming

How can I rename files named like these iq - 000001 - 2008.07.31 - 14.49.47 - location1.bin iq - 000001 - 2008.07.31 - 14.49.47 - location12.bin iq - 000008 - 2008.07.31 - 14.52.01 - location500.bin to iq_2008.07.31_14.49.47_location1.bin iq_2008.07.31_14.49.47_location12.bin... (7 Replies)
Discussion started by: larne
7 Replies

9. UNIX for Dummies Questions & Answers

Help in renaming file !!!

Hi All, I want to rename a file inside a script which has a date portion appended at the start of the file name. The script i wrote works fine when the file comes on a day to day basis but sometimes it comes late too. #!/usr/bin/ksh cd /space/file/source dt=$(date "date "+%m%d%Y")... (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies

10. UNIX for Dummies Questions & Answers

Renaming a file to the same name

Hi All, I was wondering if anyone knows how i take in a file, format the file, and then rename the file as the same name as the input file in shell script. Here is an example of what I am doing: if ] then echo "please enter a filename" else export infile=$1 I take in a file... (3 Replies)
Discussion started by: lachino8
3 Replies
Login or Register to Ask a Question