How can I check, if on remote server directory is empty or have files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I check, if on remote server directory is empty or have files?
# 1  
Old 04-07-2017
How can I check, if on remote server directory is empty or have files?

I have a script, which is supposed to run 1 day of the month, connect to remote server certain directory, find files, tar the, and copy

Code:
  
 find . -ctime -1 | tar -cvf transfer_dmz_start_monthly.tar *${Today}*.*;
 if [ $? != 0 ]
then
        echo "Cannot create a tar file, the terminated abnormaly"
        exit 1
fi
gzip transfer_dmz_start_monthly.tar
  
 /usr/bin/scp ${DESTSERVNAME}:${MONTH_DEST_DIR}/${TARGZFILE} ${LOCL_FILES_DIR}/cool_${Today}/monthly

Now problem was changed
I have to check if directory has files for Today's day. If not - exit, if yes, then logic above.

Is below code will work?

Code:
 if [ `ls | wc -l` = 0 ]
then
   exit
 else

find . -ctime -1 | tar -cvf transfer_dmz_start_monthly.tar *${Today}*.*;

 fi

Or you can find something better.
I am on AIX and have no bash
BTW, it will always have archived files , which I don't need. I.e. probably
Code:
 ls ${Today}

Thanks for contribution
# 2  
Old 04-07-2017
Code:
cnt=$(ls ${Today}* 2>/dev/null | wc -l)
if [ $cnt -eq 0 ] ; then
   echo 'No files found to archive'
   exit
fi
# run the rest of the script here

# 3  
Old 04-08-2017
Why not just evaluate ls's exit code? Like
Code:
ls ${Today}* || exit

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Shell script to find the GB files in /tmp directory in remote server

Hi, i need help on shell scripting. Main intention of the script is step 1: ssh to remote server Step 2: cd /tmp in remote server Step 3: in tmp i want to grep only files and directories which are in GB sizes All the servers list file is - tmpsrv.txt vi tmpsrv.txt ... (17 Replies)
Discussion started by: kumar85shiv
17 Replies

3. Homework & Coursework Questions

Check whether a Directory is empty or not

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1.pls tell me the command for checking whether a given directory is empty or not . 2. can i check what is the... (1 Reply)
Discussion started by: upvan111
1 Replies

4. Shell Programming and Scripting

Check whether a Directory is empty or not

1.pls tell me the command for checking whether a given directory is empty or not . 2. can i check what is the last copied item in a directory . and i yes , i want to move that last copied item in another directory . pls help me with shell code for these two tasks thanks (1 Reply)
Discussion started by: upvan111
1 Replies

5. Shell Programming and Scripting

to check if a remote server is up

I need to check if a remote server is up before i send a file to it through FTP. I thought of using "ping" command to check it. But the problem is, my script needs to run from 2 different servers, one is Solaris and other is HP-UX. And ping works in different way in each of them when I do -... (3 Replies)
Discussion started by: Vidhyaprakash
3 Replies

6. Shell Programming and Scripting

Check files copied from remote server

There is a process which copy files form unix a to unix b I would like to check whether all files copied from a to b or not ,and list which are the missing files. Is there a command to check like that (3 Replies)
Discussion started by: lalitpct
3 Replies

7. Shell Programming and Scripting

FTP files from different directory from remote server to one directory in local

Hi All, I want to search for .log files from folders and sub folders in remote server and FTP them to one particular folder in the local machine. I dont want to copy the entire directory tree structure, just have to take all the .log files from all the folders by doing a recursive search from the... (3 Replies)
Discussion started by: dassv
3 Replies

8. Shell Programming and Scripting

Empty Directory Check

Hi All, I have a requirement to check all the files in a directory and mail non empty files Files are in csv format , i need to skip header while checking pls help Thanks (12 Replies)
Discussion started by: gwrm
12 Replies

9. Shell Programming and Scripting

check empty directory !!!

I need to check if a directory is empty using an if condition in the pseudocode below if ; then else although i looked at a few forums on this topic, I left feeling a little unclear and i could not use the command successfully what can i substitute in the if conditon above,... (2 Replies)
Discussion started by: allah_waris45
2 Replies

10. Shell Programming and Scripting

check whether the directory is empty or not

I have the list of users in user.log, under each user folder there is sub1 folder is there. i want to check whether sub1 is empty or not, if it is empty i have to skip that user user folder and iterate next user folders. i have the sample code,its not giving not proper results. while read line... (8 Replies)
Discussion started by: KiranKumarKarre
8 Replies
Login or Register to Ask a Question