How to move files with first part as new filenamevia bash shell scripting?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to move files with first part as new filenamevia bash shell scripting?
# 1  
Old 09-12-2018
How to move files with first part as new filenamevia bash shell scripting?

I am on linux red hat, want to use the logic within bash shell script file.

I have the following type, named files in folder /staging, want to move with new filename just the firstpart upto underscore, example 20180904105056.dat, how can i get upto first part of filename and validate to chk if filename exists then append filename with sequential number at end.

if the file already exists in target folder want to append with _2, _3, _4 so on so forth.
Code:
#sample file names in source folder, these guys using space in filenames too ,
20180904105056_ss043883_prod_0defaul0000664.dat
20180904105056_ss043883_prod_0defaul0000664 2.dat

mv /staging/*.dat /targetfolder/20180904105056.dat
mv /staging/*.dat /targetfolder/20180904105056_2.dat #since here in target already filename exists.

thank you very much for the helpful info.
# 2  
Old 09-12-2018
Try
Code:
for FN in *.dat; do echo mv $FN /targetfolder/${FN%%_*}.${FN##*.}; done

Would the --backup=numbered option to mv - should it exist on your system, which btw you do not mention - satisfy your needs?
# 3  
Old 09-12-2018
Quote:
Originally Posted by RudiC
Try
Code:
for FN in *.dat; do echo mv $FN /targetfolder/${FN%%_*}.${FN##*.}; done

Would the --backup=numbered option to mv - should it exist on your system, which btw you do not mention - satisfy your needs?
Given that one of the sample filenames contains a <space> character (and not knowing whether or not there might be other field separators in the various parts of the real filenames that will be processed by this code) we need to add at least one pair of double-quotes and a second pair would probably be safer:
Code:
for FN in *.dat; do echo mv "$FN" "/targetfolder/${FN%%_*}.${FN##*.}"; done

This User Gave Thanks to Don Cragun 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

Bash to move specific files to directory based on match to file

I am trying to mv each of the .vcf files in the variants folder to the folder in /home/cmccabe/f2 that the .vcf id is found in file. $2 in file will always have the id of a .vcf in the variants folder. The line in blue staring with R_2019 in file up to the -v5.6 will always be an exact match to a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Need BASH Script Help to Move Files While Creating Directories

I've got this script to loop through all folders and move files that are more than 2 years old. I'm using the install command because it creates the necessary directories on the destination path and then I remove the source. I'd like to change the script to use the mv command since it is much... (4 Replies)
Discussion started by: consultant
4 Replies

3. Shell Programming and Scripting

Bash to move specific files from folders in find file

I have a directory /home/cmccabe/nfs/exportedReports that contains multiple folders in it. The find writes the name of each folder to out.txt. A new directory is then created in a new location /home/cmccabe/Desktop/NGS/API, named with the date. What I am trying to do, unsuccessfully at the moment,... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. UNIX for Dummies Questions & Answers

Learn bash shell scripting

I do not know shell scripting. But at work place, I have got an in and out shell scripting task. I just need to understand a very big script. Is there any tool in which I can place the script and it can tell me the meaning of the whole script? (3 Replies)
Discussion started by: lg123
3 Replies

5. UNIX for Dummies Questions & Answers

BASH Shell Scripting: If, Then Statement

I'm having trouble trying to create a BASH shell script. I want the user to input a command "cat file_name.c" and then the shell script will delete all comments "/* */" from file_name.c else exit. So far I have this: #!/bin/bash read "cat file" // User will input command cat... (7 Replies)
Discussion started by: inkjoy00
7 Replies

6. Shell Programming and Scripting

Bash shell scripting doubt

Hello All, I am setting up a cron job, where i am calling a shell script to make few builds. I got struck at a point, need some expert inputs to proceed further. The script is categorized in 5 parts and in the last part while building software it asks for few questions like:- 1. Build mode... (4 Replies)
Discussion started by: sahil_jammu
4 Replies

7. Homework & Coursework Questions

Help with bash shell scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a bash shell script that takes as an argument a name. Then your script will ask the user for two numbers.... (15 Replies)
Discussion started by: boyboy1212
15 Replies

8. Shell Programming and Scripting

Bash shell Scripting help

wwww wwwwwwww wwwwwwwwwwwww (0 Replies)
Discussion started by: keyvan
0 Replies

9. Shell Programming and Scripting

Help!! bash shell scripting..

Can any1 please help me... i'm really lost in using bash shell scripting... and i got to hand this up on monday... please anyone teach me how to do this assignment... Please use basic things because i just learn the program only... thanks ... (1 Reply)
Discussion started by: Fr0z3n999
1 Replies

10. Shell Programming and Scripting

bash shell scripting

Hi, i am new to UNIX. I have couple of basic questions. 1. Is the syntax for BASH shell programming same in the LINUX and SUN SOLARIS operating systems? 2. I have to work on BASH shell programming in SUN SOLARIS operating system. I am going through the documentation from the following... (4 Replies)
Discussion started by: azazalis
4 Replies
Login or Register to Ask a Question