Testing for the presence or absence of a folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Testing for the presence or absence of a folder
# 1  
Old 03-03-2018
Testing for the presence or absence of a folder

My directory: /folder1/folder2/file.fna.gz

Now following on from renaming the files of interest (fna.gz) I now need to loop through folder1.
As I loop through folder1 I am trying to first of all capture the first word in the file before the first underscore, for example if the folder name is /Long_John_Silver/ then I am trying to capture only "Long".
Then I want to check whether there is a folder with the same name "Long" in a distant directory /another/directory/ and if there is then a move onto the next folder in folder1, but if there isn't then I create a new folder with the captured name i.e. "Long".

Here's my work so far, I am struggling with the capturing of "Long" part.

Code:
for fldr in /folder1/folder2/.fna.gz ; do

## Trying to regex capture here but I'm unclear on the syntax, there's expansion, grep  etc but don't know which to use. 
    new_fldr = ${fldr} 

## Then the test for existence.
    if [-f /another/directory/$new_fldr ]; then

        echo "Folder exists already, moving on..." ;

    else [ ! -f /another/directory/$new_fldr ];

        echo "Folder not present, creating folder..." ;

        cd /another/directory/ ;

        mkdir $new_fldr ;

# 2  
Old 03-04-2018
Code:
for a in `find /folder1 -type d `
{
 test -d "$a" || mkdir -p "${a##*/}"
}


Last edited by abdulbadii; 03-04-2018 at 06:09 PM..
# 3  
Old 03-04-2018
How about this;

Code:
for fldr in ./folder1/* ; do
   basenm=${fldr##*/}
   if [[ "$basenm" = *_* ]] && [ -d "$fldr" ]; then
       new_fldr=${basenm%%_*}
       if [ -d "/another/directory/$new_fldr" ]; then
          echo "Folder exists already, moving on..." ;
       else
          echo "Folder not present, creating folder..." ;
          mkdir "/another/directory/$new_fldr" ;
       fi
   else
      echo "not looking at $fldr"
   fi
done

Notice how most references to basenm, fldr and newdir are quoted, this is necessary if any of your folder names could ever contain white space characters (like space or newline).

There is no need to cd into /another/directory to create folders, and doing this will cause the next loop to fail, so just pass the full directory path to mkdir and keep the current directory where it is.
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check presence of trigger file

Hi, I make a sftp connection successfully.My requirement is the script shall execute only after i find a trigger file(dailyreport.OK.psv) in the remote dir. If the trigger file is not present ,the script should exit else it should continue with the rest of the scripts statements. Below code is... (13 Replies)
Discussion started by: samrat dutta
13 Replies

2. Shell Programming and Scripting

Identifying presence and name of new file(s)?

I have an HP-UX server that runs a script each night. The script connects to an SFTP server and downloads all xml files (if any are present) from a certain folder, and then deletes the files from the SFTP server. So sometimes it will download a new file, sometimes it will download 2 or 3 new... (4 Replies)
Discussion started by: lupin..the..3rd
4 Replies

3. Shell Programming and Scripting

Check the presence of file >size?

Hi, Following script work fine: #!/bin/bash FILE=$1 if ; then echo Yay else echo Boo fi But I would like to add another condition that if FILE... (3 Replies)
Discussion started by: nrjrasaxena
3 Replies

4. Shell Programming and Scripting

check presence of input file

My script is taking a file "input.in" as input for running the script. My worry is that i need to execute the script only if the file is present, if it's not don't perform the next commands. Just to have a check at the beginning of the script : If "input.in" exists, then go on. If it's does not... (4 Replies)
Discussion started by: newpromo
4 Replies

5. Shell Programming and Scripting

Polling continously for presence of a file

Hi, My os is sun solaris 5.10 and Korn shell scripting. I have a file name like CCNA_EARLY_SWP.w062309 where 062309 is date in mmddyy .This is the value date of the file.(will I need to check continously from 5.00 - 7.00 am daily for this file . If the file has not come at 5 am or 7am... (4 Replies)
Discussion started by: manoj39
4 Replies

6. Shell Programming and Scripting

Checking for presence of any files

Is there code in Cshell scripting to check for the presence of any files in the current directory (and only the current directory)? I tried: if (-r *) then ... but Cshell doesn't like that. Thanks, Paul Hudgens (0 Replies)
Discussion started by: phudgens
0 Replies

7. Shell Programming and Scripting

Checking for presence of any files

I have tried the following script to check for the presence of any files in a folder: if (-r *) then goto ZipMiscFiles else echo "" echo " No Miscellaneous files found. Exiting program." echo "" exit endif The -r works fine with the wildcard in combo with other... (4 Replies)
Discussion started by: phudgens
4 Replies

8. Shell Programming and Scripting

testing for presence of files in ftp

I have a shell script which GETS a file, but I only want to run this script if $FILE exists on the remote host, don't know correct syntax.....please help ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS $PASSWD bin get $FILE rename export.prn export.prn.$TIMESTAMP !cat... (0 Replies)
Discussion started by: walterja
0 Replies
Login or Register to Ask a Question