[Solved] take name of directory and files as variables


Trivia Category: Science: Gadgets
Difficulty: Easy
Microphones can be used not only to pick up sound, but also to project sound similar to a speaker.
True or False?

 
Thread Tools Search this Thread
# 1  
[Solved] take name of directory and files as variables

hi,

want to create script that takes name of directory and all files and will copy each file to new directory.
then fix errors like files do not exist or no permission to create new directory...

these what I have so far...

Code:
#!/bin/sh
dir=~/Documents/Scripts/Copy

for i in $(pwd) $(ls)
do
echo "$i"
if [ -d $dir ]; then
cp -a * ~/Documents/Scripts/Copy #these would copy all files to Copy dir
                                           #but I want copy them one by one with
                                           #message that each one was copied
echo " files copied to directory $dir "
else
mkdir Copy && cp $i $dir
echo " $2 copied to directory $dir "
end


Last edited by vbe; 11-21-2011 at 11:33 AM..
# 2  
You will have to explain to me what you are trying to do in your script (I don't get you here... especially $1 $2 ...).

What are you trying to achieve? An almost exact copy of a directory content to a new directory? If so why not use appropriate unix commands ( but I can understand you also want to perfect your understanding and improve in shell scripting ! SmilieSmilie
# 3  
Quote:
Originally Posted by vbe
You will have to explain to me what you are trying to do in your script (I don't get you here... especially $1 $2 ...).

What are you trying to achieve? An almost exact copy of a directory content to a new directory? If so why not use appropriate unix commands ( but I can understand you also want to perfect your understanding and improve in shell scripting ! SmilieSmilie
hi vbe, exactly!!!
I want to improve here, I would say need to improve a lot Smilie

what I am trying to write a script that show where you are ( $pwd ) name of directory and all files within that directory then each file copy to other directory ( so my understanding was that $1 $2 ... would be those files ) but dont want to use
PHP Code:
cp * /new/direcotry 
, instead I want to have each file to be copied one by one with echo message showing which file was copied .
PHP Code:
echo " file one (name of file ) has been copied "
echo " file two ( name of file )has been copied " etc 
so I think, variables needs to be used for each file but need a help on that
#hope it sounds more clearly now
# 4  
Yes Smilie

So I will make you work on your script:
Here is how I would write the script (just little modifications for...) in order to see what is happening so you can understand and try to debug:

Code:
vi sample0001


#!/bin/sh -x
echo "======================"
echo " Start of  sample0001 "
echo "======================"

dir=~/Documents/Scripts/Copy

for i in $(pwd) $(ls)
do
   echo "$i"
   if [ -d $dir ]; then
      echo "\$2: " $2
      echo "idr : " $dir
      cp $2 $dir
      echo " $2 copied to directory $dir "
   else
      mkdir Copy && cp $2 $dir
      echo "line 18 -  $2 copied to directory $dir "
   fi
   echo next
   read kyb
done

Feel free to ask what is not clear to you...
# 5  
As you can see I have not changed anything in the logic (just added debugging material...)
The idea is you make this script executable and run it!
Then we can discuss what it is displaying
# 6  
Gone Silent?
(I am having connection issues... Is it working normally your side? (Connection Time outs...) )
# 7  
sorry, I was just all over the place with that script and missed your responses...

tried what you wrote but it didnt work so I was messing with it but without success...

i tried
PHP Code:
#!/bin/bash
dir="/home/andre/Documents/Scripts/Copy/"
FILE=(ls)

for 
i in $FILE
do
        
cp -i  "$1"  "${dir}${1}"
echo " file $1 has been copied to $dir "
cp -$3 $dir $3
echo " file $2 has been copied to $dir "
cp -$4 $dir $4
echo " file $3 has been copied to $dir "

done 
dont work as it should be

PHP Code:
done
for i in $(pwd) $(ls)
do
echo 
"$i"
if [ -d $dir ]; then
cp 
-* ~/Documents/Scripts/Copy #these would copy all files to Copy dir 

echo " files copied to directory $dir "
else 
mkdir Copy && cp $i $dir 
echo " $2 copied to directory $dir "
end 
these dont work either,
its like Smilie I am getting nowhere so far, to bad I missed your replies because would be helpful to chat with someone like you while doing....
Smilie
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] How to find particular files ina directory?

Hi, I am trying to write a script to find some files in a directory Example: if i have files like 2014-02-01_aaaa.txt 2014-02-01_bbbb.txt 2014-02-01_cccc.txt 2014-02-01_dddd.txt and some other files how can i just check to see if there four files exits or not i tried some thing like this... (5 Replies)
Discussion started by: vikatakavi
5 Replies

2. Shell Programming and Scripting

[SOLVED] nawk FS using pipe read variables from file

I have a file data_1.out which contains: 1|abc mail|mail subject|mail body 2|def mail|mail subject|def mail body I am trying to read the variables from data_1.out and use them to print to 2 different files based on the id (first_column) The problem is I am not able to read the file... (8 Replies)
Discussion started by: sol_nov
8 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Writing a loop to changing the names of files in a directory

Hi, I would like to write a loop to change the names of files in a directory. The files are called data1.txt through data1000.txt. I'd like to change their names to a1.txt through a1000.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

4. Shell Programming and Scripting

[solved] Question for using variables outside a while loop

I want to get newvar outside the while any ideas? while read myline; do var=${myline} newvar1=$(let "$var") done echo $newvar1 I found it its ok now Thank you! (0 Replies)
Discussion started by: sanantonio7777
0 Replies

5. Shell Programming and Scripting

[Solved] Working with date (add minutes using variables)

Dear all, today I'm scratching my head with a simple (I believe) issue. Working with date is quite simple, so if I Need to add some seconds to current time, I'll use: date --date='+30 seconds' +"%Y-%m-%d %H:%M:%S"But, how to pass the value to add from a variable? I tried the following without... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

6. Shell Programming and Scripting

Moving files from one directory to another based on 2 date variables

Hi All, I am currently coding for a requirement(LINUX OS) where I am supposed to move a file (Lets Call it Employee.txt) from Directory A to Directory B based on 2 date fields as below, Date_Current = 20120620 Date_Previous = 20120610 Source Directory : /iis_data/source Target... (11 Replies)
Discussion started by: dsfreddie
11 Replies

7. Shell Programming and Scripting

[Solved] Bash test 2 variables to see if ones greater by n

Experts, I have a bash shell script that generates 2 variables that have the current minute and a minute from a log file. Can someone please show me the best way to test if the minutes stray by 5. So basically if: This is ok: Last Fitting Min ============= 02 Current Minute =============... (2 Replies)
Discussion started by: jaysunn
2 Replies

8. Shell Programming and Scripting

[Solved] Look for strings and use variables

Hello guys, First of all, thanks for taking the time to read my post. So, here I have a file from my honeypot which record IP addresses and web pages visited. I would like to manipulate it in order to create Snort signatures and ACLs. But I am having troubles to extract the src IP address and... (6 Replies)
Discussion started by: Benou
6 Replies

9. UNIX for Dummies Questions & Answers

[SOLVED] Delete files and folders under given directory

I have a requirement to delete the files and folders under a given directory. my directory structure is like this.. Data | A(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files |--files | B(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files ... (1 Reply)
Discussion started by: ramse8pc
1 Replies

10. Shell Programming and Scripting

[solved] merging two files and writing to another file- solved

i have two files as file1: 1 2 3 file2: a b c and the output should be: file3: 1~a 2~b 3~c (1 Reply)
Discussion started by: mlpathir
1 Replies

Featured Tech Videos