Visit Our UNIX and Linux User Community


Script to List, Modify, replace filename for an upload?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to List, Modify, replace filename for an upload?
# 1  
Old 09-16-2011
Script to List, Modify, replace filename for an upload?

Hello, here is my problem:

I have ma program in a first directory dir1:
Code:
ls path1/rep1/
file1.f90 file1.f90~ file1.o file2.f90 .... etc...

I have modified folder in an other directory:
Code:
ls path2/rep2/
file1_modified.f90 file2_modified.f90 .... etc...

All files from first directory are not all in the second (only the ones that are modified) but they have the same name plus "modified" at end.

I want to make a short script to:
1- save files of 1st directory from file1.f90 to file_old.f90 if the upload didn't work.

2- copy all files from 2nd directory to the first and change their name as needed.

Do you know how to do this and manipulate filename string and path to do that ?

thanks for your help Smilie
# 2  
Old 09-16-2011
Quote:
Originally Posted by shadok
1- save files of 1st directory from file1.f90 to file_old.f90 if the upload didn't work.
What upload? Are the _modified files the ones arriving, being uploaded?
# 3  
Old 09-16-2011
Sorry, I was not very clear Smilie

What I want is that:

step 1:
in path1/rep1/
Code:
cp file1.f90 file1_old.f90

but for all f90 files and i don't know how to easily manipulate filename string

step2:
Code:
cp /path2/rep2/file1_modified.f90 path1/rep1/file1.f90

and i want to do it for all file.f90 of the rep2 automatically.

so file1.f90 will be replace in the second step.

How should I modify the string filename ?
# 4  
Old 09-17-2011
This script would do the job:

Code:
#!/bin/sh
for mod_file in path2/rep2/*_modified.f90
do
        base_name=$(basename ${mod_file});
        base_part=${base_name%_modified*};
        target_file=path1/rep1/${base_part}.f90;
        if [ -f $target_file ]
        then
                save_file=path1/rep1/${base_part}_old.f90;
                cp ${target_file} ${save_file}
        fi
        cp ${mod_file} ${target_file}
done

One detail - if you don't want to copy a modified file which does not exist in the path1/rep1 then move the last copy statetment inside the if - fi bracket right after the copy target to save old file statement.

If you are learning shell scripting pay attention to string manipulation (see how base_part of the file name is extracted) it comes handy quite often.
# 5  
Old 09-21-2011
Hello,

I tried your solution but apprently, the test doesn't work well since no action that have to be done if the "if" test are done.

I tried to replace the test from
Code:
if [ -f $target_file]

to

Code:
if [test -f $target_file]

but it says
Code:
./upload.sh: 25: [test: not found

So, what should I do to test properly the existence of the file ?

thanks for your hellpull help!! Smilie Smilie
# 6  
Old 09-21-2011
Quote:
Originally Posted by shadok
Hello,

I tried your solution but apprently, the test doesn't work well since no action that have to be done if the "if" test are done.

I tried to replace the test from
Code:
if [ -f $target_file]

to

Code:
if [test -f $target_file]

but it says
Code:
./upload.sh: 25: [test: not found

So, what should I do to test properly the existence of the file ?

Both [ and test are the same command.

Code:
if test -f "$target_file"

or:
Code:
if [ -f "$target_file" ]  ## spaces are required

# 7  
Old 09-21-2011
THANKS ! it works perfectly now !

Smilie Smilie

But why thes space are so important ? Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #552
Difficulty: Medium
If variable i has the value 10, after x = 5 + (++i); the value of x after this code is 15.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To replace a filename

Hi i want to replace date stamp filename to original file name. i want to remove the datestamp after .txt For eg: file_name.txt_01-1002014 output: file_name.txt tried with below one but not able to achieve mv file_name.txt_01-1002014`basename "file_name.txt_01-1002014 "`.txt (4 Replies)
Discussion started by: rohit_shinez
4 Replies

2. Shell Programming and Scripting

Help shell script to list filename file_path

HI owner date and time and size of file in a row shell script to list filename file_path i have tried the below code present_dir=`pwd` dir=`dirname $0` list=`ls -lrt | awk {'print $9,$3,$6,$7,$8'}` size=`du -h` path=`cd $dir;pwd;` printf "$list" printf "$path" printf " $size" ... (4 Replies)
Discussion started by: abiram
4 Replies

3. Shell Programming and Scripting

sed to replace pattern with filename

Hi all, I'm trying to replace a pattern/string in about 100 files with the filename using following commands but getting nowhere: for f in *.fa; do sed "s/^>.*/>$f/g" $f > $f_v1.fa; done for f in *.fa; do sed 's/^>.*/>`echo $f`/' > $fa_v1.fa; done Basically I want to change any line... (5 Replies)
Discussion started by: ivpz
5 Replies

4. Shell Programming and Scripting

Modify sources.list from script

i'm looking for a way to enable and disable repositories from a script. i started with a sed command like this: sed '/*'"$REPO"'/,/'"$STOP"'/ s/^*//' /etc/apt/sources.list but this enables both the normal and the source repos. for example: # deb http://www.lamaresh.net/apt lenny main #... (7 Replies)
Discussion started by: Leppie
7 Replies

5. Shell Programming and Scripting

List files and display last modify time in a particular format

hi everyone, can someone suggest how i can list the contents of a directory and display their corresponding last modify time in the format yyyymmddhhmm? thanks in advance! (16 Replies)
Discussion started by: Deanne
16 Replies

6. Shell Programming and Scripting

Replace text with filename

Hi, I have a few thousand files, each contains the word "hello". Would it be possible to do a batch job that replaces the word hello with the filename? Thanks. (2 Replies)
Discussion started by: calrog
2 Replies

7. UNIX for Dummies Questions & Answers

Replace all files with a certain filename with another file

I tried searching for this, but I might have used the wrong terms as I couldn't find answers to this question. I'm looking for a way to replace all files with a certain filename with another file within a specific directory including all of it's subdirectory using a shell-script. (2 Replies)
Discussion started by: Schmellsera
2 Replies

8. Shell Programming and Scripting

Request to modify script to list multiple parameters for V_fieldid variable

I am posting a script below which essentially excutes the following functions in the described order. 1) From a source directory pools together three files generated by system logs for each user session, tar's these files and archives them as a log set in a destination directory and these... (0 Replies)
Discussion started by: Sammy
0 Replies

9. Shell Programming and Scripting

read a part of filename from the list in the script

how can i read a part of filename from the list in the script? all file in directory...will start with "CDBACKUPFILE" then the name is stored in list.txt such as JOHN,MARRY,PETER. After this, is seq number. CDBACKUPFILEJOHN00001 CDBACKUPFILEMARRY00004 CDBACKUPFILEPETER00003 I will use:... (3 Replies)
Discussion started by: happyv
3 Replies

10. 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

Featured Tech Videos