Add 0 in start of filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add 0 in start of filename
# 1  
Old 04-08-2013
Add 0 in start of filename

Code:
1 - abcd1.txt
2 - abcd2.txt
.
.
10  - abcd3.txt

need to write a script to rename all files that start with a number less than 10, to have a zero in front. ie. 1 - abcd1.txt will be renamed to 01 - abcd1.txt.
# 2  
Old 04-08-2013
Show what you tried first.
# 3  
Old 04-08-2013
Currently nothing in mind that's why ask question here for help. I think need to use find command but stuck in checking of less than 10.
# 4  
Old 04-08-2013
Show your find command etc., show your code.
# 5  
Old 04-08-2013
Nothing yet started, because this checking thing sucks and stop my mind that's why.

---------- Post updated at 01:10 PM ---------- Previous update was at 01:04 PM ----------

Please suggest this one.

Code:
#!/bin/bash
#
for i in *.txt
  do mv "$i" "0-${i/.txt}"
done

# 6  
Old 04-08-2013
Based on some assumptions:
Code:
#!/bin/bash

for file in *.txt
do
        num="${file//[[:alpha:].]/}"
        nam="${file//[[:digit:].txt]/}"
        printf "%s%02d.txt\n" $nam $num
done

If this works for you, assign printf result to a variable and perform the rename.
# 7  
Old 04-08-2013
It is giving below output.

Code:
sh1.sh: line 7: printf: 10-3: invalid number
-abcd10.txt
sh1.sh: line 7: printf: 1-3: invalid number
-abcd01.txt

I need to change 1-abcd1.txt to 01-abcd1.txt and 2-abcd2.txt to 02-abcd2.txt and do till 09-abcd9.txt not do only 10
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add part of directory name to filename

Hello, I need to add a part of folder name to the files inside it. For instance the file is HMCBackup_20150430.155027.tgz and it is under directory /nim/dr/HMCBackup/cops22 I need to add cops22 to the file name so as it would be cops22_HMCBackup_20150430.155027.tgz Any help in doing... (10 Replies)
Discussion started by: hasn318
10 Replies

2. Shell Programming and Scripting

How to add a datetime stamp at a particular position in a filename?

hi, i have some files in a directory say abc.txt def.txt ghi.txt i am storing these file names in a temp file. ls -l | grep "^-" | awk '{print $NF}' > temp_file$$ i want to add a date time stamp at a particular place in the file names. it can be 1) before the extension... (2 Replies)
Discussion started by: Little
2 Replies

3. Shell Programming and Scripting

Script to add extension to filename

Hi all, I have a folder with a bunch of files in them, and I would like to add an extension (.mp3)to all these filenames. The folder has only files that I'd like .mp3 added to. It looks something like this: Intput: File1 File2 File3Output: File1.mp3 File2.mp3 File3.mp3Thanks in... (2 Replies)
Discussion started by: repiv
2 Replies

4. Shell Programming and Scripting

How to add filepath to filename when moving it?

Hi I have a cron job setup that searches for 'core dump' files in a fixed set of directories and moves the core files to a separate file system. I am able to add date and time info to the file name. How can I add the source directory path to the file name? In case anyone is wondering why I... (5 Replies)
Discussion started by: dexter126
5 Replies

5. Shell Programming and Scripting

replace and add characters in filename

I have files named like ABAB09s099E1AAV1.pdf and ABAB09s099E2AAV1.pdf in a directory. I need to add _Lop in the end of the name, like ABAB09s099E1AAV1_Lop.pdf for all files. For files with E2 in the name I also have to replace 99 with 88 in the filename, that would be ABAB09s088E2AAV1_Lop.pdf ... (3 Replies)
Discussion started by: hakkar
3 Replies

6. Shell Programming and Scripting

How to add filename to the end of each line in file

Hi, I'm reading data from comma separated files to DB. Now there is a need to have the name of the input file in each row of that file. How can I do this in unix script? Example: $cat file1 value11,value12, value,13 value21,value22, value,23 value31,value32, value,33 And the result... (2 Replies)
Discussion started by: tmikahan
2 Replies

7. Shell Programming and Scripting

how can i add/modify filename after output?

Hi All, I have a script to convert a file and output the filename with "_output", however it not work. Can help? echo Please input list file name: read listn for file in `cat $listn.txt` do convert $file > $file_output Thanks all!! (3 Replies)
Discussion started by: happyv
3 Replies

8. Shell Programming and Scripting

add timestamp to filename

Does anyone know how to add a timestamp to a file's name extension in a shell script? Please help.. (3 Replies)
Discussion started by: walterja
3 Replies

9. UNIX for Dummies Questions & Answers

Add date to a filename

Hi I want to add the date to a filename in a script I have. So I want exp myfile01-FEB-2005.dmp How do I get the 01-FEB-2005 in there? Cheers Rich (3 Replies)
Discussion started by: richard1975
3 Replies

10. UNIX for Dummies Questions & Answers

how to add a timestamp to a filename?

whats going on guys. below is a script i made and am just curious if there is a "time stamp" command. so i can set the timestamp in a filename. #! /bin/ksh # # This scripts takes a list of files in the INDIR variable and compairs it to a list of files that are open in the same directory.... (2 Replies)
Discussion started by: Optimus_P
2 Replies
Login or Register to Ask a Question