copy files as space exist in file name..


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers copy files as space exist in file name..
# 1  
Old 04-09-2010
copy files as space exist in file name..

Hi,
i am having a directory in which files are having space in the name .
Code:
$ls -1 
aa b.txt
my file.pdf
lost file.csv
foo_file.txt

i want to copy those file to some where with date +%F as extension . But it failed for the file having space.
Code:
#!/bin/sh
ls -1 >tt
for var in `cat tt`
do
b=$var
cp "$b" "$b"-`date +%F`
done

Even i failed to get solution using assigning " to variable and appending that variable while using cp .

Thanks & Regard's
posix
# 2  
Old 04-09-2010
Code:
#!/bin/sh

for var in *
do
   b=$var
   cp "$b" "$b"-`date +%F`
done

# 3  
Old 04-09-2010
Quote:
#!/bin/sh
ls -1 >tt
for var in `cat tt`
do
b=$var
cp "$b" "$b"-`date +%F`
done

We need to replace the "for" loop with a "while" loop to preserve spaces.
Note that the "ls -1" will not work sensibly if it picks up a directory name so we need to check whether it is a file not a directory.

Code:
#!/bin/sh
ls -1 | while read filename
do
        if [ -f "${filename} ]
        then
                 cp "${filename}" "${filename}-`date +%F`"
        fi
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File exist for multiple files

Hi, I am unable to achieve the file exist conditions if there are multiple files same similar name for e.g. in a $Direct i am having files like aus.txt aus.txt_pr aus.txt_2012 i need to put a file exist condition like which is not working then echo "File present" but the... (9 Replies)
Discussion started by: rohit_shinez
9 Replies

2. UNIX for Dummies Questions & Answers

Rsync copy files if dont exist

I have a setup where I have two drives. TV TVbackup For what ever reason, I have a lot of content on my TVbackup drive which isn't on my TV drive. I want to copy all the files across which are on TVbackup but are not currently on TV. If there is a file with the same name but a... (2 Replies)
Discussion started by: Spadez
2 Replies

3. Shell Programming and Scripting

Shell script to check files if exist else touch the file

Hi All, Thanks in Advance I wrote the following code if then echo "version is 1.1" for i in "subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account" do FILE="SDP_DUMP_$i.csv" echo "$FILE" ... (5 Replies)
Discussion started by: aealexanderraj
5 Replies

4. Shell Programming and Scripting

Code to remove files when corresponding file doesnt exist isnt working.

I am trying to add some code to the begging of a script so that it will remove all the .transcript files, when their is no coressponding .wav file. But it doesnt work. This is the code I have added: for transcriptfile in `$voicemaildir/*.transcript`; do wavfile=`echo $transcriptfile | cut -d'.'... (2 Replies)
Discussion started by: ghurty
2 Replies

5. UNIX for Dummies Questions & Answers

Copy multiple files with space to folder

Please help , I am in an urgent need, Please help nawk '{for(i=1;i<=NF;i++){printf("%s\n",$i)}}' filename | sed 's/.*com//' | nawk '/pdf/ {printf("F:%s\n",$0)}' | while read line; do mv $line /images/; done the above script works for without spaces but,My path is also having some space... (3 Replies)
Discussion started by: umapearl
3 Replies

6. Shell Programming and Scripting

Help File Copy Historical Files

Hello, I have a directory /files/storage in which I have files having file name in format as follows sabclin.yyyymmdd.0 e.g. sabclin.20110621.0 etc. I want to copy all files that have a date in their file name i.e. yyyymmdd from today till 300 days in the past to another directory, I need... (3 Replies)
Discussion started by: srattani
3 Replies

7. HP-UX

Copy only files whiich do not exist on Target Directory

Hi, I am using HP-UX B 11.23 I want to copy files from one directory into another directory in such a way that, only the nonexistent files in target directory are copied from source directory e.g. dir1 has file1 file2 file3 dir2 has file1 file3 now I want that a command... (7 Replies)
Discussion started by: Chetanaz
7 Replies

8. AIX

Can not copy file not enough space on LV

Hello, Please can someone help. I have created a lv called lv00 in datavg it has 8 PP's in use and each PP is 256MB I have 537 Free PP's Every time I copy my mksysb file to this new lv it gives me an error: 0653-447 Requested a write of ..... but wrote only ... errpt tells me... (2 Replies)
Discussion started by: pobman
2 Replies

9. Shell Programming and Scripting

How to check a file exist and do a copy of other files

Hi, I would like to perform bash which would check the file A.txt to be size 0 or not. If the size is 0, I would copy file B.txt to replace A.txt. Please help. Thanks. -Jason (6 Replies)
Discussion started by: ahjiefreak
6 Replies

10. Shell Programming and Scripting

Compare data in 2 files and delete if file exist

Hi there, I have written a script called "compare" (see below) to make comparison between 2 files namely test_put.log and Output_A0.log #!/bin/ksh while read file do found="no" while read line do echo $line | grep $file > /dev/null if then echo $file found found="yes" break fi... (3 Replies)
Discussion started by: lweegp
3 Replies
Login or Register to Ask a Question