Add 0 in start of filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add 0 in start of filename
# 8  
Old 04-08-2013
Like I said before, the code was based on some assumptions.

I assumed your file names are abcd1.txt, abcd2.txt ... abcd10.txt

Here is the modified code that might work for you:
Code:
#!/bin/bash

for file in *.txt
do
        num="${file%%-*}"
        nam="${file##*-}"
        printf "%02d-%s\n" $num $nam
done

This User Gave Thanks to Yoda For This Post:
# 9  
Old 04-08-2013
Quote:
Originally Posted by Yoda
Like I said before, the code was based on some assumptions.

I assumed your file names are abcd1.txt, abcd2.txt ... abcd10.txt

Here is the modified code that might work for you:
Code:
#!/bin/bash

for file in *.txt
do
        num="${file%%-*}"
        nam="${file##*-}"
        printf "%02d-%s\n" $num $nam
done

Thanks so much sir.
# 10  
Old 04-08-2013
Code:
for file in *.txt
do
  [[ ${file%%-*} -ge 10 ]] && continue
   echo "mv $file 0$file"
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 11  
Old 04-08-2013
For the names initially given in this thread, a simple way to do what was requested is:
Code:
for i in [0-9]\ *
do      mv "$i" "0$i"
done


Last edited by Don Cragun; 04-08-2013 at 11:53 PM.. Reason: Add the required quotes...
# 12  
Old 04-08-2013
Quote:
Originally Posted by Don Cragun
For the names initially given in this thread, a simple way to do what was requested is:
Code:
for i in [0-9]\ *
do      mv $i 0$i
done

That's a nice solution, but both of mv's arguments require double quotes.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 13  
Old 04-08-2013
Quote:
Originally Posted by alister
That's a nice solution, but both of mv's arguments require double quotes.

Regards,
Alister
Yes, obviously. Smilie

I have updated my post to include the quotes.

- Don
# 14  
Old 04-09-2013
Quote:
Originally Posted by Don Cragun
For the names initially given in this thread, a simple way to do what was requested is:
Code:
for i in [0-9]\ *
do      mv "$i" "0$i"
done

Giving errors. Please check my question. I want 1-abcd1 to 01-abcd1 till 09-abcd9 but not to add in file that have 10 in start.

Code:
mv: cannot stat `[0-9] *': No such file or directory

---------- Post updated at 02:43 AM ---------- Previous update was at 02:34 AM ----------

Quote:
Originally Posted by ahamed101
Code:
for file in *.txt
do
  [[ ${file%%-*} -ge 10 ]] && continue
   echo "mv $file 0$file"
done

--ahamed
Thanks Ahamed brother. But in above script we have added 0 from 1 till 9 and file names are

Code:
01-abcd1.txt
02-abcd2.txt
...
...
09-abcd9.txt

If i will move them back to 

1-abcd1.txt
2-abcd2.txt
...
...
9-abcd9.txt

I have tried below code but not working. Please advice.

for file in *.txt
do
  [[ ${file%%-*} -ge 10 ]] && continue
   echo "mv 0$file $file"
done

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