Help with naming the file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with naming the file
# 1  
Old 07-02-2015
Help with naming the file


I have a folder that contains files

....and so on

Inside abc.txt, I have

Inside def.txt, I have

I want to rename
abc.txt to abc.txt4
def.txt to def.txt160

Can someone help me with this.


Last edited by rbatte1; 07-02-2015 at 09:26 AM..
# 2  
Old 07-02-2015
for file in *.txt
  { read; IFS=\| read nr rest;} < "$file"
  if [ "$nr" ]; then
     mv "$file" "${file}${nr}"

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-02-2015
Hello rossi,

A few questions to clarity thing first:-
  • Is there anything else in the file apart from these two lines?
  • Can I just confirm that you want to suffix the file name with the first field of the second line using a pipe | as the delimeter?
  • Are there many, many files?
  • What have you tried so far?
  • What errors have you got?
  • What shell are you using?
  • What OS and version do you have running?
  • What tools are you most comfortable with?
  • Does the code from Scrutinizer solve it?

Sorry it's so many, but i don't want anyone to shoot off on the wrong track.

Kind regards,
# 4  
Old 07-02-2015
The code works fro this problem.
Can you help in solving this issue
suppose the file name is

Assuming that kslkd has 12 inside the file

I want the output like


Last edited by rbatte1; 07-02-2015 at 10:01 AM.. Reason: Changed ICODE tags to CODE tags
# 5  
Old 07-02-2015
So drop the .txt then?

Try this:-
for file in *.txt
  { read; IFS=\| read nr rest;} < "$file"
  if [ "$nr" ]; then
     mv "$file" "${file%.txt}${nr}"

# 6  
Old 07-02-2015
Hi rabtte1

The code from Scrutinizer works for the problem I posted.
  1. Yes there are more lines in the file, but I want the number just from the 2nd line.
  2. I want the number before the | delimiter
  3. There are about 100 files
  4. I tried python but couldn't solve the issue

I have to drop the numbers before *.txt as well

Last edited by rbatte1; 07-02-2015 at 12:33 PM.. Reason: Replaced text numbered list with LIST=1 tags. Added ICODE tags for pipe symbol
# 7  
Old 07-02-2015
Try - in the code snippets posted above - this:
mv "$file" "${file%%[0-9]*}${nr}"

Last edited by RudiC; 07-09-2015 at 07:27 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Naming file with special characters ?

Hi all, I have a problem with file naming in linux. I have to create a file named like 11/22.csv but shell displays error: no such file or dir. Problem is / character in the file name. I searched unix linux naming concepts and it's restricted in OS. Please tell me if there's any other chance? OS... (3 Replies)
Discussion started by: sembii
3 Replies

2. Shell Programming and Scripting

file naming in a script

#!/bin/bash while read inputline do what="$inputline" echo $what; if ; then exit fi $reextend $what $print ls -a done this is my code i am trying to change all of the file types of a certain directory to another file type but im not all the way there can someone help please (3 Replies)
Discussion started by: domdom110
3 Replies

3. Red Hat

File System Naming Convention

Hi, I am installing a new RHEL 5 application server containing JBOSS along with other specific 3rd party applications. I know that this usually gets installed in /opt but I was thinking of installing these on a new separtate lv / file system instead. i.e. /<my_new_FS_name> rather than... (6 Replies)
Discussion started by: Duffs22
6 Replies

4. Shell Programming and Scripting

File naming format

Hi, su - oracle<<EOC export PATH=${PATH}:${ORACLE_HOME}/bin exit EOC set `sqlplus -S $user_name/$password@$tns<<EOS set head off select min(time),max(time) from products; exit; EOS` var1=$1 var2=$2 su - oracle -c "exp user/pass@localdb... (7 Replies)
Discussion started by: milink
7 Replies

5. Shell Programming and Scripting

File splitting, naming file according to internal field

Hi All, I have a rather stange set of requirements that I'm hoping someone here could help me with. We receive a file that is actually a concatenation of 4 files (don't believe this would change, but ideally the solution would handle n files). The super-file looks like:... (7 Replies)
Discussion started by: Leedor
7 Replies

6. Shell Programming and Scripting

file naming question

Hi, I need some help! I have a file in which i im splitting into 20 different files each called etc... i would like to make directory for each file using only the name and not the extension so that the directory names are model_001 model_002 etc. ... (8 Replies)
Discussion started by: olifu02
8 Replies

7. Shell Programming and Scripting

#file naming

hi all, Please advise at what circumstance those file will become -rwxr-xr-x 1 psa psa 1969088 Aug 18 2006 -rwx------ 1 psa psa 2166784 Jul 25 2006 -rwx------ 1 psa psa 904040 Jul 25 2006 -rwx------ 1 psa ... (2 Replies)
Discussion started by: rauphelhunter
2 Replies

8. Shell Programming and Scripting

issue in naming a file

Hi, I want to create a file named 'abc(+1)' and append the data of file 'abc' to it. But getting error as unexpected'(' when i tried to use the following command. cat abc > abc(+1) Is there any other way to include brackets along with +1 in the file name? TIA. (3 Replies)
Discussion started by: vimalr
3 Replies

9. Shell Programming and Scripting

Shell Script for file naming

Hi All, I am looking for a Unix shell script for file naming such that the file names itself as KARAN0001. The 4 digit sequence number must start at 0001 and end at 9999. After 9999 is reached, the number must reset to 0001. Can anyone please help me with that. Thanks & Regards ... (2 Replies)
Discussion started by: karansachdeva
2 Replies

10. Shell Programming and Scripting

naming a file to hostname

I am running a script remotely to another client. after it runs it places the file in /tmp. I need the file in /tmp to be renamed to the local hostname. but when i set the variable it names the file to my local hostname. how do i fix that (4 Replies)
Discussion started by: deaconf19
4 Replies
Login or Register to Ask a Question

Featured Tech Videos