copying files to new dir


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copying files to new dir
# 1  
Old 05-16-2009
Lightbulb copying files to new dir

Hello,

I'm new to shell scripting so I don't really understand what I'm doing wrong.
The script I'm trying to do saves all the files (*.c) on the current dir to a list and, one by one, copies them to a new one called Backup. The thing is, if there are already other versions of the files I'm copying it changes their names to 'file_name'_000 or 001, and so on...

For instance, if I copy the file aaa.c to the Backup dir and there is already one aaa.c in it, the script changes the name of the copied file to aaa.c_001!

Here's the script...

if [ $# -eq 0 ]

then
lista_actual=*.c

for h in $lista_actual
do

nome_actual=$h
if [ ! -e Backup/$nome_actual_001 ]
then
cp $nome_actual Backup/$nome_actual_001

else

y=Backup/$nome_actual*
i=1
for j in $y
do i=$(($i + 1))
done
x1=$((i/100))
aux=$((x1%100))
x2=$((aux/10))
cp $nome_actual Backup/$nome_actual_$x1$x2$i
fi
done

fi

So... can you help me out? :P
# 2  
Old 05-16-2009
Just a snippet:

Code:
for file in *.c
 do if [ -e backdir/$file ]
 then 
  mv "$file" backdir/"$file"_001; 
 else 
  mv "$file" backdir/"$file"
 fi; 
done

cheers,
Devaraj Takhellambam
# 3  
Old 05-16-2009
A simple suggestion for your for loop is:
Code:
for nome_actual in ${lista_actual}; do

  nome_number=1
  while [ -e Backup/${nome_actual}_${nome_number} ]; do
    nome_number=`expr ${nome_number} + 1`
  done
  cp ${nome_actual} Backup/${nome_actual}_${nome_number}
    
done

It does not give you the leading zeros you were using and all the backup files will have a number suffix but otherwise fits the bill and keeps things simple.
# 4  
Old 05-20-2009
Thank you both, particularly you Tony! Smilie

One thing, on my script if I do echo $nome_actual_$x1$x2$i * it only prints $x1$x2$i (the numbers), but if I do like you suggested echo ${nome_actual}_$x1$x2$i ** it prints both the file name and the numbers...

* the result would be, in example, 002.

** in this case it shows xx.c_002 like it is supposed to!

Why? What's the purpose of the { ... }?
# 5  
Old 05-20-2009
The braces { and } denote that what is inside them is the variable name, it is good practice to always use them for variables in shell scripts.
# 6  
Old 05-20-2009
Ah! I thought the $ did that already... Thanks once again! :P
# 7  
Old 05-21-2009
You are correct, I did not make myself clear, the dollar says here follows a variable, the braces show the start and end of the variable name.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Assign read write permission to the user for specific dir and it's sub dir and files in AIX

I have searched this quite a long time but couldn't find the right method for me to use. I need to assign read write permission to the user for specific directories and it's sub directories and files. I do not want to use ACL. I do not want to assign user the same group of that directories too.... (0 Replies)
Discussion started by: blinkingdan
0 Replies

2. Shell Programming and Scripting

PERL - Copying ONLY files from one dir to another

I'm writing a Perl script which has its 1st step as to copy files from one directory to another directory. The Source directory has got files with extension, without extension, directories etc. But I want to copy ONLY files with no extension. The files with extensions and directories should not get... (2 Replies)
Discussion started by: jhamaks
2 Replies

3. Shell Programming and Scripting

Issue with copying files into dir inside for loop

Hi , I'm trying to move/copy the files inside the loop into a directory . I tried the below code and the issue is the data is not copying into the created directory but the files are copying into another file file_path="/home/etc" Last_Day=20130930 mkdir $file_path/ARC_${Last_Day} ... (3 Replies)
Discussion started by: smile689
3 Replies

4. UNIX for Dummies Questions & Answers

copying the dir/subdir structure from one server to another?

Hi All, I want to copy the dir/subdir structure from SERVER-A to SERVER-B without copying all the files in each dir. Is it possible using SCP / SFTP command? For example, SERVER-A has following two dir/subdirectories and files under each subdir. ... (1 Reply)
Discussion started by: Hangman2
1 Replies

5. Shell Programming and Scripting

Copying files to new dir structure.

I am trying to figure out a way to script copying specific files from one dir structure to another. I have a dir structure like this: dira/author 1/book 1/file a.epub /book 2/file b.epub /author 2/book 1/file c.epub /author 3/book 1/file d.epub /book 2/file... (2 Replies)
Discussion started by: arcanas
2 Replies

6. Shell Programming and Scripting

KSH - Find paths of multiple files in CC (dir and sub-dir))

Dear Members, I have a list of xml files like abc.xml.table prq.xml.table ... .. . in a txt file. Now I have to search the file(s) in all directories and sub-directories and print the full path of file in a output txt file. Please help me with the script or command to do so. ... (11 Replies)
Discussion started by: Yoodit
11 Replies

7. Shell Programming and Scripting

Copying specific files from one dir to another

Hi Folks, I have one curious case. There are list of following files placed in one directory such as... And updated each month. files.JAN09.csv files.FEB09.csv files.MAR09.csv ..... Now, I need to move a specific files; i.e, For this month, I need to move only OCT09, NOV09, DEC09,... (1 Reply)
Discussion started by: Jerald Nathan
1 Replies

8. UNIX for Dummies Questions & Answers

Copying dir (and sub dir) file names from ftp server to txt file in diff server

Hey all, i want to copy only the file names from an ftp server (directory and all sub directory) to a text file in another server (non ftp), i.e. i want to recursively move through directories and copy only the names to a text file. any help is appreciated...thank you in advance (1 Reply)
Discussion started by: deking
1 Replies

9. UNIX for Advanced & Expert Users

copying of files by userB, dir & files owned by userA

I am userB and have a dir /temp1 This dir is owned by me. How do I recursively copy files from another users's dir userA? I need to preserve the original user who created files, original group information, original create date, mod date etc. I tried cp -pr /home/userA/* . ... (2 Replies)
Discussion started by: Hangman2
2 Replies

10. Shell Programming and Scripting

copying a file from one dir to another dir

hi i have a script compareFiles() { find /tmp/Satya -type f | \ while read filename1 do echo "----------------------------------------$filename1" find /tmp/Satya -type f | \ while read filename2 do if diff $filename1 $filename2 then echo "Both files... (3 Replies)
Discussion started by: Satyak
3 Replies
Login or Register to Ask a Question