Move files only if no other file is present there in first place


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move files only if no other file is present there in first place
# 1  
Old 12-05-2014
RedHat Move files only if no other file is present there in first place

Hi,

I am not that good in writing shell scripts. I need your help. I have a lot of files which need to moved to another directory but I can do it only if there are no files present there in the first place. I have an auto delete present to delete files from the destination Directory but still I can't figure out how to do it

here is my shell script

Code:
nofiles='ll /data1/app1 | wc -l'
if [ $nofiles -ne 1 ]
then
echo " files exist in the directory "
exit
else
cd /data2/app2
for file in $(ls -p | grep -v *.xml | head -100)
do
mv $file /data1/app1
done
fi

Please help me

Last edited by Don Cragun; 12-05-2014 at 03:50 AM.. Reason: Add CODE tags.
# 2  
Old 12-05-2014
What do you mean by "auto delete"? Is that some other, independent process?
Why don't you rm * on your target directory and check rm's exit code?
# 3  
Old 12-05-2014
Your script might work if you change this
Code:
nofiles='ll /data1/app1 | wc -l'

to this
Code:
nofiles=$(ll /data1/app1 | wc -l)

Right now you are comparing the literal string ll /data1/app1 | wc -l with 1.
# 4  
Old 12-05-2014
[Solved] Move files only if no other file is present there in first place

@ Rudy : files placed in the destination Directory are picked up by my java application for processing after which the Application itself deletes the files.

I did a few tweaks and finally got my script ready. Here is how it looks. I would be grateful for any inputs to improve it further.
Code:
#!/bin/bash

cd /data1/app1
nofiles=ll | wc -l
echo $nofiles
nullvalue="1"

if [ "$nofiles" > "$nullvalue" ];
then
echo " files exist in the directory "
exit

else

cd /data2/app2
for file in $(ls  | head -100)
do
mv $file /data1/app1
done
fi


Last edited by Franklin52; 12-08-2014 at 07:57 AM.. Reason: Please use code tags
# 5  
Old 12-05-2014
Please use code tags as required by forum rules!

In principle, using variables for one off use is somewhat pointless. And, why are you using ll instead of plain ls? Try
Code:
[ "$(ls)" != ""  ] && echo " files exist in the directory " || ... do your copying / moving

# 6  
Old 12-08-2014
Thanks Rudy,

Now the script works like a gem

Thanks a lot again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

2. Shell Programming and Scripting

Compare 2 files and extract the data which is present in other file - awk is not working

file2 content f1file2 content f1,1,2,3,4,5 f1,2,4,6,8,10 f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; !($1 in a)' file2 file1output f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; ($1 in a)' file2 file1output nothing... (4 Replies)
Discussion started by: gksenthilkumar
4 Replies

3. Shell Programming and Scripting

How to copy all the contents of a list of files present in a folder to a particular file?

Hi All, I want to copy all the contents of a list of files in a folder to a particular file. i am using following command: cat dir/* >> newFile.txtIt's not working. Could you please help? Thanks, Pranav (3 Replies)
Discussion started by: Pranav Bhasker
3 Replies

4. Shell Programming and Scripting

compare 2 files and extract the data which is not present in other file with condition

I have 2 files whose data's are as follows : fileA 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB alwayssng dkhf fdgdfg dfgdg sdjkgkdfjg funny rasp (7 Replies)
Discussion started by: rajniman
7 Replies

5. UNIX for Dummies Questions & Answers

using gsed with cp to sort files in directory - every N file copy to new place

Hi all, I'm having a problem with some basic piping issues... I have been able to get in a directory and ls | gsed in order to list every N file for instance: ls | gsed -n '2~5p' The thing is I want to be able to copy the output files to a new directory. Basically directory /all has a... (4 Replies)
Discussion started by: dgoss
4 Replies

6. Shell Programming and Scripting

Script to list files not present in audio.txt file

I am having following folder structure. /root/audios/pop /root/audios/jazz /root/audios/rock Inside those pop, jazz, rock folders there are following files, p1.ul, p2.ul, p3.ul, j1.ul, j2.ul, j3.ul, r1.ul, r2.ul, r3.ul And I have a file named as "audio.txt" in the path /root/audios,... (11 Replies)
Discussion started by: gopikrish81
11 Replies

7. Shell Programming and Scripting

cp RAW files if JPEG file present

hi guys and girls, i have a folder containing RAW and JPG images. eg... 001.jpg 003.jpg 005.jpg 001.raw 002.raw 003.raw 004.raw 005.raw I want to copy only RAW files that have a corresponding JPG file in to a new folder. the jpg files do not need to be copied. in this example i... (6 Replies)
Discussion started by: fxylxy
6 Replies

8. HP-UX

[Solved] Deleting a all core files present in file systems ?

Hi All IN HPUX 11 How to delete an unwanted "core" file with a single command which is being generated in different locations of the system the command should be able to free up the space occupied by all "core" file which is present in different folders and filesytems in a system ... (5 Replies)
Discussion started by: sidharthmellam
5 Replies

9. Shell Programming and Scripting

ls > file - Creating file containing the list of all files present in a directory

Hi All, I need to create a file which contains the list of all the files present in that directory. e.g., ls /export/home/user/*.dat > list_file.dat but what i am getting is: $ ls /export/home/user/*.dat > list_file.dat /export/home/user/*.dat: No such file or directory But I have... (1 Reply)
Discussion started by: pranavagarwal
1 Replies

10. Shell Programming and Scripting

Script to find all the files that contain any of the words present in another file

Hi All, I am new to UNIX and shell scripts and also new to this forum. I need a script to find all the files in a directory that contain any of the strings present in another file. Please provide me the script or if you could provide pointers to any link in this forum it would be helpful.... (4 Replies)
Discussion started by: tsanthosh
4 Replies
Login or Register to Ask a Question