Remove number in file name


 
Thread Tools Search this Thread
Operating Systems Solaris Remove number in file name
# 1  
Old 07-26-2016
Remove number in file name

Hi ,

i have a file name with date and time append like
Code:
test_SEC_AES_V1_T03_2016031404306

i want to remove the date and time after _ and rename to current date and time,can any one please let me know as i tried some options but din't helpSmilie


Thanks,



Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 07-26-2016 at 09:37 AM.. Reason: Added code tags.
# 2  
Old 07-26-2016
Please show your attempts.

And, please explain your date/time format 2016031404306:
2016 - year
03 - month
14 - day
04 - hour?
30 - minute?
6 - ?

Last edited by RudiC; 07-26-2016 at 09:41 AM..
# 3  
Old 07-26-2016
Quote:
#!/bin/bash

for i in /test/final/*.txt;
do

if [[ -e "${i%-*}.txt" ]]; then
num=1
while [[ -e "${i%-*}-$num.txt" ]]; do
(( num++ ))
done
mv "$i" "${i%-*}-$num.txt";

else
rename t.txt
fi
done
the above code is not working

---------- Post updated at 07:54 AM ---------- Previous update was at 07:43 AM ----------

yes 2016 - year
03 - month
14 - day
04 - hour
30 - minute
6 - sec

Last edited by caba_jones; 07-26-2016 at 09:44 AM.. Reason: add content
# 4  
Old 07-26-2016
Hello caba_jones,

Welcome to forums. While seeing your code I think you need to rename all txt files into your present directory. If this is the case then following may help you in same.
Code:
for file in *.txt; do echo mv $file ${file%_*}$(date +%Y%m%d%H%m)".txt"; done

Above code will write the move command, so if you are ok with this then you could remove echoin bold above code and could change the .txt files to as per your need.
EDIT: Adding a non-one liner form of solution above too.
Code:
for file in *.txt
do
   echo mv $file ${file%_*}$(date +%Y%m%d%H%m)".txt"
done

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-26-2016 at 09:59 AM.. Reason: Added a non-one liner form of solution too now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 07-26-2016
will this code rename files in same folder?
when i execute its not renaming in the same folder.can you please let me know.
# 6  
Old 07-26-2016
Quote:
Originally Posted by caba_jones
will this code rename files in same folder?
when i execute its not renaming in the same folder.can you please let me know.
Hello caba_jones,

Yes, it will rename the files in current folder where you are running the script or command as mentioned above. In case we want it to take care of a folder where we are not currently in then you should mention the complete path of those .txt files as follows.
Code:
for file in /tmp/test/*.txt       ######Basically mention complete path of .txt files in here.
do
   echo mv $file ${file%_*}$(date +%Y%m%d%H%m)".txt"
done

As mentioned before you could remove echo from your code in case you are happy with the printed commands by echo.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 07-27-2016
Quote:
Originally Posted by RavinderSingh13
Hello caba_jones,

Yes, it will rename the files in current folder where you are running the script or command as mentioned above. In case we want it to take care of a folder where we are not currently in then you should mention the complete path of those .txt files as follows.
Code:
for file in /tmp/test/*.txt       ######Basically mention complete path of .txt files in here.
do
   echo mv $file ${file%_*}$(date +%Y%m%d%H%m)".txt"
done

As mentioned before you could remove echo from your code in case you are happy with the printed commands by echo.

Thanks,
R. Singh
Code:
for file in ${DPATH}/*.txt.pgp; do  mv ${test}/${FN} ${FN%_*}$(date +%Y%m%d%H%m)"test.txt.pgp" ; done  
mv: cannot access (the above path)

i am not able to move the files even though i have 777 permission,can you please let me know any other reasons not able to move the file.



Moderator's Comments:
Mod Comment Seriously: Please use code tags as required by forum rules!

Last edited by caba_jones; 07-27-2016 at 10:32 AM.. Reason: Added code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove string between number and character

hello ! I have to remove string between a number and set of characters. For example, 35818 -stress - - -stress - - - - - - DB-3754 44412 caul kid notify DB-3747 54432 roberto -, notify DB-3725 55522 aws _ _int _ _classified 2_a _a 2_m _m 2_classified 2_search... (7 Replies)
Discussion started by: ManoharMa
7 Replies

2. Shell Programming and Scripting

Remove trailing number

I have some strings such as ABC1 ABC2 TYFASDD12 They will only have letters and numbers. In each case I want to remove the last digit? The lengths will vary. So a hard coded substr won't work. What do I do? if it doesn't end in a number, I don't want to remove any characters. (6 Replies)
Discussion started by: guessingo
6 Replies

3. UNIX for Dummies Questions & Answers

Remove char if not a number

I need to write a BASH script that takes a 2 character string and removes the second character if it is not a digit e.g. If the string is numberical value >9 e.g. string1 = '34' then leave string1 = '34'. However if the string is <10 e.g. string1 = '3X' then remove the second char (which... (7 Replies)
Discussion started by: millsy5
7 Replies

4. Shell Programming and Scripting

Remove last number from a file

Hi, I need to delete last number ( i.e 126) from the file which has below contents. ABC DEF "XYZ" 2837.5 3852.5 126 ABC DEF "XYZ" 2897.5 3852.5 126 ABC DEF "XYZ" 2957.5 3852.5 126 Please help. Thanks in advance. (17 Replies)
Discussion started by: gujrathinr
17 Replies

5. Shell Programming and Scripting

How to remove files with different release number?

Hi, I have many data files formatted in "dataName-versionNumber-releaseNumber.location.data" in a directory of Linux CentOS 6.2 system. My directory could have following files for example: graphicData1-1.2.3-2.0.0.mel.au.data graphicData1-1.2.3-2.0.1.mel.au.data... (14 Replies)
Discussion started by: hce
14 Replies

6. Shell Programming and Scripting

script for remove descending order number

hi all i want to remove some descending order number example : 1 100 200 135.00 Gk_wirs 1 1 100 200 136.00 Gk_wirs 50 1 110 210 138.00 Gk_wirs 60 1 100 200 136.00 Gk_wirs 57 ----> how to remove... (6 Replies)
Discussion started by: nithyanandan
6 Replies

7. Shell Programming and Scripting

remove the number from the String

I have string like 20091112_File_Name_40301.txt and i have a set of files in the directory with the same format . i want to write the ksh to rename the file ..... like eg 20091112_File_Name_40301.txt to File_Name.txt 20091112_abc_2343.txt to abc.txt... (6 Replies)
Discussion started by: gavemani
6 Replies

8. AIX

Remove APAR number from aix 5.3

Hi... Can i remove one APAR number from aix5.3.. If it is possible how to do.. Thanks.. (3 Replies)
Discussion started by: sumathi.k
3 Replies

9. Shell Programming and Scripting

remove a large number of user from oracle

Hi on solaris and oracle 10g2, I have number of users created in Oracle, I wonder if I have a list of the usernames will it be possible to remove the users quickly ? I want to keep the users access to system but oracle. some thing like shell script may be ?:confused: I am trying to... (4 Replies)
Discussion started by: upengan78
4 Replies

10. Shell Programming and Scripting

remove a colon and number and leaving the rest

just have a file 1:2333 2:-09393 ]3:45453 4:-09999 5:-09933 6:93939 question is to get output by removing colons as well as number before each colon (in bold) 2333 -09393 45453 -09999 09933 93939 (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question